forked from DPDK/dpdk
/
rte_eth_bond_pmd.c
3772 lines (3166 loc) · 103 KB
/
rte_eth_bond_pmd.c
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2017 Intel Corporation
*/
#include <stdlib.h>
#include <stdbool.h>
#include <netinet/in.h>
#include <rte_mbuf.h>
#include <rte_malloc.h>
#include <rte_ethdev_driver.h>
#include <rte_ethdev_vdev.h>
#include <rte_tcp.h>
#include <rte_udp.h>
#include <rte_ip.h>
#include <rte_ip_frag.h>
#include <rte_devargs.h>
#include <rte_kvargs.h>
#include <rte_bus_vdev.h>
#include <rte_alarm.h>
#include <rte_cycles.h>
#include <rte_string_fns.h>
#include "rte_eth_bond.h"
#include "eth_bond_private.h"
#include "eth_bond_8023ad_private.h"
#define REORDER_PERIOD_MS 10
#define DEFAULT_POLLING_INTERVAL_10_MS (10)
#define BOND_MAX_MAC_ADDRS 16
#define HASH_L4_PORTS(h) ((h)->src_port ^ (h)->dst_port)
/* Table for statistics in mode 5 TLB */
static uint64_t tlb_last_obytets[RTE_MAX_ETHPORTS];
static inline size_t
get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto)
{
size_t vlan_offset = 0;
if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto ||
rte_cpu_to_be_16(RTE_ETHER_TYPE_QINQ) == *proto) {
struct rte_vlan_hdr *vlan_hdr =
(struct rte_vlan_hdr *)(eth_hdr + 1);
vlan_offset = sizeof(struct rte_vlan_hdr);
*proto = vlan_hdr->eth_proto;
if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
vlan_hdr = vlan_hdr + 1;
*proto = vlan_hdr->eth_proto;
vlan_offset += sizeof(struct rte_vlan_hdr);
}
}
return vlan_offset;
}
static uint16_t
bond_ethdev_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
{
struct bond_dev_private *internals;
uint16_t num_rx_total = 0;
uint16_t slave_count;
uint16_t active_slave;
int i;
/* Cast to structure, containing bonded device's port id and queue id */
struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
internals = bd_rx_q->dev_private;
slave_count = internals->active_slave_count;
active_slave = bd_rx_q->active_slave;
for (i = 0; i < slave_count && nb_pkts; i++) {
uint16_t num_rx_slave;
/* Offset of pointer to *bufs increases as packets are received
* from other slaves */
num_rx_slave =
rte_eth_rx_burst(internals->active_slaves[active_slave],
bd_rx_q->queue_id,
bufs + num_rx_total, nb_pkts);
num_rx_total += num_rx_slave;
nb_pkts -= num_rx_slave;
if (++active_slave == slave_count)
active_slave = 0;
}
if (++bd_rx_q->active_slave >= slave_count)
bd_rx_q->active_slave = 0;
return num_rx_total;
}
static uint16_t
bond_ethdev_rx_burst_active_backup(void *queue, struct rte_mbuf **bufs,
uint16_t nb_pkts)
{
struct bond_dev_private *internals;
/* Cast to structure, containing bonded device's port id and queue id */
struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
internals = bd_rx_q->dev_private;
return rte_eth_rx_burst(internals->current_primary_port,
bd_rx_q->queue_id, bufs, nb_pkts);
}
static inline uint8_t
is_lacp_packets(uint16_t ethertype, uint8_t subtype, struct rte_mbuf *mbuf)
{
const uint16_t ether_type_slow_be =
rte_be_to_cpu_16(RTE_ETHER_TYPE_SLOW);
return !((mbuf->ol_flags & PKT_RX_VLAN) ? mbuf->vlan_tci : 0) &&
(ethertype == ether_type_slow_be &&
(subtype == SLOW_SUBTYPE_MARKER || subtype == SLOW_SUBTYPE_LACP));
}
/*****************************************************************************
* Flow director's setup for mode 4 optimization
*/
static struct rte_flow_item_eth flow_item_eth_type_8023ad = {
.dst.addr_bytes = { 0 },
.src.addr_bytes = { 0 },
.type = RTE_BE16(RTE_ETHER_TYPE_SLOW),
};
static struct rte_flow_item_eth flow_item_eth_mask_type_8023ad = {
.dst.addr_bytes = { 0 },
.src.addr_bytes = { 0 },
.type = 0xFFFF,
};
static struct rte_flow_item flow_item_8023ad[] = {
{
.type = RTE_FLOW_ITEM_TYPE_ETH,
.spec = &flow_item_eth_type_8023ad,
.last = NULL,
.mask = &flow_item_eth_mask_type_8023ad,
},
{
.type = RTE_FLOW_ITEM_TYPE_END,
.spec = NULL,
.last = NULL,
.mask = NULL,
}
};
const struct rte_flow_attr flow_attr_8023ad = {
.group = 0,
.priority = 0,
.ingress = 1,
.egress = 0,
.reserved = 0,
};
int
bond_ethdev_8023ad_flow_verify(struct rte_eth_dev *bond_dev,
uint16_t slave_port) {
struct rte_eth_dev_info slave_info;
struct rte_flow_error error;
struct bond_dev_private *internals = bond_dev->data->dev_private;
const struct rte_flow_action_queue lacp_queue_conf = {
.index = 0,
};
const struct rte_flow_action actions[] = {
{
.type = RTE_FLOW_ACTION_TYPE_QUEUE,
.conf = &lacp_queue_conf
},
{
.type = RTE_FLOW_ACTION_TYPE_END,
}
};
int ret = rte_flow_validate(slave_port, &flow_attr_8023ad,
flow_item_8023ad, actions, &error);
if (ret < 0) {
RTE_BOND_LOG(ERR, "%s: %s (slave_port=%d queue_id=%d)",
__func__, error.message, slave_port,
internals->mode4.dedicated_queues.rx_qid);
return -1;
}
ret = rte_eth_dev_info_get(slave_port, &slave_info);
if (ret != 0) {
RTE_BOND_LOG(ERR,
"%s: Error during getting device (port %u) info: %s\n",
__func__, slave_port, strerror(-ret));
return ret;
}
if (slave_info.max_rx_queues < bond_dev->data->nb_rx_queues ||
slave_info.max_tx_queues < bond_dev->data->nb_tx_queues) {
RTE_BOND_LOG(ERR,
"%s: Slave %d capabilities doesn't allow to allocate additional queues",
__func__, slave_port);
return -1;
}
return 0;
}
int
bond_8023ad_slow_pkt_hw_filter_supported(uint16_t port_id) {
struct rte_eth_dev *bond_dev = &rte_eth_devices[port_id];
struct bond_dev_private *internals = bond_dev->data->dev_private;
struct rte_eth_dev_info bond_info;
uint16_t idx;
int ret;
/* Verify if all slaves in bonding supports flow director and */
if (internals->slave_count > 0) {
ret = rte_eth_dev_info_get(bond_dev->data->port_id, &bond_info);
if (ret != 0) {
RTE_BOND_LOG(ERR,
"%s: Error during getting device (port %u) info: %s\n",
__func__, bond_dev->data->port_id,
strerror(-ret));
return ret;
}
internals->mode4.dedicated_queues.rx_qid = bond_info.nb_rx_queues;
internals->mode4.dedicated_queues.tx_qid = bond_info.nb_tx_queues;
for (idx = 0; idx < internals->slave_count; idx++) {
if (bond_ethdev_8023ad_flow_verify(bond_dev,
internals->slaves[idx].port_id) != 0)
return -1;
}
}
return 0;
}
int
bond_ethdev_8023ad_flow_set(struct rte_eth_dev *bond_dev, uint16_t slave_port) {
struct rte_flow_error error;
struct bond_dev_private *internals = bond_dev->data->dev_private;
struct rte_flow_action_queue lacp_queue_conf = {
.index = internals->mode4.dedicated_queues.rx_qid,
};
const struct rte_flow_action actions[] = {
{
.type = RTE_FLOW_ACTION_TYPE_QUEUE,
.conf = &lacp_queue_conf
},
{
.type = RTE_FLOW_ACTION_TYPE_END,
}
};
internals->mode4.dedicated_queues.flow[slave_port] = rte_flow_create(slave_port,
&flow_attr_8023ad, flow_item_8023ad, actions, &error);
if (internals->mode4.dedicated_queues.flow[slave_port] == NULL) {
RTE_BOND_LOG(ERR, "bond_ethdev_8023ad_flow_set: %s "
"(slave_port=%d queue_id=%d)",
error.message, slave_port,
internals->mode4.dedicated_queues.rx_qid);
return -1;
}
return 0;
}
static inline uint16_t
rx_burst_8023ad(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts,
bool dedicated_rxq)
{
/* Cast to structure, containing bonded device's port id and queue id */
struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
struct bond_dev_private *internals = bd_rx_q->dev_private;
struct rte_eth_dev *bonded_eth_dev =
&rte_eth_devices[internals->port_id];
struct rte_ether_addr *bond_mac = bonded_eth_dev->data->mac_addrs;
struct rte_ether_hdr *hdr;
const uint16_t ether_type_slow_be =
rte_be_to_cpu_16(RTE_ETHER_TYPE_SLOW);
uint16_t num_rx_total = 0; /* Total number of received packets */
uint16_t slaves[RTE_MAX_ETHPORTS];
uint16_t slave_count, idx;
uint8_t collecting; /* current slave collecting status */
const uint8_t promisc = rte_eth_promiscuous_get(internals->port_id);
const uint8_t allmulti = rte_eth_allmulticast_get(internals->port_id);
uint8_t subtype;
uint16_t i;
uint16_t j;
uint16_t k;
/* Copy slave list to protect against slave up/down changes during tx
* bursting */
slave_count = internals->active_slave_count;
memcpy(slaves, internals->active_slaves,
sizeof(internals->active_slaves[0]) * slave_count);
idx = bd_rx_q->active_slave;
if (idx >= slave_count) {
bd_rx_q->active_slave = 0;
idx = 0;
}
for (i = 0; i < slave_count && num_rx_total < nb_pkts; i++) {
j = num_rx_total;
collecting = ACTOR_STATE(&bond_mode_8023ad_ports[slaves[idx]],
COLLECTING);
/* Read packets from this slave */
num_rx_total += rte_eth_rx_burst(slaves[idx], bd_rx_q->queue_id,
&bufs[num_rx_total], nb_pkts - num_rx_total);
for (k = j; k < 2 && k < num_rx_total; k++)
rte_prefetch0(rte_pktmbuf_mtod(bufs[k], void *));
/* Handle slow protocol packets. */
while (j < num_rx_total) {
if (j + 3 < num_rx_total)
rte_prefetch0(rte_pktmbuf_mtod(bufs[j + 3], void *));
hdr = rte_pktmbuf_mtod(bufs[j], struct rte_ether_hdr *);
subtype = ((struct slow_protocol_frame *)hdr)->slow_protocol.subtype;
/* Remove packet from array if:
* - it is slow packet but no dedicated rxq is present,
* - slave is not in collecting state,
* - bonding interface is not in promiscuous mode:
* - packet is unicast and address does not match,
* - packet is multicast and bonding interface
* is not in allmulti,
*/
if (unlikely(
(!dedicated_rxq &&
is_lacp_packets(hdr->ether_type, subtype,
bufs[j])) ||
!collecting ||
(!promisc &&
((rte_is_unicast_ether_addr(&hdr->d_addr) &&
!rte_is_same_ether_addr(bond_mac,
&hdr->d_addr)) ||
(!allmulti &&
rte_is_multicast_ether_addr(&hdr->d_addr)))))) {
if (hdr->ether_type == ether_type_slow_be) {
bond_mode_8023ad_handle_slow_pkt(
internals, slaves[idx], bufs[j]);
} else
rte_pktmbuf_free(bufs[j]);
/* Packet is managed by mode 4 or dropped, shift the array */
num_rx_total--;
if (j < num_rx_total) {
memmove(&bufs[j], &bufs[j + 1], sizeof(bufs[0]) *
(num_rx_total - j));
}
} else
j++;
}
if (unlikely(++idx == slave_count))
idx = 0;
}
if (++bd_rx_q->active_slave >= slave_count)
bd_rx_q->active_slave = 0;
return num_rx_total;
}
static uint16_t
bond_ethdev_rx_burst_8023ad(void *queue, struct rte_mbuf **bufs,
uint16_t nb_pkts)
{
return rx_burst_8023ad(queue, bufs, nb_pkts, false);
}
static uint16_t
bond_ethdev_rx_burst_8023ad_fast_queue(void *queue, struct rte_mbuf **bufs,
uint16_t nb_pkts)
{
return rx_burst_8023ad(queue, bufs, nb_pkts, true);
}
#if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
uint32_t burstnumberRX;
uint32_t burstnumberTX;
#ifdef RTE_LIBRTE_BOND_DEBUG_ALB
static void
arp_op_name(uint16_t arp_op, char *buf, size_t buf_len)
{
switch (arp_op) {
case RTE_ARP_OP_REQUEST:
strlcpy(buf, "ARP Request", buf_len);
return;
case RTE_ARP_OP_REPLY:
strlcpy(buf, "ARP Reply", buf_len);
return;
case RTE_ARP_OP_REVREQUEST:
strlcpy(buf, "Reverse ARP Request", buf_len);
return;
case RTE_ARP_OP_REVREPLY:
strlcpy(buf, "Reverse ARP Reply", buf_len);
return;
case RTE_ARP_OP_INVREQUEST:
strlcpy(buf, "Peer Identify Request", buf_len);
return;
case RTE_ARP_OP_INVREPLY:
strlcpy(buf, "Peer Identify Reply", buf_len);
return;
default:
break;
}
strlcpy(buf, "Unknown", buf_len);
return;
}
#endif
#define MaxIPv4String 16
static void
ipv4_addr_to_dot(uint32_t be_ipv4_addr, char *buf, uint8_t buf_size)
{
uint32_t ipv4_addr;
ipv4_addr = rte_be_to_cpu_32(be_ipv4_addr);
snprintf(buf, buf_size, "%d.%d.%d.%d", (ipv4_addr >> 24) & 0xFF,
(ipv4_addr >> 16) & 0xFF, (ipv4_addr >> 8) & 0xFF,
ipv4_addr & 0xFF);
}
#define MAX_CLIENTS_NUMBER 128
uint8_t active_clients;
struct client_stats_t {
uint16_t port;
uint32_t ipv4_addr;
uint32_t ipv4_rx_packets;
uint32_t ipv4_tx_packets;
};
struct client_stats_t client_stats[MAX_CLIENTS_NUMBER];
static void
update_client_stats(uint32_t addr, uint16_t port, uint32_t *TXorRXindicator)
{
int i = 0;
for (; i < MAX_CLIENTS_NUMBER; i++) {
if ((client_stats[i].ipv4_addr == addr) && (client_stats[i].port == port)) {
/* Just update RX packets number for this client */
if (TXorRXindicator == &burstnumberRX)
client_stats[i].ipv4_rx_packets++;
else
client_stats[i].ipv4_tx_packets++;
return;
}
}
/* We have a new client. Insert him to the table, and increment stats */
if (TXorRXindicator == &burstnumberRX)
client_stats[active_clients].ipv4_rx_packets++;
else
client_stats[active_clients].ipv4_tx_packets++;
client_stats[active_clients].ipv4_addr = addr;
client_stats[active_clients].port = port;
active_clients++;
}
#ifdef RTE_LIBRTE_BOND_DEBUG_ALB
#define MODE6_DEBUG(info, src_ip, dst_ip, eth_h, arp_op, port, burstnumber) \
rte_log(RTE_LOG_DEBUG, bond_logtype, \
"%s port:%d SrcMAC:%02X:%02X:%02X:%02X:%02X:%02X SrcIP:%s " \
"DstMAC:%02X:%02X:%02X:%02X:%02X:%02X DstIP:%s %s %d\n", \
info, \
port, \
eth_h->s_addr.addr_bytes[0], eth_h->s_addr.addr_bytes[1], \
eth_h->s_addr.addr_bytes[2], eth_h->s_addr.addr_bytes[3], \
eth_h->s_addr.addr_bytes[4], eth_h->s_addr.addr_bytes[5], \
src_ip, \
eth_h->d_addr.addr_bytes[0], eth_h->d_addr.addr_bytes[1], \
eth_h->d_addr.addr_bytes[2], eth_h->d_addr.addr_bytes[3], \
eth_h->d_addr.addr_bytes[4], eth_h->d_addr.addr_bytes[5], \
dst_ip, \
arp_op, ++burstnumber)
#endif
static void
mode6_debug(const char __rte_unused *info,
struct rte_ether_hdr *eth_h, uint16_t port,
uint32_t __rte_unused *burstnumber)
{
struct rte_ipv4_hdr *ipv4_h;
#ifdef RTE_LIBRTE_BOND_DEBUG_ALB
struct rte_arp_hdr *arp_h;
char dst_ip[16];
char ArpOp[24];
char buf[16];
#endif
char src_ip[16];
uint16_t ether_type = eth_h->ether_type;
uint16_t offset = get_vlan_offset(eth_h, ðer_type);
#ifdef RTE_LIBRTE_BOND_DEBUG_ALB
strlcpy(buf, info, 16);
#endif
if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
ipv4_h = (struct rte_ipv4_hdr *)((char *)(eth_h + 1) + offset);
ipv4_addr_to_dot(ipv4_h->src_addr, src_ip, MaxIPv4String);
#ifdef RTE_LIBRTE_BOND_DEBUG_ALB
ipv4_addr_to_dot(ipv4_h->dst_addr, dst_ip, MaxIPv4String);
MODE6_DEBUG(buf, src_ip, dst_ip, eth_h, "", port, *burstnumber);
#endif
update_client_stats(ipv4_h->src_addr, port, burstnumber);
}
#ifdef RTE_LIBRTE_BOND_DEBUG_ALB
else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
arp_h = (struct rte_arp_hdr *)((char *)(eth_h + 1) + offset);
ipv4_addr_to_dot(arp_h->arp_data.arp_sip, src_ip, MaxIPv4String);
ipv4_addr_to_dot(arp_h->arp_data.arp_tip, dst_ip, MaxIPv4String);
arp_op_name(rte_be_to_cpu_16(arp_h->arp_opcode),
ArpOp, sizeof(ArpOp));
MODE6_DEBUG(buf, src_ip, dst_ip, eth_h, ArpOp, port, *burstnumber);
}
#endif
}
#endif
static uint16_t
bond_ethdev_rx_burst_alb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
{
struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
struct bond_dev_private *internals = bd_rx_q->dev_private;
struct rte_ether_hdr *eth_h;
uint16_t ether_type, offset;
uint16_t nb_recv_pkts;
int i;
nb_recv_pkts = bond_ethdev_rx_burst(queue, bufs, nb_pkts);
for (i = 0; i < nb_recv_pkts; i++) {
eth_h = rte_pktmbuf_mtod(bufs[i], struct rte_ether_hdr *);
ether_type = eth_h->ether_type;
offset = get_vlan_offset(eth_h, ðer_type);
if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
#if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
mode6_debug("RX ARP:", eth_h, bufs[i]->port, &burstnumberRX);
#endif
bond_mode_alb_arp_recv(eth_h, offset, internals);
}
#if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
mode6_debug("RX IPv4:", eth_h, bufs[i]->port, &burstnumberRX);
#endif
}
return nb_recv_pkts;
}
static uint16_t
bond_ethdev_tx_burst_round_robin(void *queue, struct rte_mbuf **bufs,
uint16_t nb_pkts)
{
struct bond_dev_private *internals;
struct bond_tx_queue *bd_tx_q;
struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
uint16_t num_of_slaves;
uint16_t slaves[RTE_MAX_ETHPORTS];
uint16_t num_tx_total = 0, num_tx_slave;
static int slave_idx = 0;
int i, cslave_idx = 0, tx_fail_total = 0;
bd_tx_q = (struct bond_tx_queue *)queue;
internals = bd_tx_q->dev_private;
/* Copy slave list to protect against slave up/down changes during tx
* bursting */
num_of_slaves = internals->active_slave_count;
memcpy(slaves, internals->active_slaves,
sizeof(internals->active_slaves[0]) * num_of_slaves);
if (num_of_slaves < 1)
return num_tx_total;
/* Populate slaves mbuf with which packets are to be sent on it */
for (i = 0; i < nb_pkts; i++) {
cslave_idx = (slave_idx + i) % num_of_slaves;
slave_bufs[cslave_idx][(slave_nb_pkts[cslave_idx])++] = bufs[i];
}
/* increment current slave index so the next call to tx burst starts on the
* next slave */
slave_idx = ++cslave_idx;
/* Send packet burst on each slave device */
for (i = 0; i < num_of_slaves; i++) {
if (slave_nb_pkts[i] > 0) {
num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
slave_bufs[i], slave_nb_pkts[i]);
/* if tx burst fails move packets to end of bufs */
if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
int tx_fail_slave = slave_nb_pkts[i] - num_tx_slave;
tx_fail_total += tx_fail_slave;
memcpy(&bufs[nb_pkts - tx_fail_total],
&slave_bufs[i][num_tx_slave],
tx_fail_slave * sizeof(bufs[0]));
}
num_tx_total += num_tx_slave;
}
}
return num_tx_total;
}
static uint16_t
bond_ethdev_tx_burst_active_backup(void *queue,
struct rte_mbuf **bufs, uint16_t nb_pkts)
{
struct bond_dev_private *internals;
struct bond_tx_queue *bd_tx_q;
bd_tx_q = (struct bond_tx_queue *)queue;
internals = bd_tx_q->dev_private;
if (internals->active_slave_count < 1)
return 0;
return rte_eth_tx_burst(internals->current_primary_port, bd_tx_q->queue_id,
bufs, nb_pkts);
}
static inline uint16_t
ether_hash(struct rte_ether_hdr *eth_hdr)
{
unaligned_uint16_t *word_src_addr =
(unaligned_uint16_t *)eth_hdr->s_addr.addr_bytes;
unaligned_uint16_t *word_dst_addr =
(unaligned_uint16_t *)eth_hdr->d_addr.addr_bytes;
return (word_src_addr[0] ^ word_dst_addr[0]) ^
(word_src_addr[1] ^ word_dst_addr[1]) ^
(word_src_addr[2] ^ word_dst_addr[2]);
}
static inline uint32_t
ipv4_hash(struct rte_ipv4_hdr *ipv4_hdr)
{
return ipv4_hdr->src_addr ^ ipv4_hdr->dst_addr;
}
static inline uint32_t
ipv6_hash(struct rte_ipv6_hdr *ipv6_hdr)
{
unaligned_uint32_t *word_src_addr =
(unaligned_uint32_t *)&(ipv6_hdr->src_addr[0]);
unaligned_uint32_t *word_dst_addr =
(unaligned_uint32_t *)&(ipv6_hdr->dst_addr[0]);
return (word_src_addr[0] ^ word_dst_addr[0]) ^
(word_src_addr[1] ^ word_dst_addr[1]) ^
(word_src_addr[2] ^ word_dst_addr[2]) ^
(word_src_addr[3] ^ word_dst_addr[3]);
}
void
burst_xmit_l2_hash(struct rte_mbuf **buf, uint16_t nb_pkts,
uint16_t slave_count, uint16_t *slaves)
{
struct rte_ether_hdr *eth_hdr;
uint32_t hash;
int i;
for (i = 0; i < nb_pkts; i++) {
eth_hdr = rte_pktmbuf_mtod(buf[i], struct rte_ether_hdr *);
hash = ether_hash(eth_hdr);
slaves[i] = (hash ^= hash >> 8) % slave_count;
}
}
void
burst_xmit_l23_hash(struct rte_mbuf **buf, uint16_t nb_pkts,
uint16_t slave_count, uint16_t *slaves)
{
uint16_t i;
struct rte_ether_hdr *eth_hdr;
uint16_t proto;
size_t vlan_offset;
uint32_t hash, l3hash;
for (i = 0; i < nb_pkts; i++) {
eth_hdr = rte_pktmbuf_mtod(buf[i], struct rte_ether_hdr *);
l3hash = 0;
proto = eth_hdr->ether_type;
hash = ether_hash(eth_hdr);
vlan_offset = get_vlan_offset(eth_hdr, &proto);
if (rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4) == proto) {
struct rte_ipv4_hdr *ipv4_hdr = (struct rte_ipv4_hdr *)
((char *)(eth_hdr + 1) + vlan_offset);
l3hash = ipv4_hash(ipv4_hdr);
} else if (rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6) == proto) {
struct rte_ipv6_hdr *ipv6_hdr = (struct rte_ipv6_hdr *)
((char *)(eth_hdr + 1) + vlan_offset);
l3hash = ipv6_hash(ipv6_hdr);
}
hash = hash ^ l3hash;
hash ^= hash >> 16;
hash ^= hash >> 8;
slaves[i] = hash % slave_count;
}
}
void
burst_xmit_l34_hash(struct rte_mbuf **buf, uint16_t nb_pkts,
uint16_t slave_count, uint16_t *slaves)
{
struct rte_ether_hdr *eth_hdr;
uint16_t proto;
size_t vlan_offset;
int i;
struct rte_udp_hdr *udp_hdr;
struct rte_tcp_hdr *tcp_hdr;
uint32_t hash, l3hash, l4hash;
for (i = 0; i < nb_pkts; i++) {
eth_hdr = rte_pktmbuf_mtod(buf[i], struct rte_ether_hdr *);
size_t pkt_end = (size_t)eth_hdr + rte_pktmbuf_data_len(buf[i]);
proto = eth_hdr->ether_type;
vlan_offset = get_vlan_offset(eth_hdr, &proto);
l3hash = 0;
l4hash = 0;
if (rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4) == proto) {
struct rte_ipv4_hdr *ipv4_hdr = (struct rte_ipv4_hdr *)
((char *)(eth_hdr + 1) + vlan_offset);
size_t ip_hdr_offset;
l3hash = ipv4_hash(ipv4_hdr);
/* there is no L4 header in fragmented packet */
if (likely(rte_ipv4_frag_pkt_is_fragmented(ipv4_hdr)
== 0)) {
ip_hdr_offset = (ipv4_hdr->version_ihl
& RTE_IPV4_HDR_IHL_MASK) *
RTE_IPV4_IHL_MULTIPLIER;
if (ipv4_hdr->next_proto_id == IPPROTO_TCP) {
tcp_hdr = (struct rte_tcp_hdr *)
((char *)ipv4_hdr +
ip_hdr_offset);
if ((size_t)tcp_hdr + sizeof(*tcp_hdr)
< pkt_end)
l4hash = HASH_L4_PORTS(tcp_hdr);
} else if (ipv4_hdr->next_proto_id ==
IPPROTO_UDP) {
udp_hdr = (struct rte_udp_hdr *)
((char *)ipv4_hdr +
ip_hdr_offset);
if ((size_t)udp_hdr + sizeof(*udp_hdr)
< pkt_end)
l4hash = HASH_L4_PORTS(udp_hdr);
}
}
} else if (rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6) == proto) {
struct rte_ipv6_hdr *ipv6_hdr = (struct rte_ipv6_hdr *)
((char *)(eth_hdr + 1) + vlan_offset);
l3hash = ipv6_hash(ipv6_hdr);
if (ipv6_hdr->proto == IPPROTO_TCP) {
tcp_hdr = (struct rte_tcp_hdr *)(ipv6_hdr + 1);
l4hash = HASH_L4_PORTS(tcp_hdr);
} else if (ipv6_hdr->proto == IPPROTO_UDP) {
udp_hdr = (struct rte_udp_hdr *)(ipv6_hdr + 1);
l4hash = HASH_L4_PORTS(udp_hdr);
}
}
hash = l3hash ^ l4hash;
hash ^= hash >> 16;
hash ^= hash >> 8;
slaves[i] = hash % slave_count;
}
}
struct bwg_slave {
uint64_t bwg_left_int;
uint64_t bwg_left_remainder;
uint16_t slave;
};
void
bond_tlb_activate_slave(struct bond_dev_private *internals) {
int i;
for (i = 0; i < internals->active_slave_count; i++) {
tlb_last_obytets[internals->active_slaves[i]] = 0;
}
}
static int
bandwidth_cmp(const void *a, const void *b)
{
const struct bwg_slave *bwg_a = a;
const struct bwg_slave *bwg_b = b;
int64_t diff = (int64_t)bwg_b->bwg_left_int - (int64_t)bwg_a->bwg_left_int;
int64_t diff2 = (int64_t)bwg_b->bwg_left_remainder -
(int64_t)bwg_a->bwg_left_remainder;
if (diff > 0)
return 1;
else if (diff < 0)
return -1;
else if (diff2 > 0)
return 1;
else if (diff2 < 0)
return -1;
else
return 0;
}
static void
bandwidth_left(uint16_t port_id, uint64_t load, uint8_t update_idx,
struct bwg_slave *bwg_slave)
{
struct rte_eth_link link_status;
int ret;
ret = rte_eth_link_get_nowait(port_id, &link_status);
if (ret < 0) {
RTE_BOND_LOG(ERR, "Slave (port %u) link get failed: %s",
port_id, rte_strerror(-ret));
return;
}
uint64_t link_bwg = link_status.link_speed * 1000000ULL / 8;
if (link_bwg == 0)
return;
link_bwg = link_bwg * (update_idx+1) * REORDER_PERIOD_MS;
bwg_slave->bwg_left_int = (link_bwg - 1000*load) / link_bwg;
bwg_slave->bwg_left_remainder = (link_bwg - 1000*load) % link_bwg;
}
static void
bond_ethdev_update_tlb_slave_cb(void *arg)
{
struct bond_dev_private *internals = arg;
struct rte_eth_stats slave_stats;
struct bwg_slave bwg_array[RTE_MAX_ETHPORTS];
uint16_t slave_count;
uint64_t tx_bytes;
uint8_t update_stats = 0;
uint16_t slave_id;
uint16_t i;
internals->slave_update_idx++;
if (internals->slave_update_idx >= REORDER_PERIOD_MS)
update_stats = 1;
for (i = 0; i < internals->active_slave_count; i++) {
slave_id = internals->active_slaves[i];
rte_eth_stats_get(slave_id, &slave_stats);
tx_bytes = slave_stats.obytes - tlb_last_obytets[slave_id];
bandwidth_left(slave_id, tx_bytes,
internals->slave_update_idx, &bwg_array[i]);
bwg_array[i].slave = slave_id;
if (update_stats) {
tlb_last_obytets[slave_id] = slave_stats.obytes;
}
}
if (update_stats == 1)
internals->slave_update_idx = 0;
slave_count = i;
qsort(bwg_array, slave_count, sizeof(bwg_array[0]), bandwidth_cmp);
for (i = 0; i < slave_count; i++)
internals->tlb_slaves_order[i] = bwg_array[i].slave;
rte_eal_alarm_set(REORDER_PERIOD_MS * 1000, bond_ethdev_update_tlb_slave_cb,
(struct bond_dev_private *)internals);
}
static uint16_t
bond_ethdev_tx_burst_tlb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
{
struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
struct bond_dev_private *internals = bd_tx_q->dev_private;
struct rte_eth_dev *primary_port =
&rte_eth_devices[internals->primary_port];
uint16_t num_tx_total = 0;
uint16_t i, j;
uint16_t num_of_slaves = internals->active_slave_count;
uint16_t slaves[RTE_MAX_ETHPORTS];
struct rte_ether_hdr *ether_hdr;
struct rte_ether_addr primary_slave_addr;
struct rte_ether_addr active_slave_addr;
if (num_of_slaves < 1)
return num_tx_total;
memcpy(slaves, internals->tlb_slaves_order,
sizeof(internals->tlb_slaves_order[0]) * num_of_slaves);
rte_ether_addr_copy(primary_port->data->mac_addrs, &primary_slave_addr);
if (nb_pkts > 3) {
for (i = 0; i < 3; i++)
rte_prefetch0(rte_pktmbuf_mtod(bufs[i], void*));
}
for (i = 0; i < num_of_slaves; i++) {
rte_eth_macaddr_get(slaves[i], &active_slave_addr);
for (j = num_tx_total; j < nb_pkts; j++) {
if (j + 3 < nb_pkts)
rte_prefetch0(rte_pktmbuf_mtod(bufs[j+3], void*));
ether_hdr = rte_pktmbuf_mtod(bufs[j],
struct rte_ether_hdr *);
if (rte_is_same_ether_addr(ðer_hdr->s_addr,
&primary_slave_addr))
rte_ether_addr_copy(&active_slave_addr,
ðer_hdr->s_addr);
#if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
mode6_debug("TX IPv4:", ether_hdr, slaves[i], &burstnumberTX);
#endif
}
num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
bufs + num_tx_total, nb_pkts - num_tx_total);
if (num_tx_total == nb_pkts)
break;
}
return num_tx_total;
}
void
bond_tlb_disable(struct bond_dev_private *internals)
{
rte_eal_alarm_cancel(bond_ethdev_update_tlb_slave_cb, internals);
}
void
bond_tlb_enable(struct bond_dev_private *internals)
{
bond_ethdev_update_tlb_slave_cb(internals);
}
static uint16_t
bond_ethdev_tx_burst_alb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
{
struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
struct bond_dev_private *internals = bd_tx_q->dev_private;
struct rte_ether_hdr *eth_h;
uint16_t ether_type, offset;
struct client_data *client_info;
/*
* We create transmit buffers for every slave and one additional to send
* through tlb. In worst case every packet will be send on one port.
*/
struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS + 1][nb_pkts];
uint16_t slave_bufs_pkts[RTE_MAX_ETHPORTS + 1] = { 0 };
/*
* We create separate transmit buffers for update packets as they won't
* be counted in num_tx_total.