forked from mbakholdina/srt-packet-reordering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_reordering.py
664 lines (591 loc) · 20 KB
/
packet_reordering.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import datetime as dt
import logging
import time
import typing
import click
import pandas as pd
import srt_utils.process as process
logger = logging.getLogger(__name__)
PAYLOAD_SIZE = 1316
MAXIMUM_SEQUENCE_NUMBER = 2 ** 32
def _nodes_split(ctx, param, value):
return list(value)
def generate_payload():
"""
Generate payload of PAYLOAD_SIZE size of the following type:
|<------------------- Payload Size ------------------------>|
+---+---+---+---+---+---+---+---+---+---+---+---+---+ +---+
| 1 | 2 | 3 |...|255| 1 | 2 | 3 |...|255|...|...|...|...| 0 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+ +---+
|
0 byte at the end indicates the end of payload__/
"""
return bytearray([(1 + i % 255) for i in range(0, PAYLOAD_SIZE - 1)]) + bytearray([0])
def insert_srcByte(payload, s):
"""
Insert SrcByte, SrcTime in packet payload of type
|<------------------- Payload Size ------------------------>|
|<-- SrcByte -->|<-- SrcTime -->| |
| 4 bytes | 4 bytes | |
+---+---+---+---+---+---+---+---+---+---+---+---+---+ +---+
| x | x | x | x | x | x | x | x | 9 |10 |...|...|...|...| 0 |
+---+---+---+---+---+---+---+---+---+---+---+---+---+ +---+
|
0 byte at the end indicates the end of payload__/
where
SrcByte -- Packet Sequence Number applied at the source,
in units of payload bytes,
SrcTime -- the time of packet emission from the source,
in units of payload bytes (not yet implemented).
Attributes:
payload:
Packet payload,
s:
the unique packet sequence number applied at the source,
in units of messages.
"""
payload[0] = s >> 24
payload[1] = (s >> 16) & 255
payload[2] = (s >> 8) & 255
payload[3] = (s >> 0) & 255
return payload
def calculate_interval(bitrate):
"""
Calculate interval between sending consecutive packets depending on
desired bitrate, in seconds with microseconds accuracy.
Attributes:
bitrate:
Bitrate, Mbit/s.
"""
if bitrate is None:
# Corresponds to 1.05 Mbit/s
return 0.01
else:
return round((PAYLOAD_SIZE * 8) / (bitrate * 1000000), 6)
def calculate_target_time(interval_us):
# Known issue to consider
# https://stackoverflow.com/questions/12448592/how-to-add-delta-to-python-datetime-time
now = dt.datetime.now().time()
delta = dt.timedelta(microseconds = interval_us)
target_time = (dt.datetime.combine(dt.date(1,1,1),now) + delta).time()
return target_time
def print_list(elements):
for element in elements:
print(element)
def start_sender(args, interval_s, k):
"""
Start sender (either srt-live-transmit or srt-test-live application) with
arguments `args` in order to generate and send `k` packets with `interval_s`
interval between consecutive packets.
generate packet --> stdin --> SRT
Examples for debugging purposes as per Section 7 of
https://tools.ietf.org/html/rfc4737#section-7
1. Example with a single packet reordered
sending_order_1 = [1, 2, 3, 5, 6, 7, 8, 4, 9, 10]
2. Example with two packets reordered
sending_order_2 = [1, 2, 3, 4, 7, 5, 6, 8, 9, 10]
3. Example with three packets reordered
sending_order_3 = [1, 2, 3, 7, 8, 9, 10, 4, 5, 6, 11]
4. Example with a single packet reordered and two duplicate packets
sending_order_1_dup = [1, 2, 3, 5, 6, 7, 8, 4, 9, 10, 10, 6]
"""
if k > MAXIMUM_SEQUENCE_NUMBER:
logger.error('The number of packets exceeds the maximum possible packet sequence number')
logger.info('Starting sender')
proc = process.Process(args)
proc.start()
# Sleep for 1s in order to give some time for sender and receiver
# to establish the connection
time.sleep(1)
payload = generate_payload()
interval_us = int(interval_s * 1000000)
assert 0 <= interval_us < 1000000
try:
for s in range(1, k + 1):
target_time = calculate_target_time(interval_us)
logger.debug(f'Sending packet {s}')
payload_srcByte = insert_srcByte(payload, s)
proc.process.stdin.write(payload_srcByte)
proc.process.stdin.flush()
while (dt.datetime.now().time() < target_time):
pass
except KeyboardInterrupt:
logger.info('KeyboardInterrupt has been caught. Cleaning up ...')
except Exception as e:
logger.error(e)
finally:
# Sleep for 1s in order to give some time for sender to deliver
# the remain portion of packets at the end of experiment
time.sleep(1)
logger.info('Stopping sender')
proc.stop()
logger.info('Collecting sender stdout, stderr')
stdout, stderr = proc.collect_results()
print('\nstdout:')
print_list(stdout)
print('\nstderr:')
print_list(stderr)
print('\n')
def read_data(proc, interval_s):
"""
Read data of `PAYLOAD_SIZE` size from stdout of a process `proc`.
There are three possible cases:
- no data in stdout when the transmission has not been started yet
or has been finished already,
- there is data, however it's b'', the reason is a possible bug in test
application,
- there is a packet received.
"""
while True:
# If there is no data in stdout, the code will hang here
data = proc.process.stdout.read(PAYLOAD_SIZE)
if len(data) != 0:
break
time.sleep(interval_s)
return data
def type_p_reordered_ratio_stream(df: pd.DataFrame):
"""
Type-P-Reordered-Ratio-Stream metric as per Section 4.1 of
https://tools.ietf.org/html/rfc4737#section-4.1
The ratio of reordered packets to received packets.
R = (Count of packets with Type-P-Reordered=TRUE) / (L) * 100,
where L is the total number of packets received out of K packets sent.
Recall that identical copies (duplicates) have been removed, so L <= K.
Attributes:
df:
Dataframe with received packets information. Duplications
should be preliminarily removed.
Returns a tuple of (packets reordered, Type-P-Reordered-Ratio-Stream metric).
"""
assert df['s@Dst'].is_unique
packets_reordered = df['Type-P-Reordered'].sum()
packets_reordered_metric = round(packets_reordered / len(df.index) * 100, 4)
return (packets_reordered, packets_reordered_metric)
def sequence_discontinuities(df: pd.DataFrame):
"""
Calculates the number of sequence discontinuities and their
total size in packets as per Section 3.4 of
https://tools.ietf.org/html/rfc4737#section-3.4
Recall that identical copies (duplicates) have been removed.
"""
assert df['s@Dst'].is_unique
return (df['Seq Disc'].sum(), df['Seq Disc Size'].sum())
def calculate_print_metrics(df: pd.DataFrame, k: int):
"""
Calculates different metrics based on the received packets info
and prints the results in terminal.
Attributes:
df:
`pd.DataFrame` with information regarding received packets
(containing possible duplicates).
k:
Number of packets generated and sent by receiver.
"""
df_duplicates = df
packets_received = len(df.index)
# Remove duplicates from df
# l does not include duplicated packets,
# k is the number of packets sent by receiver
df.drop_duplicates(subset ='s@Dst', keep = 'first', inplace = True)
l = len(df.index)
assert l <= k
duplicates = packets_received - l
duplicates_ratio = round(duplicates * 100 / packets_received, 4)
seq_discontinuities, total_size = sequence_discontinuities(df)
# This value can be also calculated as the difference between total
# sequence discontinuities size minus packets reordered, see below
packets_lost = k - l
packets_lost_ratio = round(packets_lost * 100 / k, 4)
packets_reordered, packets_reordered_ratio = type_p_reordered_ratio_stream(df)
packets_lost_2 = total_size - packets_reordered
packets_lost_2_ratio = round(packets_lost_2 * 100 / k, 4)
# data_1 = [
# ('Packets Received', packets_received, ),
# ('Duplicates', duplicates, duplicates_ratio),
# ('Packets Reordered', packets_reordered, packets_reordered_ratio),
# ('Packets Lost', packets_lost, packets_lost_ratio)
# ]
# df_stats_1 = pd.DataFrame(data_1, columns = ['Metric', 'Number, packet(s)', 'Ratio, %'])
# data_2 = [('Sequence Discontinuities', seq_discontinuities, total_size)]
# df_stats_2 = pd.DataFrame(data_2, columns = ['Metric', 'Number', 'Total Size, packet(s)'])
print(f'Packets Generated (by Sender): {k}')
print(f'Packets Received: {packets_received}')
print(f'Duplicates: {duplicates}')
print(f'Packets Reordered: {packets_reordered}')
print(f'Sequence Discontinuities: {seq_discontinuities}, Total Size: {total_size} packet(s)')
print(f'Packets Lost (Generated - Received - Duplicates): {packets_lost}')
print(f'Packets Lost (Total Size of Sequence Discontinuities - Reordered): {packets_lost_2}')
print(f'Duplicates Ratio: {duplicates_ratio} %')
print(f'Reordered Packets Ratio: {packets_reordered_ratio} %')
print(f'Lost Packets Ratio (Generated - Received - Duplicates): {packets_lost_ratio} %')
print(f'Lost Packets Ratio (Total Size of Sequence Discontinuities - Reordered): {packets_lost_2_ratio} %')
print('\n')
logger.info(
'Writing results to a set of .csv files: packets_duplicates.csv, '
'packets_no_duplicates.csv'
)
df_duplicates.to_csv('packets_duplicates.csv')
df.to_csv('packets_no_duplicates.csv')
logger.info('Writing to .csv is finished')
def start_receiver(args, interval_s, k):
"""
Start receiver (either srt-live-transmit or srt-test-live application) with
arguments `args` in order to receive `k` packets that have been sent by
a sender with `interval_s` interval between consecutive packets and analyze
received data knowing the algorithm of packets generation at a sender side.
SRT --> stdout --> analyze received packets
"""
if k > MAXIMUM_SEQUENCE_NUMBER:
logger.error('The number of packets exceeds the maximum possible packet sequence number')
logger.info('Starting receiver')
proc = process.Process(args)
proc.start()
logger.info(
'Please start a sender with 1) the same value of n or duration '
'and bitrate, 2) the same attributes ...'
)
payload = generate_payload()
# NextExp -- the next expected sequence number at the destination,
# in units of messages. The stored value in NextExp is determined
# from a previously arriving packet.
# next_exp = 0
next_exp = 1
# List of dictionaries for storing received packets info
dicts = []
try:
# NOTE: On one hand, the number of actually arrived packets can be less then
# the number of sent packets k because of losses; on the other hand,
# duplicates can make it greater than k. As of now, we will stop the experiment
# once k packets are received, however some percentage of k can be introduced
# for checking the possibility of receiving duplicate packets.
for i in range(1, k + 1):
received_packet = read_data(proc, interval_s)
src_byte = received_packet[:4]
s = int.from_bytes(src_byte, byteorder='big')
logger.debug(f'Received packet {s}')
src_byte = src_byte.hex()
previous_next_exp = next_exp
if s >= next_exp:
# If s >= next_exp, packet s is in-order. In this case, next_exp
# is set to s+1 for comparison with the next packet to arrive.
if s > next_exp:
# Some packets in the original sequence have not yet arrived,
# and there is a sequence discontinuity assotiated with packet s.
# The size of this discontinuty is s-next_exp, equal to the
# number of packets presently missing, either reordered or lost.
seq_discontinuty = True
seq_discontinuty_size = s - next_exp
else:
# When s = next_exp, the original sequence has been maintained,
# and there is no discontinuty present.
seq_discontinuty = False
next_exp = s + 1
type_p_reordered = False
else:
# When s < next_exp, the packet is reordered. In this case the
# next_exp value does not change.
type_p_reordered = True
seq_discontinuty = False
if not seq_discontinuty:
seq_discontinuty_size = 0
dicts += [{
's@Dst': s,
'NextExp': previous_next_exp,
'SrcByte (hex)': src_byte,
'Dst Order': i,
'Type-P-Reordered': type_p_reordered,
'Seq Disc': seq_discontinuty,
'Seq Disc Size': seq_discontinuty_size,
}]
except KeyboardInterrupt:
logger.info('KeyboardInterrupt has been caught. Cleaning up ...')
finally:
logger.info('Stopping receiver')
proc.stop()
logger.info('Collecting receiver stdout, stderr')
stdout, stderr = proc.collect_results()
print('\nstdout:')
print_list(stdout)
print('\nstderr:')
print_list(stderr)
print('\n')
if len(dicts) == 0:
logger.info('No packets received')
return
logger.info('Experiment results: \n')
df = pd.DataFrame(dicts)
calculate_print_metrics(df, k)
@click.group()
@click.option(
'--debug/--no-debug',
default=False,
help='Activate DEBUG level of script logs'
)
def cli(debug):
if debug:
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)-15s [%(levelname)s] %(message)s',
)
else:
logging.basicConfig(
level=logging.INFO,
format='%(asctime)-15s [%(levelname)s] %(message)s',
)
@cli.command()
@click.option(
'--ip',
default='127.0.0.1',
help='IP to call',
show_default=True
)
@click.option(
'--port',
default=4200,
help='Port to call',
show_default=True
)
@click.option(
'--duration',
default=60,
help='Duration, s',
show_default=True
)
@click.option(
'--n',
help='Number of packets',
type=int
)
@click.option(
'--bitrate',
help='Bitrate, Mbit/s',
type=float
)
@click.option(
'--attrs',
help='SRT attributes to pass within query. Format: "key1=value1&key2=value2"'
)
@click.argument(
'path',
type=click.Path(exists=True)
)
def sender(ip, port, duration, n, bitrate, attrs, path):
srt_str = f'srt://{ip}:{port}?nakreport=0&linger=0'
if attrs:
srt_str += f'&{attrs}'
args = [
f'{path}',
'file://con',
srt_str,
# '-v',
# '-loglevel:error'
]
interval = calculate_interval(bitrate)
if n is None:
n = int(duration // interval) + 1
logger.info(f'interval: {interval}, n: {n}')
logger.info(f'args: {args}')
start_sender(args, interval, n)
@cli.command()
@click.option(
'--port',
default=4200,
help='Port to listen',
show_default=True
)
@click.option(
'--duration',
default=60,
help='Duration, s',
show_default=True
)
@click.option(
'--n',
help='Number of packets',
type=int
)
@click.option(
'--bitrate',
help='Bitrate, Mbit/s',
type=float
)
@click.option(
'--attrs',
help='SRT attributes to pass within query. Format: "key1=value1&key2=value2"'
)
@click.argument(
'path',
type=click.Path(exists=True)
)
def receiver(port, duration, n, bitrate, attrs, path):
srt_str = f'srt://:{port}?nakreport=0&linger=0'
if attrs:
srt_str += f'&{attrs}'
args = [
f'{path}',
srt_str,
'file://con',
# '-v',
# '-loglevel:error'
]
interval = calculate_interval(bitrate)
if n is None:
n = int(duration // interval) + 1
logger.info(f'interval: {interval}, n: {n}')
logger.info(f'args: {args}')
start_receiver(args, interval, n)
@cli.command()
@click.option(
'--node',
help='host:port combination, multiple nodes can be defined',
required=True,
multiple=True,
callback=_nodes_split
)
@click.option(
'--duration',
default=60,
help='Duration, s',
show_default=True
)
@click.option(
'--n',
help='Number of packets',
type=int
)
@click.option(
'--bitrate',
help='Bitrate, Mbit/s',
type=float
)
@click.option(
'--attrs',
help='SRT attributes to pass within query. Format: "key1=value1&key2=value2"'
)
@click.option(
'--ll',
type=click.Choice(['fatal', 'error', 'note', 'warning', 'debug']),
default='debug',
help='Minimum severity for logs',
show_default=True
)
@click.option(
'--lfa',
help='Enabled functional areas for logs, multiple areas can be defined',
multiple=True
)
@click.option(
'--lf',
type=click.Path(),
help='File to send logs to'
)
@click.argument(
'path',
type=click.Path(exists=True)
)
def re_sender(node, duration, n, bitrate, attrs, ll, lfa, lf, path):
# sender, caller
# ../srt/srt-ethouris/_build/srt-test-live file://con -g srt://*?type=redundancy 127.0.0.1:4200
# TODO: type=redundancy has changed to type=broadcast, test this properly once the URL format for
# srt-test-live application is fixed
srt_str = f'srt://*'
if attrs:
srt_str += f'?{attrs}'
args = [
f'{path}',
'file://con',
'-g',
srt_str,
]
args += node
if lf:
args += [
'-ll', ll,
'-lf', lf,
]
if lfa:
args += ['-lfa']
args += lfa
interval = calculate_interval(bitrate)
if n is None:
n = int(duration // interval) + 1
logger.info(f'interval: {interval}, n: {n}')
logger.info(f'args: {args}')
start_sender(args, interval, n)
@cli.command()
@click.option(
'--port',
default=4200,
help='Port to listen',
show_default=True
)
@click.option(
'--duration',
default=60,
help='Duration, s',
show_default=True
)
@click.option(
'--n',
help='Number of packets',
type=int
)
@click.option(
'--bitrate',
help='Bitrate, Mbit/s',
type=float
)
@click.option(
'--attrs',
help='SRT attributes to pass within query. Format: "key1=value1&key2=value2"'
)
@click.option(
'--ll',
type=click.Choice(['fatal', 'error', 'note', 'warning', 'debug']),
default='debug',
help='Minimum severity for logs',
show_default=True
)
@click.option(
'--lfa',
help='Enabled functional areas for logs, multiple areas can be defined',
multiple=True
)
@click.option(
'--lf',
type=click.Path(),
help='File to send logs to'
)
@click.argument(
'path',
type=click.Path(exists=True)
)
def re_receiver(port, duration, n, bitrate, attrs, ll, lfa, lf, path):
# receiver, listener
# ../srt/srt-ethouris/_build/srt-test-live srt://:4200?groupconnect=true file://con
# TODO: groupconnect=true changed to groupconnect=1, test this additionally once
# the URL format for srt-test-live application is fixed
srt_str = f'srt://:{port}'
if attrs:
srt_str += f'?{attrs}'
args = [
f'{path}',
srt_str,
'file://con',
]
if lf:
args += [
'-ll', ll,
'-lf', lf,
]
if lfa:
args += ['-lfa']
args += lfa
interval = calculate_interval(bitrate)
if n is None:
n = int(duration // interval) + 1
logger.info(f'interval: {interval}, n: {n}')
logger.info(f'args: {args}')
start_receiver(args, interval, n)
if __name__ == '__main__':
cli()