/
ovs_gdb.py
572 lines (477 loc) · 18.6 KB
/
ovs_gdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#
# Copyright (c) 2018 Eelco Chaudron
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# Files name:
# ovs_gdb.py
#
# Description:
# GDB commands and functions for Open vSwitch debugging
#
# Author:
# Eelco Chaudron
#
# Initial Created:
# 23 April 2018
#
# Notes:
# It implements the following GDB commands:
# - ovs_dump_bridge [ports|wanted]
# - ovs_dump_bridge_ports <struct bridge *>
# - ovs_dump_dp_netdev [ports]
# - ovs_dump_dp_netdev_poll_threads <struct dp_netdev *>
# - ovs_dump_dp_netdev_ports <struct dp_netdev *>
# - ovs_dump_netdev
# - ovs_dump_ovs_list <struct ovs_list *> {[<structure>] [<member>] {dump}]}
#
# Example:
# $ gdb $(which ovs-vswitchd) $(pidof ovs-vswitchd)
# (gdb) source ./utilities/gdb/ovs_gdb.py
#
# (gdb) ovs_dump_<TAB>
# ovs_dump_bridge ovs_dump_bridge_ports ovs_dump_dp_netdev
# ovs_dump_dp_netdev_ports ovs_dump_netdev
#
# (gdb) ovs_dump_bridge
# (struct bridge *) 0x5615471ed2e0: name = br2, type = system
# (struct bridge *) 0x561547166350: name = br0, type = system
# (struct bridge *) 0x561547216de0: name = ovs_pvp_br0, type = netdev
# (struct bridge *) 0x5615471d0420: name = br1, type = system
#
# (gdb) p *(struct bridge *) 0x5615471d0420
# $1 = {node = {hash = 24776443, next = 0x0}, name = 0x5615471cca90 "br1",
# type = 0x561547163bb0 "system",
# ...
# ...
#
import gdb
#
# The container_of code below is a copied from the Linux kernel project file,
# scripts/gdb/linux/utils.py. It has the following copyright header:
#
# # gdb helper commands and functions for Linux kernel debugging
# #
# # common utilities
# #
# # Copyright (c) Siemens AG, 2011-2013
# #
# # Authors:
# # Jan Kiszka <jan.kiszka@siemens.com>
# #
# # This work is licensed under the terms of the GNU GPL version 2.
#
class CachedType:
def __init__(self, name):
self._type = None
self._name = name
def _new_objfile_handler(self, event):
self._type = None
gdb.events.new_objfile.disconnect(self._new_objfile_handler)
def get_type(self):
if self._type is None:
self._type = gdb.lookup_type(self._name)
if self._type is None:
raise gdb.GdbError(
"cannot resolve type '{0}'".format(self._name))
if hasattr(gdb, 'events') and hasattr(gdb.events, 'new_objfile'):
gdb.events.new_objfile.connect(self._new_objfile_handler)
return self._type
long_type = CachedType("long")
def get_long_type():
global long_type
return long_type.get_type()
def offset_of(typeobj, field):
element = gdb.Value(0).cast(typeobj)
return int(str(element[field].address).split()[0], 16)
def container_of(ptr, typeobj, member):
return (ptr.cast(get_long_type()) -
offset_of(typeobj, member)).cast(typeobj)
#
# Class that will provide an iterator over an OVS cmap.
#
class ForEachCMAP(object):
def __init__(self, cmap, typeobj=None, member='node'):
self.cmap = cmap
self.first = True
self.typeobj = typeobj
self.member = member
# Cursor values
self.node = 0
self.bucket_idx = 0
self.entry_idx = 0
def __iter__(self):
return self
def __get_CMAP_K(self):
ptr_type = gdb.lookup_type("void").pointer()
return (64 - 4) / (4 + ptr_type.sizeof)
def __next(self):
ipml = self.cmap['impl']['p']
if self.node != 0:
self.node = self.node['next']['p']
if self.node != 0:
return
while self.bucket_idx <= ipml['mask']:
buckets = ipml['buckets'][self.bucket_idx]
while self.entry_idx < self.__get_CMAP_K():
self.node = buckets['nodes'][self.entry_idx]['next']['p']
self.entry_idx += 1
if self.node != 0:
return
self.bucket_idx += 1
self.entry_idx = 0
raise StopIteration
def next(self):
ipml = self.cmap['impl']['p']
if ipml['n'] == 0:
raise StopIteration
self.__next()
if self.typeobj is None:
return self.node
return container_of(self.node,
gdb.lookup_type(self.typeobj).pointer(),
self.member)
#
# Class that will provide an iterator over an OVS hmap.
#
class ForEachHMAP(object):
def __init__(self, hmap, typeobj=None, member='node'):
self.hmap = hmap
self.node = None
self.first = True
self.typeobj = typeobj
self.member = member
def __iter__(self):
return self
def __next(self, start):
for i in range(start, (self.hmap['mask'] + 1)):
self.node = self.hmap['buckets'][i]
if self.node != 0:
return
raise StopIteration
def next(self):
#
# In the real implementation the n values is never checked,
# however when debugging we do, as we might try to access
# a hmap that has been cleared/hmap_destroy().
#
if self.hmap['n'] <= 0:
raise StopIteration
if self.first:
self.first = False
self.__next(0)
elif self.node['next'] != 0:
self.node = self.node['next']
else:
self.__next((self.node['hash'] & self.hmap['mask']) + 1)
if self.typeobj is None:
return self.node
return container_of(self.node,
gdb.lookup_type(self.typeobj).pointer(),
self.member)
#
# Class that will provide an iterator over an OVS shash.
#
class ForEachSHASH(ForEachHMAP):
def __init__(self, shash, typeobj=None):
self.data_typeobj = typeobj
super(ForEachSHASH, self).__init__(shash['map'],
"struct shash_node", "node")
def next(self):
node = super(ForEachSHASH, self).next()
if self.data_typeobj is None:
return node
return node['data'].cast(gdb.lookup_type(self.data_typeobj).pointer())
#
# Class that will provide an iterator over an OVS list.
#
class ForEachLIST():
def __init__(self, list, typeobj=None, member='node'):
self.list = list
self.node = list
self.typeobj = typeobj
self.member = member
def __iter__(self):
return self
def next(self):
if self.list.address == self.node['next']:
raise StopIteration
self.node = self.node['next']
if self.typeobj is None:
return self.node
return container_of(self.node,
gdb.lookup_type(self.typeobj).pointer(),
self.member)
#
# Implements the GDB "ovs_dump_bridges" command
#
class CmdDumpBridge(gdb.Command):
"""Dump all configured bridges.
Usage: ovs_dump_bridge [ports|wanted]
"""
def __init__(self):
super(CmdDumpBridge, self).__init__("ovs_dump_bridge",
gdb.COMMAND_DATA)
def invoke(self, arg, from_tty):
ports = False
wanted = False
arg_list = gdb.string_to_argv(arg)
if len(arg_list) > 1 or \
(len(arg_list) == 1 and arg_list[0] != "ports" and
arg_list[0] != "wanted"):
print("usage: ovs_dump_bridge [ports|wanted]")
return
elif len(arg_list) == 1:
if arg_list[0] == "ports":
ports = True
else:
wanted = True
dp_netdevs = gdb.lookup_symbol('all_bridges')[0]
if dp_netdevs is None or not dp_netdevs.is_variable:
print("Can't find all_bridges global variable, are you sure "
"your debugging OVS?")
return
all_bridges = gdb.parse_and_eval('all_bridges')
for node in ForEachHMAP(all_bridges,
"struct bridge", "node"):
print("(struct bridge *) {}: name = {}, type = {}".
format(node, node['name'].string(),
node['type'].string()))
if ports:
for port in ForEachHMAP(node['ports'],
"struct port", "hmap_node"):
CmdDumpBridgePorts.display_single_port(port, 4)
if wanted:
for port in ForEachSHASH(node['wanted_ports'],
typeobj="struct ovsrec_port"):
print(" (struct ovsrec_port *) {}: name = {}".
format(port, port['name'].string()))
# print port.dereference()
#
# Implements the GDB "ovs_dump_bridge_ports" command
#
class CmdDumpBridgePorts(gdb.Command):
"""Dump all ports added to a specific struct bridge*.
Usage: ovs_dump_bridge_ports <struct bridge *>
"""
def __init__(self):
super(CmdDumpBridgePorts, self).__init__("ovs_dump_bridge_ports",
gdb.COMMAND_DATA)
@staticmethod
def display_single_port(port, indent=0):
indent = " " * indent
port = port.cast(gdb.lookup_type('struct port').pointer())
print("{}(struct port *) {}: name = {}, brige = (struct bridge *) {}".
format(indent, port, port['name'].string(),
port['bridge']))
indent += " " * 4
for iface in ForEachLIST(port['ifaces'], "struct iface", "port_elem"):
print("{}(struct iface *) {}: name = {}, ofp_port = {}, "
"netdev = (struct netdev *) {}".
format(indent, iface, iface['name'],
iface['ofp_port'], iface['netdev']))
def invoke(self, arg, from_tty):
arg_list = gdb.string_to_argv(arg)
if len(arg_list) != 1:
print("usage: ovs_dump_bridge_ports <struct bridge *>")
return
bridge = gdb.parse_and_eval(arg_list[0]).cast(
gdb.lookup_type('struct bridge').pointer())
for node in ForEachHMAP(bridge['ports'],
"struct port", "hmap_node"):
self.display_single_port(node)
#
# Implements the GDB "ovs_dump_dp_netdev" command
#
class CmdDumpDpNetdev(gdb.Command):
"""Dump all registered dp_netdev structures.
Usage: ovs_dump_dp_netdev [ports]
"""
def __init__(self):
super(CmdDumpDpNetdev, self).__init__("ovs_dump_dp_netdev",
gdb.COMMAND_DATA)
def invoke(self, arg, from_tty):
ports = False
arg_list = gdb.string_to_argv(arg)
if len(arg_list) > 1 or \
(len(arg_list) == 1 and arg_list[0] != "ports"):
print("usage: ovs_dump_dp_netdev [ports]")
return
elif len(arg_list) == 1:
ports = True
dp_netdevs = gdb.lookup_symbol('dp_netdevs')[0]
if dp_netdevs is None or not dp_netdevs.is_variable:
print("Can't find dp_netdevs global variable, are you sure "
"your debugging OVS?")
return
dp_netdevs = gdb.parse_and_eval('dp_netdevs')
for node in ForEachSHASH(dp_netdevs):
dp = node['data'].cast(
gdb.lookup_type('struct dp_netdev').pointer())
print("(struct dp_netdev *) {}: name = {}, class = "
"(struct dpif_class *) {}".
format(dp, dp['name'], dp['class']))
if ports:
for node in ForEachHMAP(dp['ports'],
"struct dp_netdev_port", "node"):
CmdDumpDpNetdevPorts.display_single_port(node, 4)
#
# Implements the GDB "ovs_dump_dp_netdev_poll_threads" command
#
class CmdDumpDpNetdevPollThreads(gdb.Command):
"""Dump all poll_thread info added to a specific struct dp_netdev*.
Usage: ovs_dump_dp_netdev_poll_threads <struct dp_netdev *>
"""
def __init__(self):
super(CmdDumpDpNetdevPollThreads, self).__init__(
"ovs_dump_dp_netdev_poll_threads",
gdb.COMMAND_DATA)
@staticmethod
def display_single_poll_thread(pmd_thread, indent=0):
indent = " " * indent
print("{}(struct dp_netdev_pmd_thread *) {}: core_id = {:s}, "
"numa_id {}".format(indent,
pmd_thread, pmd_thread['core_id'],
pmd_thread['numa_id']))
def invoke(self, arg, from_tty):
arg_list = gdb.string_to_argv(arg)
if len(arg_list) != 1:
print("usage: ovs_dump_dp_netdev_poll_threads "
"<struct dp_netdev *>")
return
dp_netdev = gdb.parse_and_eval(arg_list[0]).cast(
gdb.lookup_type('struct dp_netdev').pointer())
for node in ForEachCMAP(dp_netdev['poll_threads'],
"struct dp_netdev_pmd_thread", "node"):
self.display_single_poll_thread(node)
#
# Implements the GDB "ovs_dump_dp_netdev_ports" command
#
class CmdDumpDpNetdevPorts(gdb.Command):
"""Dump all ports added to a specific struct dp_netdev*.
Usage: ovs_dump_dp_netdev_ports <struct dp_netdev *>
"""
def __init__(self):
super(CmdDumpDpNetdevPorts, self).__init__("ovs_dump_dp_netdev_ports",
gdb.COMMAND_DATA)
@staticmethod
def display_single_port(port, indent=0):
indent = " " * indent
print("{}(struct dp_netdev_port *) {}:".format(indent, port))
print("{} port_no = {}, n_rxq = {}, type = {}".
format(indent, port['port_no'], port['n_rxq'],
port['type'].string()))
print("{} netdev = (struct netdev *) {}: name = {}, "
"n_txq/rxq = {}/{}".
format(indent, port['netdev'],
port['netdev']['name'].string(),
port['netdev']['n_txq'],
port['netdev']['n_rxq']))
def invoke(self, arg, from_tty):
arg_list = gdb.string_to_argv(arg)
if len(arg_list) != 1:
print("usage: ovs_dump_dp_netdev_ports <struct dp_netdev *>")
return
dp_netdev = gdb.parse_and_eval(arg_list[0]).cast(
gdb.lookup_type('struct dp_netdev').pointer())
for node in ForEachHMAP(dp_netdev['ports'],
"struct dp_netdev_port", "node"):
# print node.dereference()
self.display_single_port(node)
#
# Implements the GDB "ovs_dump_netdev" command
#
class CmdDumpNetdev(gdb.Command):
"""Dump all registered netdev structures.
Usage: ovs_dump_netdev
"""
def __init__(self):
super(CmdDumpNetdev, self).__init__("ovs_dump_netdev",
gdb.COMMAND_DATA)
@staticmethod
def display_single_netdev(netdev, indent=0):
indent = " " * indent
print("{}(struct netdev *) {}: name = {:15}, auto_classified = {:5}, "
"netdev_class = {}".
format(indent, netdev, netdev['name'].string(),
netdev['auto_classified'], netdev['netdev_class']))
def invoke(self, arg, from_tty):
netdev_shash = gdb.lookup_symbol('netdev_shash')[0]
if netdev_shash is None or not netdev_shash.is_variable:
print("Can't find netdev_shash global variable, are you sure "
"your debugging OVS?")
return
netdev_shash = gdb.parse_and_eval('netdev_shash')
for netdev in ForEachSHASH(netdev_shash, "struct netdev"):
self.display_single_netdev(netdev)
#
# Implements the GDB "ovs_dump_ovs_list" command
#
class CmdDumpOvsList(gdb.Command):
"""Dump all nodes of an ovs_list give
Usage: ovs_dump_ovs_list <struct ovs_list *> {[<structure>] [<member>] {dump}]}
For example dump all the none quiescent OvS RCU threads:
(gdb) ovs_dump_ovs_list &ovsrcu_threads
(struct ovs_list *) 0x7f2a14000900
(struct ovs_list *) 0x7f2acc000900
(struct ovs_list *) 0x7f2a680668d0
This is not very useful, so please use this with the container_of mode:
(gdb) ovs_dump_ovs_list &ovsrcu_threads 'struct ovsrcu_perthread' list_node
(struct ovsrcu_perthread *) 0x7f2a14000900
(struct ovsrcu_perthread *) 0x7f2acc000900
(struct ovsrcu_perthread *) 0x7f2a680668d0
Now you can manually use the print command to show the content, or use the
dump option to dump the structure for all nodes:
(gdb) ovs_dump_ovs_list &ovsrcu_threads 'struct ovsrcu_perthread' list_node dump
(struct ovsrcu_perthread *) 0x7f2a14000900 =
{list_node = {prev = 0xf48e80 <ovsrcu_threads>, next = 0x7f2acc000900}, mutex...
(struct ovsrcu_perthread *) 0x7f2acc000900 =
{list_node = {prev = 0x7f2a14000900, next = 0x7f2a680668d0}, mutex ...
(struct ovsrcu_perthread *) 0x7f2a680668d0 =
{list_node = {prev = 0x7f2acc000900, next = 0xf48e80 <ovsrcu_threads>}, ...
"""
def __init__(self):
super(CmdDumpOvsList, self).__init__("ovs_dump_ovs_list",
gdb.COMMAND_DATA)
def invoke(self, arg, from_tty):
arg_list = gdb.string_to_argv(arg)
typeobj = None
member = None
dump = False
if len(arg_list) != 1 and len(arg_list) != 3 and len(arg_list) != 4:
print("usage: ovs_dump_ovs_list <struct ovs_list *> "
"{[<structure>] [<member>] {dump}]}")
return
header = gdb.parse_and_eval(arg_list[0]).cast(
gdb.lookup_type('struct ovs_list').pointer())
if len(arg_list) >= 3:
typeobj = arg_list[1]
member = arg_list[2]
if len(arg_list) == 4 and arg_list[3] == "dump":
dump = True
for node in ForEachLIST(header.dereference()):
if typeobj is None or member is None:
print("(struct ovs_list *) {}".format(node))
else:
print("({} *) {} =".format(
typeobj,
container_of(node,
gdb.lookup_type(typeobj).pointer(), member)))
if dump:
print(" {}\n".format(container_of(
node,
gdb.lookup_type(typeobj).pointer(),
member).dereference()))
#
# Initialize all GDB commands
#
CmdDumpBridge()
CmdDumpBridgePorts()
CmdDumpDpNetdev()
CmdDumpDpNetdevPollThreads()
CmdDumpDpNetdevPorts()
CmdDumpNetdev()
CmdDumpOvsList()