blob: 9bfb9c9c2ece53482574c6b95ea808a97bbbd5c4 [file] [log] [blame]
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +02001#!/usr/bin/env python3
2doc=r'''
3Reading a log, it can be hard to figure out what is the important bit going on.
4This is a tool that reads an osmo-msc log and tries to compose a ladder diagram from it automatically.
5'''
6
7import argparse
8import sys
9import re
10import tempfile
11import os
12
13def error(*msg):
14 sys.stderr.write('%s\n' % (''.join(msg)))
15 exit(1)
16
17def quote(msg, quote='"'):
18 return '"%s"' % (msg.replace('"', r'\"'))
19
20class Entity:
21 def __init__(self, name, descr=None, attrs={}):
22 self.name = name
23 self.descr = descr
24 self.attrs = attrs
25
26class Arrow:
27 def __init__(self, mo_mt, left, arrow, right, descr=None, attrs={}, ran_conn=None, imsi=None, tmsi=None):
28 self.mo_mt = mo_mt
29 self.left = left
30 self.arrow = arrow
31 self.right = right
32 self.descr = descr
33 self.attrs = attrs
34 self.ran_conn = ran_conn
35 self.imsi = imsi
36 self.tmsi = tmsi
37
38 def __repr__(self):
39 return 'Arrow(%s %s %s %s: %s IMSI=%s)' % (self.mo_mt, self.left, self.arrow, self.right, self.descr, self.imsi)
40
41class Separator:
42 def __init__(self):
43 self.separator = None
44 self.descr = None
45 self.attrs = {}
46
47class EmptyLine:
48 def __init__(self):
49 self.count = 1
50
51MS = 'ms'
52UE = 'ms' #'ue'
53MS_UE_UNKNOWN = 'ms' #None
54MSC = 'msc'
55MGW = 'mgw'
Neels Hofmeyr075ca602023-03-08 00:53:47 +010056MNCC = 'mncc'
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +020057
58MO = 'mo'
59MT = 'mt'
60MO_MT_UNKNOWN = None
61
62
63class OutputBase:
64 def __init__(self, write_to, start_with_re=None):
65 self._write_to = write_to
66
67 self.start_with_re = None
68 if start_with_re is not None:
69 self.start_with_re = re.compile(start_with_re)
70
71 def head(self):
Neels Hofmeyr075ca602023-03-08 00:53:47 +010072 self.writeln('# Generated by osmo-msc.git/doc/sequence_charts/msc_log_to_ladder.py')
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +020073
74 def tail(self):
75 pass
76
77 def render(self, diagram):
78 self.head()
79 if diagram.root_attrs:
80 self.root_attrs(diagram.root_attrs)
81 self.entities(diagram.entities)
82
83 started = (self.start_with_re is None)
84
85 for line in diagram.lines:
86 if not started:
87 if hasattr(line, 'descr') and self.start_with_re.match(line.descr):
88 started = True
89 else:
90 continue
91 self.add(line)
92 self.tail()
93
94 def entities(self, entities):
95 for entity in entities:
96 self.entity(entity)
97
98 def write(self, line):
99 self._write_to.write(line)
100
101 def writeln(self, line):
102 self.write('%s\n' % line)
103
104 def emptyline(self, emptyline):
105 self.write('\n' * emptyline.count);
106
107 def add(self, thing):
108 func = getattr(self, thing.__class__.__name__.lower())
109 func(thing)
110
111
112class OutputLadder(OutputBase):
113
114 def indent_multiline(self, s):
115 return s.replace('\n', '\n\t')
116
117 def attrs_str(self, attrs, prefix=' '):
118 if not attrs:
119 return ''
120 return '%s{%s}' % (prefix or '', ','.join('%s=%s' % (k,v) for k,v in attrs.items()))
121
122 def root_attrs(self, attrs):
123 self.writeln(self.attrs_str(attrs, prefix=None))
124
125 def entity(self, entity):
126 self.writeln('%s = %s%s' % (entity.name, self.indent_multiline(entity.descr), self.attrs_str(entity.attrs)))
127
128 def arrow(self, arrow):
129 mo_mt = arrow.mo_mt or MO
130
131 def prepend_mo_mt(name):
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100132 if name in ('.', MNCC):
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200133 return name
134 return '%s%s' % (mo_mt, name)
135
136 self.writeln('%s %s %s%s%s%s'
137 % (prepend_mo_mt(arrow.left),
138 arrow.arrow,
139 prepend_mo_mt(arrow.right),
140 '\t' if arrow.descr else '',
141 self.indent_multiline(arrow.descr or ''),
142 self.attrs_str(arrow.attrs)))
143
144 def separator(self, sep_str, descr, attrs):
145 self.writeln('%s%s%s%s'
146 % (separator.separator,
147 ' ' if separator.descr else '',
148 self.indent_multiline(separator.descr or ''),
149 self.attrs_str(separator.attrs)))
150
151class OutputMscgen(OutputBase):
152 ARROWS = {
153 '>' : '=>>',
154 '->' : '=>',
155 '-->' : '>>',
156 '~>' : '->',
157 '=>' : ':>',
158 '-><' : '-x',
159
160 '<' : '<<=',
161 '<-' : '<=',
162 '<--' : '<<',
163 '<~' : '<-',
164 '<=' : '<:',
165 '><-' : 'x-',
166
167 '<>' : 'abox',
168 '()' : 'rbox',
169 '[]' : 'note',
170 }
171
172 def head(self):
173 super().head()
174 self.writeln('msc {')
175
176 def tail(self):
177 self.writeln('}')
178
179 def entities(self, entities):
180 estr = []
181 for entity in entities:
182 estr.append('%s%s' % (entity.name, self.attrs_str(self.all_attrs(entity.descr, entity.attrs), prefix='')))
183 if not estr:
184 return
185 self.writeln('%s;' % (','.join(estr)))
186
187 def attrs_list_str(self, attrs):
188 if not attrs:
189 return ''
190 def escape(s):
191 return s.replace('\n', r'\n').replace('\r', '').replace('\t', r'\t')
192 return ','.join('%s="%s"' % (k,escape(v)) for k,v in attrs.items())
193
194 def attrs_str(self, attrs, prefix=' '):
195 attrs_list_str = self.attrs_list_str(attrs)
196 if not attrs_list_str:
197 return ''
198 return '%s[%s]' % (prefix or '', attrs_list_str)
199
200 def root_attrs(self, attrs):
201 if not attrs:
202 return
203 self.writeln('%s;' % self.attrs_list_str(attrs))
204
205 def all_attrs(self, descr, attrs):
206 a = {}
207 if descr:
208 a['label'] = descr
209 a.update(attrs)
210 return a
211
212 def entity(self, entity):
213 error('OutputMscgen.entity() should not be called')
214
215 def arrow_txlate(self, arrow):
216 txlate = OutputMscgen.ARROWS.get(arrow)
217 if not txlate:
218 error('Unknown arrow: %r' % arrow)
219 return txlate
220
221 def arrow(self, arrow):
222 mo_mt = arrow.mo_mt or MO
223
224 def prepend_mo_mt(name):
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100225 if name in ('.', MNCC):
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200226 return name
227 return '%s%s' % (mo_mt, name)
228
229 l = prepend_mo_mt(arrow.left)
230 r = arrow.right
231 if r == '.':
232 r = l
233 else:
234 r = prepend_mo_mt(r)
235
236 a = {'label': arrow.descr}
237 a.update(arrow.attrs)
238 attrs = self.attrs_str(a)
239
240 self.writeln('%s %s %s%s;'
241 % (l, self.arrow_txlate(arrow.arrow), r,
242 self.attrs_str(self.all_attrs(arrow.descr, arrow.attrs), prefix='\t')))
243
244 def separator(self, sep_str, descr, attrs):
245 self.writeln('%s%s%s%s;'
246 % (separator.separator,
247 self.attrs_str(self.all_attrs(descr, attrs), prefix='\t')))
248
249 def emptyline(self, emptyline):
250 self.write('\n' * emptyline.count);
251
252
253def ms_from_ran(ran_type_or_conn):
254 if ran_type_or_conn.startswith('UTRAN-Iu'):
255 return UE
256 if ran_type_or_conn.startswith('RANAP'):
257 return UE
258 if ran_type_or_conn.startswith('GERAN-A'):
259 return MS
260 if ran_type_or_conn.startswith('BSS'):
261 return MS
262 return MS_UE_UNKNOWN
263
264class Diagram:
265 def __init__(self):
266 self.root_attrs = {}
267 self.entities = []
268 self.lines = []
269 self.mo_mt_unknown_lines = []
270
271 def add_line(self, line):
272 self.lines.append(line)
273
274 def resolve_unknown_mo_mt(self):
275
276 def try_match(a, b):
277 if a < 0 or a >= len(self.lines):
278 return False
279 if b < 0 or b >= len(self.lines):
280 return False
281 la = self.lines[a]
282 lb = self.lines[b]
283
284 if not hasattr(lb, 'mo_mt'):
285 return False
286 if lb.mo_mt == MO_MT_UNKNOWN:
287 return False
288
289 for match_attr in ('imsi', 'tmsi', 'ran_conn'):
290 if not hasattr(la, match_attr):
291 continue
292 if not hasattr(lb, match_attr):
293 continue
294 la_attr = getattr(la, match_attr)
295 if la_attr is None:
296 continue
297 lb_attr = getattr(lb, match_attr)
298 if la_attr == lb_attr:
299 la.mo_mt = lb.mo_mt
300 return True
301 return False
302
303
304 while True:
305 changes = 0
306 for i in range(len(self.lines)):
307 line = self.lines[i]
308
309 if not hasattr(line, 'mo_mt'):
310 continue
311 if line.mo_mt is not MO_MT_UNKNOWN:
312 continue
313
314 # don't know MO/MT, try to resolve from near messages
315 for j in range(1,100):
316 if try_match(i, i-j):
317 break
318 if try_match(i, i+j):
319 break
320 if line.mo_mt is not MO_MT_UNKNOWN:
321 changes += 1
322 if not changes:
323 break
324
325
326re_source_file_last = re.compile(r'(.*) \(([^):]+:[0-9]+)\)$')
327
328class Rule:
329 def __init__(self, name, re_str, handler):
330 self.name = name
331 self.re = re.compile(re_str)
332 self.handler = handler
333
334 def match(self, line):
335 m = self.re.match(line)
336 if not m:
337 return False
338 return self.handler(m)
339
340
341def mo_mt_from_l3type(l3type):
342 if l3type == 'PAGING_RESP':
343 return MT
344 elif l3type == 'CM_SERVICE_REQ':
345 return MO
346 else:
347 return MO_MT_UNKNOWN
348
349def int_to_hex(val, bits):
350 return hex((int(val) + (1 << bits)) % (1 << bits))
351
352class Callref:
353 MAX = 0x7fffffff
354 MIN = -0x80000000
355 BITS = 32
356
357 def int_to_hex(val):
358 return int_to_hex(val, Callref.BITS)
359
360 def hex_to_int(hexstr):
361 val = int(hexstr, 16)
362 if val > Callref.MAX:
363 val = Callref.MIN + (val & Callref.MAX)
364 return val
365
366class Parse:
367
368 def __init__(self, output, mask_values=False):
369
370 self.diagram = Diagram()
371 self.output = output
372 self.linenr = 0
373 self.rules = []
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100374 self.rules_hit = {}
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200375 self.seen_udtrace_mncc = False
376
377 self.callrefs_mo_mt = {}
378 self.mask_values = mask_values
379 self.masked_values = {}
380
381 for member in dir(self):
382 if not member.startswith('rule_'):
383 continue
384 func = getattr(self, member)
385 if not callable(func):
386 continue
387
388 docstr = func.__doc__
389 if not docstr:
390 continue
391 re_str = docstr.splitlines()[0]
392
393 self.rules.append(Rule(name=member, re_str=re_str, handler=func))
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100394 self.rules_hit[member] = 0
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200395
396
397
398 def error(self, *msg):
399 error('line %d: ' % self.linenr, *msg)
400
401 def start(self):
402 self.diagram.root_attrs = {'hscale': '3'}
403 for name, descr in (
404 ('moms', 'MS,BSS (MO)\\nUE,hNodeB (MO)'),
405 #('moue', 'UE,hNodeB (MO)'),
406 ('momgw', 'MGW for MSC (MO)'),
407 ('momsc', 'MSC (MO)'),
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100408 ('mncc', 'MNCC'),
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200409 ('mtmsc', 'MSC (MT)'),
410 ('mtmgw', 'MGW for MSC (MT)'),
411 ('mtms', 'BSS,MS (MT)\\nhNodeB,UE (MT)'),
412 #('mtue', 'hNodeB,UE (MT)'),
413 ):
414 self.diagram.entities.append(Entity(name, descr))
415
416 def end(self):
417 self.diagram.resolve_unknown_mo_mt()
418 self.output.render(self.diagram)
419
420 def mask_value(self, name, val):
421 if not self.mask_values:
422 return val
423 if not val:
424 return val
425 name_dict = self.masked_values.get(name)
426 if not name_dict:
427 name_dict = {}
428 self.masked_values[name] = name_dict
429
430 masked_val = name_dict.get(val)
431 if masked_val is None:
432 masked_val = '%s-%d' % (name, len(name_dict) + 1)
433 name_dict[val] = masked_val
434 return masked_val
435
436 def add_line(self, line):
437 self.linenr += 1
438 if line.endswith('\n'):
439 line = line[:-1]
440 if line.endswith('\r'):
441 line = line[:-1]
442
443 self.interpret(line)
444
445 def interpret(self, line):
446
447 m = re_source_file_last.match(line)
448 if m:
449 line = m.group(1)
450
451 for rule in self.rules:
452 if rule.match(line):
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100453 self.rules_hit[rule.name] = self.rules_hit.get(rule.name, 0) + 1
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200454 break
455
456 RE_DTAP_NAME = re.compile('.*GSM48_MT_([^_]+)_(.+)')
457
458 def rule_paging(self, m):
459 r'.*ran_peer\(([^:]+):.* Paging for ([^ ]+) on ([^ ]+)'
460 ran_type, subscr, cell = m.groups()
461
462 self.diagram.add_line(Arrow(MT, ms_from_ran(ran_type), '<', MSC, 'Paging'))
463 return True
464
465 RE_IMSI = re.compile('IMSI-([0-9]+)')
466 RE_TMSI = re.compile('TMSI-0x([0-9a-fA-F]+)')
467
468 def rule_dtap(self, m):
469 r'.*msc_a\(([^)]*):([^:]+):([^:]+)\).* (Dispatching 04.08 message|Sending DTAP): (.+)$'
470
471 subscr, ran_conn, l3type, rx_tx, dtap = m.groups()
472 tx = (rx_tx == 'Sending DTAP')
473
474 m = self.RE_DTAP_NAME.match(dtap)
475 if m:
476 dtap = '%s %s' % m.groups()
477
478 if 'IMSI_DETACH_IND' in dtap:
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100479 # detecting IMSI Detach separately, because this log line does not contain the IMSI.
480 # By using the rule_imsi_detach(), we can accurately put it on the MO/MT side.
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200481 return True
482
483 if l3type == 'NONE' and not tx and dtap.endswith('PAG_RESP'):
484 e = MT
485 else:
486 e = mo_mt_from_l3type(l3type)
487
488 imsi = None
489 for m in Parse.RE_IMSI.finditer(subscr):
490 imsi = m.group(1)
491 tmsi = None
492 for m in Parse.RE_TMSI.finditer(subscr):
493 tmsi = m.group(1)
494
495 self.diagram.add_line(Arrow(e, ms_from_ran(ran_conn), '<' if tx else '>', MSC, dtap,
496 ran_conn=ran_conn, imsi=imsi, tmsi=tmsi))
497 return True
498
499 def rule_imsi_detach(self, m):
500 r'.*IMSI DETACH for IMSI-([0-9]+):.*'
501 imsi = m.group(1)
502 e = MO_MT_UNKNOWN
503 self.diagram.add_line(Arrow(e, MS_UE_UNKNOWN, '>', MSC, 'IMSI Detach', imsi=imsi))
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100504 return True
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200505
506 def rule_mgcp_tx(self, m):
507 r'.*mgw-endp\([^)]*:([^:]+):([^:]+)\).* (rtpbridge[^ ]+) .* RTP_TO_(RAN|CN)( CI=([^:]+)|): ([^ :]+).*: Sending'
508 ran, l3type, endp, rtp_to, cond_ci, ci, verb = m.groups()
509 e = mo_mt_from_l3type(l3type)
510 if '*' not in endp:
511 endp = self.mask_value('EP', endp)
512 ci = self.mask_value('CI', ci)
513 ci_str = ''
514 if ci:
515 ci_str = ' %s' % ci
516 self.diagram.add_line(Arrow(e, MGW, '<', MSC, 'for %s: %s\\n%s%s' % (rtp_to, verb, endp, ci_str)))
517 return True
518
519 def rule_mgcp_rx(self, m):
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100520 r'.*mgw-endp\(([^)]+):([^:)]+):([^:)]+)\).* (rtpbridge[^ ]+) .* RTP_TO_(RAN|CN)( CI=([^:]+)|).*: received successful response to ([^:]+): RTP=[^:]+:([0-9.:]+)'
521 subscr, ran_conn, l3type, endp, rtp_to, cond_ci, ci, verb, rtp = m.groups()
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200522 e = mo_mt_from_l3type(l3type)
523 endp = self.mask_value('EP', endp)
524 ci = self.mask_value('CI', ci)
525 ci_str = ''
526 if ci:
527 ci_str = ' %s' % ci
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100528 rtp = self.mask_value('IP:port', rtp)
529 self.diagram.add_line(Arrow(e, MGW, '>', MSC, 'for %s: %s OK\\n%s%s %s' % (rtp_to, verb, endp, ci_str, rtp)))
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200530 return True
531
532 def rule_ran_tx(self, m):
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100533 r'.*msc_a\(([^)]+):([^:)]+):([^:)]+)\).* RAN encode: ([^: ]+): (.+)$'
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200534
535 subscr, ran_conn, l3type, ran_type, msg_type = m.groups()
536
537 if msg_type in ('DTAP', 'DirectTransfer'):
538 # will get DTAP details from rule_dtap() instead, not from BSSMAP logging
539 return True
540 if msg_type.startswith('Tx'):
541 # skip 'Tx RANAP SECURITY MODE COMMAND to RNC, ik 47...'
542 return True
543 if '=' in msg_type:
544 # skip 'RAB Assignment: rab_id=1, rtp=192.168.178.66:50008, use_x213_nsap=1'
545 return True
546
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200547
548 e = mo_mt_from_l3type(l3type)
549
550 imsi = None
551 for m in Parse.RE_IMSI.finditer(subscr):
552 imsi = m.group(1)
553 tmsi = None
554 for m in Parse.RE_TMSI.finditer(subscr):
555 tmsi = m.group(1)
556
557 self.diagram.add_line(Arrow(e, ms_from_ran(ran_conn), '<', MSC, '(%s) %s' % (ran_type, msg_type),
558 ran_conn=ran_conn, imsi=imsi, tmsi=tmsi))
559 return True
560
561 def rule_ran_rx(self, m):
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100562 r'.*msc_a\(([^)]+):([^:)]+):([^:)]+)\).* RAN decode: ([^: ]+) (.+)$'
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200563
564 subscr, ran_conn, l3type, ran_type, msg_type = m.groups()
565
566 if msg_type in ('DTAP', 'DirectTransfer', 'DirectTransfer RAN PDU'):
567 # will get DTAP details from rule_dtap() instead, not from BSSMAP logging
568 return True
569
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200570
571 e = mo_mt_from_l3type(l3type)
572
573 imsi = None
574 for m in Parse.RE_IMSI.finditer(subscr):
575 imsi = m.group(1)
576 tmsi = None
577 for m in Parse.RE_TMSI.finditer(subscr):
578 tmsi = m.group(1)
579
580 self.diagram.add_line(Arrow(e, ms_from_ran(ran_type), '>', MSC, '(%s) %s' % (ran_type, msg_type),
581 ran_conn=ran_conn, imsi=imsi, tmsi=tmsi))
582 return True
583
584 def rule_cc_state(self, m):
585 r'.*trans\(CC[^) ]* [^ )]+:([^:)]+) callref-([^ ]+) [^)]+\) new state ([^ ]+) -> ([^ ]+)'
586 l3type, callref_hex, from_state, to_state = m.groups()
587
588 e = mo_mt_from_l3type(l3type)
589 self.callrefs_mo_mt[callref_hex] = e
590
591 self.diagram.add_line(Arrow(e, MSC, '<>', '.', 'CC state:\\n%s' % to_state))
592 return True
593
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100594 def rule_log_mncc_no_rtp(self, m):
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200595 r'.*trans\(CC[^) ]* [^ )]+:([^:)]+) callref-([^ ]+) [^)]+\) (tx|rx) (MNCC_[^ ]*)$'
596 l3type, callref_hex, tx_rx, mncc_msg = m.groups()
597
598 if self.seen_udtrace_mncc:
599 # If no udtrace is present, take the MNCC logging.
600 # But if there is udtrace logging available, we should not duplicate those MNCC lines.
601 return True
602
603 tx = (tx_rx == 'tx')
604
605 try:
606 e = self.callrefs_mo_mt.get(callref_hex, MT)
607 except:
608 e = MT
609
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100610 self.diagram.add_line(Arrow(e, MSC, '>' if tx else '<', 'mncc', mncc_msg))
611 return True
612
613 def rule_log_mncc_with_rtp(self, m):
614 r'.*trans\(CC[^) ]* [^ )]+:([^:)]+) callref-([^ ]+) [^)]+\) (tx|rx) (MNCC_[^ ]*) \(RTP=([^){]+)(|{.*})\)$'
615 l3type, callref_hex, tx_rx, mncc_msg, rtp, codec = m.groups()
616
617 if self.seen_udtrace_mncc:
618 # If no udtrace is present, take the MNCC logging.
619 # But if there is udtrace logging available, we should not duplicate those MNCC lines.
620 return True
621
622 tx = (tx_rx == 'tx')
623
624 try:
625 e = self.callrefs_mo_mt.get(callref_hex, MT)
626 except:
627 e = MT
628
629 rtp = self.mask_value('IP:port', rtp)
630 self.diagram.add_line(Arrow(e, MSC, '>' if tx else '<', 'mncc', f'{mncc_msg}\\n{rtp}'))
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200631 return True
632
633 RE_MNCC_RTP = re.compile(' ip := ([^, ]+), rtp_port := ([0-9]+),')
634 RE_MNCC_CALLREF = re.compile(' callref := ([^ ,]+), ')
635
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100636 # detecting MNCC with udtrace has the advantage that we also get an indication whether RTP information is
637 # present
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200638 def rule_udtrace_mncc(self, m):
639 r'.*(write|recv).* (Tx|Rx): \{ msg_type := ([^ ]+) .* u := \{ (.*) \} \}$'
640 write_recv, tx_rx, msg_type, u = m.groups()
641
642 self.seen_udtrace_mncc = True
643
644 tx = (tx_rx == 'Tx')
645
646 callref_intstr = None
647 for m in Parse.RE_MNCC_CALLREF.finditer(u):
648 callref_intstr = m.group(1)
649 if not callref_intstr:
650 # log only MNCC that has a callref
651 return True
652
653 try:
654 e = self.callrefs_mo_mt.get(Callref.int_to_hex(callref_intstr), MT)
655 except:
656 e = MT
657
658 descr = msg_type
659
660 for m in Parse.RE_MNCC_RTP.finditer(u):
661 ip_str, port_str = m.groups()
662 try:
663 if int(ip_str) == 0 or int(port_str) == 0:
664 break
665 except:
666 break
667 ip_hex = int_to_hex(ip_str, 32)
668 ip = []
669 ip_val = int(ip_hex, 16)
670 for byte in range(4):
671 ip.insert(0, (ip_val & (0xff << (8*byte))) >> (8*byte))
672 rtp_info = '%s:%s' % ('.'.join(str(b) for b in ip), port_str)
673 rtp_info = self.mask_value('IP:port', rtp_info)
674 descr = '%s\\n%s' % (descr, rtp_info)
675 break
676
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100677 self.diagram.add_line(Arrow(e, MSC, '>' if tx else '<', 'mncc', descr))
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200678 return True
679
680 def rule_cc_timer(self, m):
681 r'.*trans\(CC.*IMSI-([0-9]+):.*\) (starting|stopping pending) (guard timer|timer T[0-9]+)( with ([0-9]+) seconds|)'
682 imsi, start_stop, t, with_cond, s = m.groups()
683 start = (start_stop == 'starting')
684 e = MO_MT_UNKNOWN
685 if start:
686 self.diagram.add_line(Arrow(e, MSC, '[]', '.', 'CC starts %s (%ss)' % (t, s), imsi=imsi))
687 else:
688 self.diagram.add_line(Arrow(e, MSC, '[]', '.', 'CC stops %s' % (t), imsi=imsi))
689 return True
690
691
692def translate(inf, outf, cmdline):
693 if cmdline.output_format == 'mscgen':
694 output = OutputMscgen(outf, cmdline.start_with_re)
695 else:
696 output = OutputLadder(outf, cmdline.start_with_re)
697 parse = Parse(output, cmdline.mask_values)
698
699 parse.start()
700
701 while inf.readable():
702 line = inf.readline()
703 if not line:
704 break;
705 parse.add_line(line)
706 parse.end()
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100707 if cmdline.verbose:
708 for name, count in parse.rules_hit.items():
709 print(f" {name}: {count}")
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200710
711def open_output(inf, cmdline):
712 if cmdline.output_file == '-':
713 translate(inf, sys.stdout, cmdline)
714 else:
715 with tempfile.NamedTemporaryFile(dir=os.path.dirname(cmdline.output_file), mode='w', encoding='utf-8') as tmp_out:
716 translate(inf, tmp_out, cmdline)
717 if os.path.exists(cmdline.output_file):
718 os.unlink(cmdline.output_file)
719 os.link(tmp_out.name, cmdline.output_file)
720
721def open_input(cmdline):
722 if cmdline.input_file == '-':
723 open_output(sys.stdin, cmdline)
724 else:
725 with open(cmdline.input_file, 'r') as f:
726 open_output(f, cmdline)
727
728def main(cmdline):
729 open_input(cmdline)
730
731
732if __name__ == '__main__':
733 parser = argparse.ArgumentParser(description=doc)
734 parser.add_argument('-i', '--input-file', dest='input_file', default="-",
735 help='Read from this file, or stdin if "-"')
736 parser.add_argument('-o', '--output-file', dest='output_file', default="-",
737 help='Write to this file, or stdout if "-"')
738 parser.add_argument('-t', '--output-format', dest='output_format', default="mscgen",
739 choices=('mscgen','ladder'),
740 help='Pick output format: mscgen format or libosmocore/contrib/ladder_to_msc.py format')
741 parser.add_argument('-m', '--mask-values', dest='mask_values', action='store_true',
742 help='Do not output specific values like IP address, port, endpoint CI, instead just indicate that a value is'
Martin Hauke3f07dac2019-11-14 17:49:08 +0100743 ' present. This makes the output reproducible across various logs.')
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200744 parser.add_argument('-s', '--start-with', dest='start_with_re', default=None,
745 help='Skip until the first message with this label (regex), e.g. -s "CC SETUP"')
Neels Hofmeyr075ca602023-03-08 00:53:47 +0100746 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
747 help='show some debug info, like which regex rules were hit and which were not.')
Neels Hofmeyr0f6664b2019-10-08 06:38:02 +0200748
749 cmdline = parser.parse_args()
750
751 main(cmdline)
752
753# vim: shiftwidth=8 noexpandtab tabstop=8 autoindent nocindent