mbuf: remove build option to disable refcnt
[dpdk.git] / lib / librte_pmd_bond / rte_eth_bond_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdlib.h>
34 #include <netinet/in.h>
35
36 #include <rte_mbuf.h>
37 #include <rte_malloc.h>
38 #include <rte_ethdev.h>
39 #include <rte_tcp.h>
40 #include <rte_udp.h>
41 #include <rte_ip.h>
42 #include <rte_devargs.h>
43 #include <rte_kvargs.h>
44 #include <rte_dev.h>
45 #include <rte_alarm.h>
46 #include <rte_cycles.h>
47
48 #include "rte_eth_bond.h"
49 #include "rte_eth_bond_private.h"
50 #include "rte_eth_bond_8023ad_private.h"
51
52 #define REORDER_PERIOD_MS 10
53
54 #define HASH_L4_PORTS(h) ((h)->src_port ^ (h)->dst_port)
55
56 /* Table for statistics in mode 5 TLB */
57 static uint64_t tlb_last_obytets[RTE_MAX_ETHPORTS];
58
59 static inline size_t
60 get_vlan_offset(struct ether_hdr *eth_hdr, uint16_t *proto)
61 {
62         size_t vlan_offset = 0;
63
64         if (rte_cpu_to_be_16(ETHER_TYPE_VLAN) == *proto) {
65                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
66
67                 vlan_offset = sizeof(struct vlan_hdr);
68                 *proto = vlan_hdr->eth_proto;
69
70                 if (rte_cpu_to_be_16(ETHER_TYPE_VLAN) == *proto) {
71                         vlan_hdr = vlan_hdr + 1;
72                         *proto = vlan_hdr->eth_proto;
73                         vlan_offset += sizeof(struct vlan_hdr);
74                 }
75         }
76         return vlan_offset;
77 }
78
79 static uint16_t
80 bond_ethdev_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
81 {
82         struct bond_dev_private *internals;
83
84         uint16_t num_rx_slave = 0;
85         uint16_t num_rx_total = 0;
86
87         int i;
88
89         /* Cast to structure, containing bonded device's port id and queue id */
90         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
91
92         internals = bd_rx_q->dev_private;
93
94
95         for (i = 0; i < internals->active_slave_count && nb_pkts; i++) {
96                 /* Offset of pointer to *bufs increases as packets are received
97                  * from other slaves */
98                 num_rx_slave = rte_eth_rx_burst(internals->active_slaves[i],
99                                 bd_rx_q->queue_id, bufs + num_rx_total, nb_pkts);
100                 if (num_rx_slave) {
101                         num_rx_total += num_rx_slave;
102                         nb_pkts -= num_rx_slave;
103                 }
104         }
105
106         return num_rx_total;
107 }
108
109 static uint16_t
110 bond_ethdev_rx_burst_active_backup(void *queue, struct rte_mbuf **bufs,
111                 uint16_t nb_pkts)
112 {
113         struct bond_dev_private *internals;
114
115         /* Cast to structure, containing bonded device's port id and queue id */
116         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
117
118         internals = bd_rx_q->dev_private;
119
120         return rte_eth_rx_burst(internals->current_primary_port,
121                         bd_rx_q->queue_id, bufs, nb_pkts);
122 }
123
124 static uint16_t
125 bond_ethdev_rx_burst_8023ad(void *queue, struct rte_mbuf **bufs,
126                 uint16_t nb_pkts)
127 {
128         /* Cast to structure, containing bonded device's port id and queue id */
129         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
130         struct bond_dev_private *internals = bd_rx_q->dev_private;
131         struct ether_addr bond_mac;
132
133         struct ether_hdr *hdr;
134
135         const uint16_t ether_type_slow_be = rte_be_to_cpu_16(ETHER_TYPE_SLOW);
136         uint16_t num_rx_total = 0;      /* Total number of received packets */
137         uint8_t slaves[RTE_MAX_ETHPORTS];
138         uint8_t slave_count;
139
140         uint8_t collecting;  /* current slave collecting status */
141         const uint8_t promisc = internals->promiscuous_en;
142         uint8_t i, j, k;
143
144         rte_eth_macaddr_get(internals->port_id, &bond_mac);
145         /* Copy slave list to protect against slave up/down changes during tx
146          * bursting */
147         slave_count = internals->active_slave_count;
148         memcpy(slaves, internals->active_slaves,
149                         sizeof(internals->active_slaves[0]) * slave_count);
150
151         for (i = 0; i < slave_count && num_rx_total < nb_pkts; i++) {
152                 j = num_rx_total;
153                 collecting = ACTOR_STATE(&mode_8023ad_ports[slaves[i]], COLLECTING);
154
155                 /* Read packets from this slave */
156                 num_rx_total += rte_eth_rx_burst(slaves[i], bd_rx_q->queue_id,
157                                 &bufs[num_rx_total], nb_pkts - num_rx_total);
158
159                 for (k = j; k < 2 && k < num_rx_total; k++)
160                         rte_prefetch0(rte_pktmbuf_mtod(bufs[k], void *));
161
162                 /* Handle slow protocol packets. */
163                 while (j < num_rx_total) {
164                         if (j + 3 < num_rx_total)
165                                 rte_prefetch0(rte_pktmbuf_mtod(bufs[j + 3], void *));
166
167                         hdr = rte_pktmbuf_mtod(bufs[j], struct ether_hdr *);
168                         /* Remove packet from array if it is slow packet or slave is not
169                          * in collecting state or bondign interface is not in promiscus
170                          * mode and packet address does not match. */
171                         if (unlikely(hdr->ether_type == ether_type_slow_be ||
172                                 !collecting || (!promisc &&
173                                         !is_same_ether_addr(&bond_mac, &hdr->d_addr)))) {
174
175                                 if (hdr->ether_type == ether_type_slow_be) {
176                                         bond_mode_8023ad_handle_slow_pkt(internals, slaves[i],
177                                                 bufs[j]);
178                                 } else
179                                         rte_pktmbuf_free(bufs[j]);
180
181                                 /* Packet is managed by mode 4 or dropped, shift the array */
182                                 num_rx_total--;
183                                 if (j < num_rx_total) {
184                                         memmove(&bufs[j], &bufs[j + 1], sizeof(bufs[0]) *
185                                                 (num_rx_total - j));
186                                 }
187                         } else
188                                 j++;
189                 }
190         }
191
192         return num_rx_total;
193 }
194
195 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
196 uint32_t burstnumberRX;
197 uint32_t burstnumberTX;
198
199 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
200
201 static void
202 arp_op_name(uint16_t arp_op, char *buf)
203 {
204         switch (arp_op) {
205         case ARP_OP_REQUEST:
206                 snprintf(buf, sizeof("ARP Request"), "%s", "ARP Request");
207                 return;
208         case ARP_OP_REPLY:
209                 snprintf(buf, sizeof("ARP Reply"), "%s", "ARP Reply");
210                 return;
211         case ARP_OP_REVREQUEST:
212                 snprintf(buf, sizeof("Reverse ARP Request"), "%s",
213                                 "Reverse ARP Request");
214                 return;
215         case ARP_OP_REVREPLY:
216                 snprintf(buf, sizeof("Reverse ARP Reply"), "%s",
217                                 "Reverse ARP Reply");
218                 return;
219         case ARP_OP_INVREQUEST:
220                 snprintf(buf, sizeof("Peer Identify Request"), "%s",
221                                 "Peer Identify Request");
222                 return;
223         case ARP_OP_INVREPLY:
224                 snprintf(buf, sizeof("Peer Identify Reply"), "%s",
225                                 "Peer Identify Reply");
226                 return;
227         default:
228                 break;
229         }
230         snprintf(buf, sizeof("Unknown"), "%s", "Unknown");
231         return;
232 }
233 #endif
234 #define MaxIPv4String   16
235 static void
236 ipv4_addr_to_dot(uint32_t be_ipv4_addr, char *buf, uint8_t buf_size)
237 {
238         uint32_t ipv4_addr;
239
240         ipv4_addr = rte_be_to_cpu_32(be_ipv4_addr);
241         snprintf(buf, buf_size, "%d.%d.%d.%d", (ipv4_addr >> 24) & 0xFF,
242                 (ipv4_addr >> 16) & 0xFF, (ipv4_addr >> 8) & 0xFF,
243                 ipv4_addr & 0xFF);
244 }
245
246 #define MAX_CLIENTS_NUMBER      128
247 uint8_t active_clients;
248 struct client_stats_t {
249         uint8_t port;
250         uint32_t ipv4_addr;
251         uint32_t ipv4_rx_packets;
252         uint32_t ipv4_tx_packets;
253 };
254 struct client_stats_t client_stats[MAX_CLIENTS_NUMBER];
255
256 static void
257 update_client_stats(uint32_t addr, uint8_t port, uint32_t *TXorRXindicator)
258 {
259         int i = 0;
260
261         for (; i < MAX_CLIENTS_NUMBER; i++)     {
262                 if ((client_stats[i].ipv4_addr == addr) && (client_stats[i].port == port))      {
263                         /* Just update RX packets number for this client */
264                         if (TXorRXindicator == &burstnumberRX)
265                                 client_stats[i].ipv4_rx_packets++;
266                         else
267                                 client_stats[i].ipv4_tx_packets++;
268                         return;
269                 }
270         }
271         /* We have a new client. Insert him to the table, and increment stats */
272         if (TXorRXindicator == &burstnumberRX)
273                 client_stats[active_clients].ipv4_rx_packets++;
274         else
275                 client_stats[active_clients].ipv4_tx_packets++;
276         client_stats[active_clients].ipv4_addr = addr;
277         client_stats[active_clients].port = port;
278         active_clients++;
279
280 }
281
282 void print_client_stats(void);
283 void print_client_stats(void)
284 {
285         int i = 0;
286         char buf[MaxIPv4String];
287
288         for (; i < active_clients; i++) {
289                 ipv4_addr_to_dot(client_stats[i].ipv4_addr, buf, MaxIPv4String);
290                 printf("port:%d client:%s RX:%d TX:%d\n", client_stats[i].port, buf,
291                                 client_stats[i].ipv4_rx_packets,
292                                 client_stats[i].ipv4_tx_packets);
293         }
294 }
295 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
296 #define MODE6_DEBUG(info, src_ip, dst_ip, eth_h, arp_op, port, burstnumber)     \
297                 RTE_LOG(DEBUG, PMD, \
298                 "%s " \
299                 "port:%d " \
300                 "SrcMAC:%02X:%02X:%02X:%02X:%02X:%02X " \
301                 "SrcIP:%s " \
302                 "DstMAC:%02X:%02X:%02X:%02X:%02X:%02X " \
303                 "DstIP:%s " \
304                 "%s " \
305                 "%d\n", \
306                 info, \
307                 port, \
308                 eth_h->s_addr.addr_bytes[0], \
309                 eth_h->s_addr.addr_bytes[1], \
310                 eth_h->s_addr.addr_bytes[2], \
311                 eth_h->s_addr.addr_bytes[3], \
312                 eth_h->s_addr.addr_bytes[4], \
313                 eth_h->s_addr.addr_bytes[5], \
314                 src_ip, \
315                 eth_h->d_addr.addr_bytes[0], \
316                 eth_h->d_addr.addr_bytes[1], \
317                 eth_h->d_addr.addr_bytes[2], \
318                 eth_h->d_addr.addr_bytes[3], \
319                 eth_h->d_addr.addr_bytes[4], \
320                 eth_h->d_addr.addr_bytes[5], \
321                 dst_ip, \
322                 arp_op, \
323                 ++burstnumber)
324 #endif
325
326 static void
327 mode6_debug(const char __attribute__((unused)) *info, struct ether_hdr *eth_h,
328                 uint8_t port, uint32_t __attribute__((unused)) *burstnumber)
329 {
330         struct ipv4_hdr *ipv4_h;
331 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
332         struct arp_hdr *arp_h;
333         char dst_ip[16];
334         char ArpOp[24];
335         char buf[16];
336 #endif
337         char src_ip[16];
338
339         uint16_t ether_type = eth_h->ether_type;
340         uint16_t offset = get_vlan_offset(eth_h, &ether_type);
341
342 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
343         snprintf(buf, 16, "%s", info);
344 #endif
345
346         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
347                 ipv4_h = (struct ipv4_hdr *)((char *)(eth_h + 1) + offset);
348                 ipv4_addr_to_dot(ipv4_h->src_addr, src_ip, MaxIPv4String);
349 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
350                 ipv4_addr_to_dot(ipv4_h->dst_addr, dst_ip, MaxIPv4String);
351                 MODE6_DEBUG(buf, src_ip, dst_ip, eth_h, "", port, *burstnumber);
352 #endif
353                 update_client_stats(ipv4_h->src_addr, port, burstnumber);
354         }
355 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
356         else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_ARP)) {
357                 arp_h = (struct arp_hdr *)((char *)(eth_h + 1) + offset);
358                 ipv4_addr_to_dot(arp_h->arp_data.arp_sip, src_ip, MaxIPv4String);
359                 ipv4_addr_to_dot(arp_h->arp_data.arp_tip, dst_ip, MaxIPv4String);
360                 arp_op_name(rte_be_to_cpu_16(arp_h->arp_op), ArpOp);
361                 MODE6_DEBUG(buf, src_ip, dst_ip, eth_h, ArpOp, port, *burstnumber);
362         }
363 #endif
364 }
365 #endif
366
367 static uint16_t
368 bond_ethdev_rx_burst_alb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
369 {
370         struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
371         struct bond_dev_private *internals = bd_tx_q->dev_private;
372         struct ether_hdr *eth_h;
373         uint16_t ether_type, offset;
374         uint16_t nb_recv_pkts;
375         int i;
376
377         nb_recv_pkts = bond_ethdev_rx_burst(queue, bufs, nb_pkts);
378
379         for (i = 0; i < nb_recv_pkts; i++) {
380                 eth_h = rte_pktmbuf_mtod(bufs[i], struct ether_hdr *);
381                 ether_type = eth_h->ether_type;
382                 offset = get_vlan_offset(eth_h, &ether_type);
383
384                 if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_ARP)) {
385 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
386                         mode6_debug("RX ARP:", eth_h, bufs[i]->port, &burstnumberRX);
387 #endif
388                         bond_mode_alb_arp_recv(eth_h, offset, internals);
389                 }
390 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
391                 else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
392                         mode6_debug("RX IPv4:", eth_h, bufs[i]->port, &burstnumberRX);
393 #endif
394         }
395
396         return nb_recv_pkts;
397 }
398
399 static uint16_t
400 bond_ethdev_tx_burst_round_robin(void *queue, struct rte_mbuf **bufs,
401                 uint16_t nb_pkts)
402 {
403         struct bond_dev_private *internals;
404         struct bond_tx_queue *bd_tx_q;
405
406         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
407         uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
408
409         uint8_t num_of_slaves;
410         uint8_t slaves[RTE_MAX_ETHPORTS];
411
412         uint16_t num_tx_total = 0, num_tx_slave;
413
414         static int slave_idx = 0;
415         int i, cslave_idx = 0, tx_fail_total = 0;
416
417         bd_tx_q = (struct bond_tx_queue *)queue;
418         internals = bd_tx_q->dev_private;
419
420         /* Copy slave list to protect against slave up/down changes during tx
421          * bursting */
422         num_of_slaves = internals->active_slave_count;
423         memcpy(slaves, internals->active_slaves,
424                         sizeof(internals->active_slaves[0]) * num_of_slaves);
425
426         if (num_of_slaves < 1)
427                 return num_tx_total;
428
429         /* Populate slaves mbuf with which packets are to be sent on it  */
430         for (i = 0; i < nb_pkts; i++) {
431                 cslave_idx = (slave_idx + i) % num_of_slaves;
432                 slave_bufs[cslave_idx][(slave_nb_pkts[cslave_idx])++] = bufs[i];
433         }
434
435         /* increment current slave index so the next call to tx burst starts on the
436          * next slave */
437         slave_idx = ++cslave_idx;
438
439         /* Send packet burst on each slave device */
440         for (i = 0; i < num_of_slaves; i++) {
441                 if (slave_nb_pkts[i] > 0) {
442                         num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
443                                         slave_bufs[i], slave_nb_pkts[i]);
444
445                         /* if tx burst fails move packets to end of bufs */
446                         if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
447                                 int tx_fail_slave = slave_nb_pkts[i] - num_tx_slave;
448
449                                 tx_fail_total += tx_fail_slave;
450
451                                 memcpy(&bufs[nb_pkts - tx_fail_total],
452                                                 &slave_bufs[i][num_tx_slave],
453                                                 tx_fail_slave * sizeof(bufs[0]));
454                         }
455                         num_tx_total += num_tx_slave;
456                 }
457         }
458
459         return num_tx_total;
460 }
461
462 static uint16_t
463 bond_ethdev_tx_burst_active_backup(void *queue,
464                 struct rte_mbuf **bufs, uint16_t nb_pkts)
465 {
466         struct bond_dev_private *internals;
467         struct bond_tx_queue *bd_tx_q;
468
469         bd_tx_q = (struct bond_tx_queue *)queue;
470         internals = bd_tx_q->dev_private;
471
472         if (internals->active_slave_count < 1)
473                 return 0;
474
475         return rte_eth_tx_burst(internals->current_primary_port, bd_tx_q->queue_id,
476                         bufs, nb_pkts);
477 }
478
479 static inline uint16_t
480 ether_hash(struct ether_hdr *eth_hdr)
481 {
482         uint16_t *word_src_addr = (uint16_t *)eth_hdr->s_addr.addr_bytes;
483         uint16_t *word_dst_addr = (uint16_t *)eth_hdr->d_addr.addr_bytes;
484
485         return (word_src_addr[0] ^ word_dst_addr[0]) ^
486                         (word_src_addr[1] ^ word_dst_addr[1]) ^
487                         (word_src_addr[2] ^ word_dst_addr[2]);
488 }
489
490 static inline uint32_t
491 ipv4_hash(struct ipv4_hdr *ipv4_hdr)
492 {
493         return (ipv4_hdr->src_addr ^ ipv4_hdr->dst_addr);
494 }
495
496 static inline uint32_t
497 ipv6_hash(struct ipv6_hdr *ipv6_hdr)
498 {
499         uint32_t *word_src_addr = (uint32_t *)&(ipv6_hdr->src_addr[0]);
500         uint32_t *word_dst_addr = (uint32_t *)&(ipv6_hdr->dst_addr[0]);
501
502         return (word_src_addr[0] ^ word_dst_addr[0]) ^
503                         (word_src_addr[1] ^ word_dst_addr[1]) ^
504                         (word_src_addr[2] ^ word_dst_addr[2]) ^
505                         (word_src_addr[3] ^ word_dst_addr[3]);
506 }
507
508 uint16_t
509 xmit_l2_hash(const struct rte_mbuf *buf, uint8_t slave_count)
510 {
511         struct ether_hdr *eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
512
513         uint32_t hash = ether_hash(eth_hdr);
514
515         return (hash ^= hash >> 8) % slave_count;
516 }
517
518 uint16_t
519 xmit_l23_hash(const struct rte_mbuf *buf, uint8_t slave_count)
520 {
521         struct ether_hdr *eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
522         uint16_t proto = eth_hdr->ether_type;
523         size_t vlan_offset = get_vlan_offset(eth_hdr, &proto);
524         uint32_t hash, l3hash = 0;
525
526         hash = ether_hash(eth_hdr);
527
528         if (rte_cpu_to_be_16(ETHER_TYPE_IPv4) == proto) {
529                 struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)
530                                 ((char *)(eth_hdr + 1) + vlan_offset);
531                 l3hash = ipv4_hash(ipv4_hdr);
532
533         } else if (rte_cpu_to_be_16(ETHER_TYPE_IPv6) == proto) {
534                 struct ipv6_hdr *ipv6_hdr = (struct ipv6_hdr *)
535                                 ((char *)(eth_hdr + 1) + vlan_offset);
536                 l3hash = ipv6_hash(ipv6_hdr);
537         }
538
539         hash = hash ^ l3hash;
540         hash ^= hash >> 16;
541         hash ^= hash >> 8;
542
543         return hash % slave_count;
544 }
545
546 uint16_t
547 xmit_l34_hash(const struct rte_mbuf *buf, uint8_t slave_count)
548 {
549         struct ether_hdr *eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
550         uint16_t proto = eth_hdr->ether_type;
551         size_t vlan_offset = get_vlan_offset(eth_hdr, &proto);
552
553         struct udp_hdr *udp_hdr = NULL;
554         struct tcp_hdr *tcp_hdr = NULL;
555         uint32_t hash, l3hash = 0, l4hash = 0;
556
557         if (rte_cpu_to_be_16(ETHER_TYPE_IPv4) == proto) {
558                 struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)
559                                 ((char *)(eth_hdr + 1) + vlan_offset);
560                 size_t ip_hdr_offset;
561
562                 l3hash = ipv4_hash(ipv4_hdr);
563
564                 ip_hdr_offset = (ipv4_hdr->version_ihl & IPV4_HDR_IHL_MASK) *
565                                 IPV4_IHL_MULTIPLIER;
566
567                 if (ipv4_hdr->next_proto_id == IPPROTO_TCP) {
568                         tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr +
569                                         ip_hdr_offset);
570                         l4hash = HASH_L4_PORTS(tcp_hdr);
571                 } else if (ipv4_hdr->next_proto_id == IPPROTO_UDP) {
572                         udp_hdr = (struct udp_hdr *)((char *)ipv4_hdr +
573                                         ip_hdr_offset);
574                         l4hash = HASH_L4_PORTS(udp_hdr);
575                 }
576         } else if  (rte_cpu_to_be_16(ETHER_TYPE_IPv6) == proto) {
577                 struct ipv6_hdr *ipv6_hdr = (struct ipv6_hdr *)
578                                 ((char *)(eth_hdr + 1) + vlan_offset);
579                 l3hash = ipv6_hash(ipv6_hdr);
580
581                 if (ipv6_hdr->proto == IPPROTO_TCP) {
582                         tcp_hdr = (struct tcp_hdr *)(ipv6_hdr + 1);
583                         l4hash = HASH_L4_PORTS(tcp_hdr);
584                 } else if (ipv6_hdr->proto == IPPROTO_UDP) {
585                         udp_hdr = (struct udp_hdr *)(ipv6_hdr + 1);
586                         l4hash = HASH_L4_PORTS(udp_hdr);
587                 }
588         }
589
590         hash = l3hash ^ l4hash;
591         hash ^= hash >> 16;
592         hash ^= hash >> 8;
593
594         return hash % slave_count;
595 }
596
597 struct bwg_slave {
598         uint64_t bwg_left_int;
599         uint64_t bwg_left_remainder;
600         uint8_t slave;
601 };
602
603 void
604 bond_tlb_activate_slave(struct bond_dev_private *internals) {
605         int i;
606
607         for (i = 0; i < internals->active_slave_count; i++) {
608                 tlb_last_obytets[internals->active_slaves[i]] = 0;
609         }
610 }
611
612 static int
613 bandwidth_cmp(const void *a, const void *b)
614 {
615         const struct bwg_slave *bwg_a = a;
616         const struct bwg_slave *bwg_b = b;
617         int64_t diff = (int64_t)bwg_b->bwg_left_int - (int64_t)bwg_a->bwg_left_int;
618         int64_t diff2 = (int64_t)bwg_b->bwg_left_remainder -
619                         (int64_t)bwg_a->bwg_left_remainder;
620         if (diff > 0)
621                 return 1;
622         else if (diff < 0)
623                 return -1;
624         else if (diff2 > 0)
625                 return 1;
626         else if (diff2 < 0)
627                 return -1;
628         else
629                 return 0;
630 }
631
632 static void
633 bandwidth_left(int port_id, uint64_t load, uint8_t update_idx,
634                 struct bwg_slave *bwg_slave)
635 {
636         struct rte_eth_link link_status;
637
638         rte_eth_link_get(port_id, &link_status);
639         uint64_t link_bwg = link_status.link_speed * 1000000ULL / 8;
640         if (link_bwg == 0)
641                 return;
642         link_bwg = link_bwg * (update_idx+1) * REORDER_PERIOD_MS;
643         bwg_slave->bwg_left_int = (link_bwg - 1000*load) / link_bwg;
644         bwg_slave->bwg_left_remainder = (link_bwg - 1000*load) % link_bwg;
645 }
646
647 static void
648 bond_ethdev_update_tlb_slave_cb(void *arg)
649 {
650         struct bond_dev_private *internals = arg;
651         struct rte_eth_stats slave_stats;
652         struct bwg_slave bwg_array[RTE_MAX_ETHPORTS];
653         uint8_t slave_count;
654         uint64_t tx_bytes;
655
656         uint8_t update_stats = 0;
657         uint8_t i, slave_id;
658
659         internals->slave_update_idx++;
660
661
662         if (internals->slave_update_idx >= REORDER_PERIOD_MS)
663                 update_stats = 1;
664
665         for (i = 0; i < internals->active_slave_count; i++) {
666                 slave_id = internals->active_slaves[i];
667                 rte_eth_stats_get(slave_id, &slave_stats);
668                 tx_bytes = slave_stats.obytes - tlb_last_obytets[slave_id];
669                 bandwidth_left(slave_id, tx_bytes,
670                                 internals->slave_update_idx, &bwg_array[i]);
671                 bwg_array[i].slave = slave_id;
672
673                 if (update_stats) {
674                         tlb_last_obytets[slave_id] = slave_stats.obytes;
675                 }
676         }
677
678         if (update_stats == 1)
679                 internals->slave_update_idx = 0;
680
681         slave_count = i;
682         qsort(bwg_array, slave_count, sizeof(bwg_array[0]), bandwidth_cmp);
683         for (i = 0; i < slave_count; i++)
684                 internals->tlb_slaves_order[i] = bwg_array[i].slave;
685
686         rte_eal_alarm_set(REORDER_PERIOD_MS * 1000, bond_ethdev_update_tlb_slave_cb,
687                         (struct bond_dev_private *)internals);
688 }
689
690 static uint16_t
691 bond_ethdev_tx_burst_tlb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
692 {
693         struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
694         struct bond_dev_private *internals = bd_tx_q->dev_private;
695
696         struct rte_eth_dev *primary_port =
697                         &rte_eth_devices[internals->primary_port];
698         uint16_t num_tx_total = 0;
699         uint8_t i, j;
700
701         uint8_t num_of_slaves = internals->active_slave_count;
702         uint8_t slaves[RTE_MAX_ETHPORTS];
703
704         struct ether_hdr *ether_hdr;
705         struct ether_addr primary_slave_addr;
706         struct ether_addr active_slave_addr;
707
708         if (num_of_slaves < 1)
709                 return num_tx_total;
710
711         memcpy(slaves, internals->tlb_slaves_order,
712                                 sizeof(internals->tlb_slaves_order[0]) * num_of_slaves);
713
714
715         ether_addr_copy(primary_port->data->mac_addrs, &primary_slave_addr);
716
717         if (nb_pkts > 3) {
718                 for (i = 0; i < 3; i++)
719                         rte_prefetch0(rte_pktmbuf_mtod(bufs[i], void*));
720         }
721
722         for (i = 0; i < num_of_slaves; i++) {
723                 rte_eth_macaddr_get(slaves[i], &active_slave_addr);
724                 for (j = num_tx_total; j < nb_pkts; j++) {
725                         if (j + 3 < nb_pkts)
726                                 rte_prefetch0(rte_pktmbuf_mtod(bufs[j+3], void*));
727
728                         ether_hdr = rte_pktmbuf_mtod(bufs[j], struct ether_hdr *);
729                         if (is_same_ether_addr(&ether_hdr->s_addr, &primary_slave_addr))
730                                 ether_addr_copy(&active_slave_addr, &ether_hdr->s_addr);
731 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
732                                         mode6_debug("TX IPv4:", ether_hdr, slaves[i], &burstnumberTX);
733 #endif
734                 }
735
736                 num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
737                                 bufs + num_tx_total, nb_pkts - num_tx_total);
738
739                 if (num_tx_total == nb_pkts)
740                         break;
741         }
742
743         return num_tx_total;
744 }
745
746 void
747 bond_tlb_disable(struct bond_dev_private *internals)
748 {
749         rte_eal_alarm_cancel(bond_ethdev_update_tlb_slave_cb, internals);
750 }
751
752 void
753 bond_tlb_enable(struct bond_dev_private *internals)
754 {
755         bond_ethdev_update_tlb_slave_cb(internals);
756 }
757
758 static uint16_t
759 bond_ethdev_tx_burst_alb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
760 {
761         struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
762         struct bond_dev_private *internals = bd_tx_q->dev_private;
763
764         struct ether_hdr *eth_h;
765         uint16_t ether_type, offset;
766
767         struct client_data *client_info;
768
769         /*
770          * We create transmit buffers for every slave and one additional to send
771          * through tlb. In worst case every packet will be send on one port.
772          */
773         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS + 1][nb_pkts];
774         uint16_t slave_bufs_pkts[RTE_MAX_ETHPORTS + 1] = { 0 };
775
776         /*
777          * We create separate transmit buffers for update packets as they wont be
778          * counted in num_tx_total.
779          */
780         struct rte_mbuf *update_bufs[RTE_MAX_ETHPORTS][ALB_HASH_TABLE_SIZE];
781         uint16_t update_bufs_pkts[RTE_MAX_ETHPORTS] = { 0 };
782
783         struct rte_mbuf *upd_pkt;
784         size_t pkt_size;
785
786         uint16_t num_send, num_not_send = 0;
787         uint16_t num_tx_total = 0;
788         uint8_t slave_idx;
789
790         int i, j;
791
792         /* Search tx buffer for ARP packets and forward them to alb */
793         for (i = 0; i < nb_pkts; i++) {
794                 eth_h = rte_pktmbuf_mtod(bufs[i], struct ether_hdr *);
795                 ether_type = eth_h->ether_type;
796                 offset = get_vlan_offset(eth_h, &ether_type);
797
798                 if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_ARP)) {
799                         slave_idx = bond_mode_alb_arp_xmit(eth_h, offset, internals);
800
801                         /* Change src mac in eth header */
802                         rte_eth_macaddr_get(slave_idx, &eth_h->s_addr);
803
804                         /* Add packet to slave tx buffer */
805                         slave_bufs[slave_idx][slave_bufs_pkts[slave_idx]] = bufs[i];
806                         slave_bufs_pkts[slave_idx]++;
807                 } else {
808                         /* If packet is not ARP, send it with TLB policy */
809                         slave_bufs[RTE_MAX_ETHPORTS][slave_bufs_pkts[RTE_MAX_ETHPORTS]] =
810                                         bufs[i];
811                         slave_bufs_pkts[RTE_MAX_ETHPORTS]++;
812                 }
813         }
814
815         /* Update connected client ARP tables */
816         if (internals->mode6.ntt) {
817                 for (i = 0; i < ALB_HASH_TABLE_SIZE; i++) {
818                         client_info = &internals->mode6.client_table[i];
819
820                         if (client_info->in_use) {
821                                 /* Allocate new packet to send ARP update on current slave */
822                                 upd_pkt = rte_pktmbuf_alloc(internals->mode6.mempool);
823                                 if (upd_pkt == NULL) {
824                                         RTE_LOG(ERR, PMD, "Failed to allocate ARP packet from pool\n");
825                                         continue;
826                                 }
827                                 pkt_size = sizeof(struct ether_hdr) + sizeof(struct arp_hdr)
828                                                 + client_info->vlan_count * sizeof(struct vlan_hdr);
829                                 upd_pkt->data_len = pkt_size;
830                                 upd_pkt->pkt_len = pkt_size;
831
832                                 slave_idx = bond_mode_alb_arp_upd(client_info, upd_pkt,
833                                                 internals);
834
835                                 /* Add packet to update tx buffer */
836                                 update_bufs[slave_idx][update_bufs_pkts[slave_idx]] = upd_pkt;
837                                 update_bufs_pkts[slave_idx]++;
838                         }
839                 }
840                 internals->mode6.ntt = 0;
841         }
842
843         /* Send ARP packets on proper slaves */
844         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
845                 if (slave_bufs_pkts[i] > 0) {
846                         num_send = rte_eth_tx_burst(i, bd_tx_q->queue_id,
847                                         slave_bufs[i], slave_bufs_pkts[i]);
848                         for (j = 0; j < slave_bufs_pkts[i] - num_send; j++) {
849                                 bufs[nb_pkts - 1 - num_not_send - j] =
850                                                 slave_bufs[i][nb_pkts - 1 - j];
851                         }
852
853                         num_tx_total += num_send;
854                         num_not_send += slave_bufs_pkts[i] - num_send;
855
856 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
857         /* Print TX stats including update packets */
858                         for (j = 0; j < slave_bufs_pkts[i]; j++) {
859                                 eth_h = rte_pktmbuf_mtod(slave_bufs[i][j], struct ether_hdr *);
860                                 mode6_debug("TX ARP:", eth_h, i, &burstnumberTX);
861                         }
862 #endif
863                 }
864         }
865
866         /* Send update packets on proper slaves */
867         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
868                 if (update_bufs_pkts[i] > 0) {
869                         num_send = rte_eth_tx_burst(i, bd_tx_q->queue_id, update_bufs[i],
870                                         update_bufs_pkts[i]);
871                         for (j = num_send; j < update_bufs_pkts[i]; j++) {
872                                 rte_pktmbuf_free(update_bufs[i][j]);
873                         }
874 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
875                         for (j = 0; j < update_bufs_pkts[i]; j++) {
876                                 eth_h = rte_pktmbuf_mtod(update_bufs[i][j], struct ether_hdr *);
877                                 mode6_debug("TX ARPupd:", eth_h, i, &burstnumberTX);
878                         }
879 #endif
880                 }
881         }
882
883         /* Send non-ARP packets using tlb policy */
884         if (slave_bufs_pkts[RTE_MAX_ETHPORTS] > 0) {
885                 num_send = bond_ethdev_tx_burst_tlb(queue,
886                                 slave_bufs[RTE_MAX_ETHPORTS],
887                                 slave_bufs_pkts[RTE_MAX_ETHPORTS]);
888
889                 for (j = 0; j < slave_bufs_pkts[RTE_MAX_ETHPORTS]; j++) {
890                         bufs[nb_pkts - 1 - num_not_send - j] =
891                                         slave_bufs[RTE_MAX_ETHPORTS][nb_pkts - 1 - j];
892                 }
893
894                 num_tx_total += num_send;
895                 num_not_send += slave_bufs_pkts[RTE_MAX_ETHPORTS] - num_send;
896         }
897
898         return num_tx_total;
899 }
900
901 static uint16_t
902 bond_ethdev_tx_burst_balance(void *queue, struct rte_mbuf **bufs,
903                 uint16_t nb_pkts)
904 {
905         struct bond_dev_private *internals;
906         struct bond_tx_queue *bd_tx_q;
907
908         uint8_t num_of_slaves;
909         uint8_t slaves[RTE_MAX_ETHPORTS];
910
911         uint16_t num_tx_total = 0, num_tx_slave = 0, tx_fail_total = 0;
912
913         int i, op_slave_id;
914
915         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
916         uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
917
918         bd_tx_q = (struct bond_tx_queue *)queue;
919         internals = bd_tx_q->dev_private;
920
921         /* Copy slave list to protect against slave up/down changes during tx
922          * bursting */
923         num_of_slaves = internals->active_slave_count;
924         memcpy(slaves, internals->active_slaves,
925                         sizeof(internals->active_slaves[0]) * num_of_slaves);
926
927         if (num_of_slaves < 1)
928                 return num_tx_total;
929
930         /* Populate slaves mbuf with the packets which are to be sent on it  */
931         for (i = 0; i < nb_pkts; i++) {
932                 /* Select output slave using hash based on xmit policy */
933                 op_slave_id = internals->xmit_hash(bufs[i], num_of_slaves);
934
935                 /* Populate slave mbuf arrays with mbufs for that slave */
936                 slave_bufs[op_slave_id][slave_nb_pkts[op_slave_id]++] = bufs[i];
937         }
938
939         /* Send packet burst on each slave device */
940         for (i = 0; i < num_of_slaves; i++) {
941                 if (slave_nb_pkts[i] > 0) {
942                         num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
943                                         slave_bufs[i], slave_nb_pkts[i]);
944
945                         /* if tx burst fails move packets to end of bufs */
946                         if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
947                                 int slave_tx_fail_count = slave_nb_pkts[i] - num_tx_slave;
948
949                                 tx_fail_total += slave_tx_fail_count;
950                                 memcpy(&bufs[nb_pkts - tx_fail_total],
951                                                 &slave_bufs[i][num_tx_slave],
952                                                 slave_tx_fail_count * sizeof(bufs[0]));
953                         }
954
955                         num_tx_total += num_tx_slave;
956                 }
957         }
958
959         return num_tx_total;
960 }
961
962 static uint16_t
963 bond_ethdev_tx_burst_8023ad(void *queue, struct rte_mbuf **bufs,
964                 uint16_t nb_pkts)
965 {
966         struct bond_dev_private *internals;
967         struct bond_tx_queue *bd_tx_q;
968
969         uint8_t num_of_slaves;
970         uint8_t slaves[RTE_MAX_ETHPORTS];
971          /* positions in slaves, not ID */
972         uint8_t distributing_offsets[RTE_MAX_ETHPORTS];
973         uint8_t distributing_count;
974
975         uint16_t num_tx_slave, num_tx_total = 0, num_tx_fail_total = 0;
976         uint16_t i, j, op_slave_idx;
977         const uint16_t buffs_size = nb_pkts + BOND_MODE_8023AX_SLAVE_TX_PKTS + 1;
978
979         /* Allocate additional packets in case 8023AD mode. */
980         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][buffs_size];
981         void *slow_pkts[BOND_MODE_8023AX_SLAVE_TX_PKTS] = { NULL };
982
983         /* Total amount of packets in slave_bufs */
984         uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
985         /* Slow packets placed in each slave */
986         uint8_t slave_slow_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
987
988         bd_tx_q = (struct bond_tx_queue *)queue;
989         internals = bd_tx_q->dev_private;
990
991         /* Copy slave list to protect against slave up/down changes during tx
992          * bursting */
993         num_of_slaves = internals->active_slave_count;
994         if (num_of_slaves < 1)
995                 return num_tx_total;
996
997         memcpy(slaves, internals->active_slaves, sizeof(slaves[0]) * num_of_slaves);
998
999         distributing_count = 0;
1000         for (i = 0; i < num_of_slaves; i++) {
1001                 struct port *port = &mode_8023ad_ports[slaves[i]];
1002
1003                 slave_slow_nb_pkts[i] = rte_ring_dequeue_burst(port->tx_ring,
1004                                 slow_pkts, BOND_MODE_8023AX_SLAVE_TX_PKTS);
1005                 slave_nb_pkts[i] = slave_slow_nb_pkts[i];
1006
1007                 for (j = 0; j < slave_slow_nb_pkts[i]; j++)
1008                         slave_bufs[i][j] = slow_pkts[j];
1009
1010                 if (ACTOR_STATE(port, DISTRIBUTING))
1011                         distributing_offsets[distributing_count++] = i;
1012         }
1013
1014         if (likely(distributing_count > 0)) {
1015                 /* Populate slaves mbuf with the packets which are to be sent on it */
1016                 for (i = 0; i < nb_pkts; i++) {
1017                         /* Select output slave using hash based on xmit policy */
1018                         op_slave_idx = internals->xmit_hash(bufs[i], distributing_count);
1019
1020                         /* Populate slave mbuf arrays with mbufs for that slave. Use only
1021                          * slaves that are currently distributing. */
1022                         uint8_t slave_offset = distributing_offsets[op_slave_idx];
1023                         slave_bufs[slave_offset][slave_nb_pkts[slave_offset]] = bufs[i];
1024                         slave_nb_pkts[slave_offset]++;
1025                 }
1026         }
1027
1028         /* Send packet burst on each slave device */
1029         for (i = 0; i < num_of_slaves; i++) {
1030                 if (slave_nb_pkts[i] == 0)
1031                         continue;
1032
1033                 num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
1034                                 slave_bufs[i], slave_nb_pkts[i]);
1035
1036                 /* If tx burst fails drop slow packets */
1037                 for ( ; num_tx_slave < slave_slow_nb_pkts[i]; num_tx_slave++)
1038                         rte_pktmbuf_free(slave_bufs[i][num_tx_slave]);
1039
1040                 num_tx_total += num_tx_slave - slave_slow_nb_pkts[i];
1041                 num_tx_fail_total += slave_nb_pkts[i] - num_tx_slave;
1042
1043                 /* If tx burst fails move packets to end of bufs */
1044                 if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
1045                         uint16_t j = nb_pkts - num_tx_fail_total;
1046                         for ( ; num_tx_slave < slave_nb_pkts[i]; j++, num_tx_slave++)
1047                                 bufs[j] = slave_bufs[i][num_tx_slave];
1048                 }
1049         }
1050
1051         return num_tx_total;
1052 }
1053
1054 static uint16_t
1055 bond_ethdev_tx_burst_broadcast(void *queue, struct rte_mbuf **bufs,
1056                 uint16_t nb_pkts)
1057 {
1058         struct bond_dev_private *internals;
1059         struct bond_tx_queue *bd_tx_q;
1060
1061         uint8_t tx_failed_flag = 0, num_of_slaves;
1062         uint8_t slaves[RTE_MAX_ETHPORTS];
1063
1064         uint16_t max_nb_of_tx_pkts = 0;
1065
1066         int slave_tx_total[RTE_MAX_ETHPORTS];
1067         int i, most_successful_tx_slave = -1;
1068
1069         bd_tx_q = (struct bond_tx_queue *)queue;
1070         internals = bd_tx_q->dev_private;
1071
1072         /* Copy slave list to protect against slave up/down changes during tx
1073          * bursting */
1074         num_of_slaves = internals->active_slave_count;
1075         memcpy(slaves, internals->active_slaves,
1076                         sizeof(internals->active_slaves[0]) * num_of_slaves);
1077
1078         if (num_of_slaves < 1)
1079                 return 0;
1080
1081         /* Increment reference count on mbufs */
1082         for (i = 0; i < nb_pkts; i++)
1083                 rte_mbuf_refcnt_update(bufs[i], num_of_slaves - 1);
1084
1085         /* Transmit burst on each active slave */
1086         for (i = 0; i < num_of_slaves; i++) {
1087                 slave_tx_total[i] = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
1088                                         bufs, nb_pkts);
1089
1090                 if (unlikely(slave_tx_total[i] < nb_pkts))
1091                         tx_failed_flag = 1;
1092
1093                 /* record the value and slave index for the slave which transmits the
1094                  * maximum number of packets */
1095                 if (slave_tx_total[i] > max_nb_of_tx_pkts) {
1096                         max_nb_of_tx_pkts = slave_tx_total[i];
1097                         most_successful_tx_slave = i;
1098                 }
1099         }
1100
1101         /* if slaves fail to transmit packets from burst, the calling application
1102          * is not expected to know about multiple references to packets so we must
1103          * handle failures of all packets except those of the most successful slave
1104          */
1105         if (unlikely(tx_failed_flag))
1106                 for (i = 0; i < num_of_slaves; i++)
1107                         if (i != most_successful_tx_slave)
1108                                 while (slave_tx_total[i] < nb_pkts)
1109                                         rte_pktmbuf_free(bufs[slave_tx_total[i]++]);
1110
1111         return max_nb_of_tx_pkts;
1112 }
1113
1114 void
1115 link_properties_set(struct rte_eth_dev *bonded_eth_dev,
1116                 struct rte_eth_link *slave_dev_link)
1117 {
1118         struct rte_eth_link *bonded_dev_link = &bonded_eth_dev->data->dev_link;
1119         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
1120
1121         if (slave_dev_link->link_status &&
1122                 bonded_eth_dev->data->dev_started) {
1123                 bonded_dev_link->link_duplex = slave_dev_link->link_duplex;
1124                 bonded_dev_link->link_speed = slave_dev_link->link_speed;
1125
1126                 internals->link_props_set = 1;
1127         }
1128 }
1129
1130 void
1131 link_properties_reset(struct rte_eth_dev *bonded_eth_dev)
1132 {
1133         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
1134
1135         memset(&(bonded_eth_dev->data->dev_link), 0,
1136                         sizeof(bonded_eth_dev->data->dev_link));
1137
1138         internals->link_props_set = 0;
1139 }
1140
1141 int
1142 link_properties_valid(struct rte_eth_link *bonded_dev_link,
1143                 struct rte_eth_link *slave_dev_link)
1144 {
1145         if (bonded_dev_link->link_duplex != slave_dev_link->link_duplex ||
1146                 bonded_dev_link->link_speed !=  slave_dev_link->link_speed)
1147                 return -1;
1148
1149         return 0;
1150 }
1151
1152 int
1153 mac_address_get(struct rte_eth_dev *eth_dev, struct ether_addr *dst_mac_addr)
1154 {
1155         struct ether_addr *mac_addr;
1156
1157         if (eth_dev == NULL) {
1158                 RTE_LOG(ERR, PMD, "%s: NULL pointer eth_dev specified\n", __func__);
1159                 return -1;
1160         }
1161
1162         if (dst_mac_addr == NULL) {
1163                 RTE_LOG(ERR, PMD, "%s: NULL pointer MAC specified\n", __func__);
1164                 return -1;
1165         }
1166
1167         mac_addr = eth_dev->data->mac_addrs;
1168
1169         ether_addr_copy(mac_addr, dst_mac_addr);
1170         return 0;
1171 }
1172
1173 int
1174 mac_address_set(struct rte_eth_dev *eth_dev, struct ether_addr *new_mac_addr)
1175 {
1176         struct ether_addr *mac_addr;
1177
1178         if (eth_dev == NULL) {
1179                 RTE_BOND_LOG(ERR, "NULL pointer eth_dev specified");
1180                 return -1;
1181         }
1182
1183         if (new_mac_addr == NULL) {
1184                 RTE_BOND_LOG(ERR, "NULL pointer MAC specified");
1185                 return -1;
1186         }
1187
1188         mac_addr = eth_dev->data->mac_addrs;
1189
1190         /* If new MAC is different to current MAC then update */
1191         if (memcmp(mac_addr, new_mac_addr, sizeof(*mac_addr)) != 0)
1192                 memcpy(mac_addr, new_mac_addr, sizeof(*mac_addr));
1193
1194         return 0;
1195 }
1196
1197 int
1198 mac_address_slaves_update(struct rte_eth_dev *bonded_eth_dev)
1199 {
1200         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
1201         int i;
1202
1203         /* Update slave devices MAC addresses */
1204         if (internals->slave_count < 1)
1205                 return -1;
1206
1207         switch (internals->mode) {
1208         case BONDING_MODE_ROUND_ROBIN:
1209         case BONDING_MODE_BALANCE:
1210         case BONDING_MODE_BROADCAST:
1211                 for (i = 0; i < internals->slave_count; i++) {
1212                         if (mac_address_set(&rte_eth_devices[internals->slaves[i].port_id],
1213                                         bonded_eth_dev->data->mac_addrs)) {
1214                                 RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
1215                                                 internals->slaves[i].port_id);
1216                                 return -1;
1217                         }
1218                 }
1219                 break;
1220         case BONDING_MODE_8023AD:
1221                 bond_mode_8023ad_mac_address_update(bonded_eth_dev);
1222                 break;
1223         case BONDING_MODE_ACTIVE_BACKUP:
1224         case BONDING_MODE_TLB:
1225         case BONDING_MODE_ALB:
1226         default:
1227                 for (i = 0; i < internals->slave_count; i++) {
1228                         if (internals->slaves[i].port_id ==
1229                                         internals->current_primary_port) {
1230                                 if (mac_address_set(&rte_eth_devices[internals->primary_port],
1231                                                 bonded_eth_dev->data->mac_addrs)) {
1232                                         RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
1233                                                         internals->current_primary_port);
1234                                         return -1;
1235                                 }
1236                         } else {
1237                                 if (mac_address_set(
1238                                                 &rte_eth_devices[internals->slaves[i].port_id],
1239                                                 &internals->slaves[i].persisted_mac_addr)) {
1240                                         RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
1241                                                         internals->slaves[i].port_id);
1242                                         return -1;
1243                                 }
1244                         }
1245                 }
1246         }
1247
1248         return 0;
1249 }
1250
1251 int
1252 bond_ethdev_mode_set(struct rte_eth_dev *eth_dev, int mode)
1253 {
1254         struct bond_dev_private *internals;
1255
1256         internals = eth_dev->data->dev_private;
1257
1258         switch (mode) {
1259         case BONDING_MODE_ROUND_ROBIN:
1260                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_round_robin;
1261                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
1262                 break;
1263         case BONDING_MODE_ACTIVE_BACKUP:
1264                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_active_backup;
1265                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_active_backup;
1266                 break;
1267         case BONDING_MODE_BALANCE:
1268                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_balance;
1269                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
1270                 break;
1271         case BONDING_MODE_BROADCAST:
1272                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_broadcast;
1273                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
1274                 break;
1275         case BONDING_MODE_8023AD:
1276                 if (bond_mode_8023ad_enable(eth_dev) != 0)
1277                         return -1;
1278
1279                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_8023ad;
1280                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_8023ad;
1281                 RTE_LOG(WARNING, PMD,
1282                                 "Using mode 4, it is necessary to do TX burst and RX burst "
1283                                 "at least every 100ms.\n");
1284                 break;
1285         case BONDING_MODE_TLB:
1286                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_tlb;
1287                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_active_backup;
1288                 break;
1289         case BONDING_MODE_ALB:
1290                 if (bond_mode_alb_enable(eth_dev) != 0)
1291                         return -1;
1292
1293                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_alb;
1294                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_alb;
1295                 break;
1296         default:
1297                 return -1;
1298         }
1299
1300         internals->mode = mode;
1301
1302         return 0;
1303 }
1304
1305 int
1306 slave_configure(struct rte_eth_dev *bonded_eth_dev,
1307                 struct rte_eth_dev *slave_eth_dev)
1308 {
1309         struct bond_rx_queue *bd_rx_q;
1310         struct bond_tx_queue *bd_tx_q;
1311
1312         int errval, q_id;
1313
1314         /* Stop slave */
1315         rte_eth_dev_stop(slave_eth_dev->data->port_id);
1316
1317         /* Enable interrupts on slave device if supported */
1318         if (slave_eth_dev->driver->pci_drv.drv_flags & RTE_PCI_DRV_INTR_LSC)
1319                 slave_eth_dev->data->dev_conf.intr_conf.lsc = 1;
1320
1321         /* Configure device */
1322         errval = rte_eth_dev_configure(slave_eth_dev->data->port_id,
1323                         bonded_eth_dev->data->nb_rx_queues,
1324                         bonded_eth_dev->data->nb_tx_queues,
1325                         &(slave_eth_dev->data->dev_conf));
1326         if (errval != 0) {
1327                 RTE_BOND_LOG(ERR, "Cannot configure slave device: port %u , err (%d)",
1328                                 slave_eth_dev->data->port_id, errval);
1329                 return errval;
1330         }
1331
1332         /* Setup Rx Queues */
1333         for (q_id = 0; q_id < bonded_eth_dev->data->nb_rx_queues; q_id++) {
1334                 bd_rx_q = (struct bond_rx_queue *)bonded_eth_dev->data->rx_queues[q_id];
1335
1336                 errval = rte_eth_rx_queue_setup(slave_eth_dev->data->port_id, q_id,
1337                                 bd_rx_q->nb_rx_desc,
1338                                 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
1339                                 &(bd_rx_q->rx_conf), bd_rx_q->mb_pool);
1340                 if (errval != 0) {
1341                         RTE_BOND_LOG(ERR,
1342                                         "rte_eth_rx_queue_setup: port=%d queue_id %d, err (%d)",
1343                                         slave_eth_dev->data->port_id, q_id, errval);
1344                         return errval;
1345                 }
1346         }
1347
1348         /* Setup Tx Queues */
1349         for (q_id = 0; q_id < bonded_eth_dev->data->nb_tx_queues; q_id++) {
1350                 bd_tx_q = (struct bond_tx_queue *)bonded_eth_dev->data->tx_queues[q_id];
1351
1352                 errval = rte_eth_tx_queue_setup(slave_eth_dev->data->port_id, q_id,
1353                                 bd_tx_q->nb_tx_desc,
1354                                 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
1355                                 &bd_tx_q->tx_conf);
1356                 if (errval != 0) {
1357                         RTE_BOND_LOG(ERR,
1358                                         "rte_eth_tx_queue_setup: port=%d queue_id %d, err (%d)",
1359                                         slave_eth_dev->data->port_id, q_id, errval);
1360                         return errval;
1361                 }
1362         }
1363
1364         /* Start device */
1365         errval = rte_eth_dev_start(slave_eth_dev->data->port_id);
1366         if (errval != 0) {
1367                 RTE_BOND_LOG(ERR, "rte_eth_dev_start: port=%u, err (%d)",
1368                                 slave_eth_dev->data->port_id, errval);
1369                 return -1;
1370         }
1371
1372         return 0;
1373 }
1374
1375 void
1376 slave_remove(struct bond_dev_private *internals,
1377                 struct rte_eth_dev *slave_eth_dev)
1378 {
1379         uint8_t i;
1380
1381         for (i = 0; i < internals->slave_count; i++)
1382                 if (internals->slaves[i].port_id ==
1383                                 slave_eth_dev->data->port_id)
1384                         break;
1385
1386         if (i < (internals->slave_count - 1))
1387                 memmove(&internals->slaves[i], &internals->slaves[i + 1],
1388                                 sizeof(internals->slaves[0]) *
1389                                 (internals->slave_count - i - 1));
1390
1391         internals->slave_count--;
1392 }
1393
1394 static void
1395 bond_ethdev_slave_link_status_change_monitor(void *cb_arg);
1396
1397 void
1398 slave_add(struct bond_dev_private *internals,
1399                 struct rte_eth_dev *slave_eth_dev)
1400 {
1401         struct bond_slave_details *slave_details =
1402                         &internals->slaves[internals->slave_count];
1403
1404         slave_details->port_id = slave_eth_dev->data->port_id;
1405         slave_details->last_link_status = 0;
1406
1407         /* If slave device doesn't support interrupts then we need to enabled
1408          * polling to monitor link status */
1409         if (!(slave_eth_dev->pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
1410                 slave_details->link_status_poll_enabled = 1;
1411
1412                 if (!internals->link_status_polling_enabled) {
1413                         internals->link_status_polling_enabled = 1;
1414
1415                         rte_eal_alarm_set(internals->link_status_polling_interval_ms * 1000,
1416                                         bond_ethdev_slave_link_status_change_monitor,
1417                                         (void *)&rte_eth_devices[internals->port_id]);
1418                 }
1419         }
1420
1421         slave_details->link_status_wait_to_complete = 0;
1422         /* clean tlb_last_obytes when adding port for bonding device */
1423         memcpy(&(slave_details->persisted_mac_addr), slave_eth_dev->data->mac_addrs,
1424                         sizeof(struct ether_addr));
1425 }
1426
1427 void
1428 bond_ethdev_primary_set(struct bond_dev_private *internals,
1429                 uint8_t slave_port_id)
1430 {
1431         int i;
1432
1433         if (internals->active_slave_count < 1)
1434                 internals->current_primary_port = slave_port_id;
1435         else
1436                 /* Search bonded device slave ports for new proposed primary port */
1437                 for (i = 0; i < internals->active_slave_count; i++) {
1438                         if (internals->active_slaves[i] == slave_port_id)
1439                                 internals->current_primary_port = slave_port_id;
1440                 }
1441 }
1442
1443 static void
1444 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev);
1445
1446 static int
1447 bond_ethdev_start(struct rte_eth_dev *eth_dev)
1448 {
1449         struct bond_dev_private *internals;
1450         int i;
1451
1452         /* slave eth dev will be started by bonded device */
1453         if (valid_bonded_ethdev(eth_dev)) {
1454                 RTE_BOND_LOG(ERR, "User tried to explicitly start a slave eth_dev (%d)",
1455                                 eth_dev->data->port_id);
1456                 return -1;
1457         }
1458
1459         eth_dev->data->dev_link.link_status = 0;
1460         eth_dev->data->dev_started = 1;
1461
1462         internals = eth_dev->data->dev_private;
1463
1464         if (internals->slave_count == 0) {
1465                 RTE_BOND_LOG(ERR, "Cannot start port since there are no slave devices");
1466                 return -1;
1467         }
1468
1469         if (internals->user_defined_mac == 0) {
1470                 struct ether_addr *new_mac_addr = NULL;
1471
1472                 for (i = 0; i < internals->slave_count; i++)
1473                         if (internals->slaves[i].port_id == internals->primary_port)
1474                                 new_mac_addr = &internals->slaves[i].persisted_mac_addr;
1475
1476                 if (new_mac_addr == NULL)
1477                         return -1;
1478
1479                 if (mac_address_set(eth_dev, new_mac_addr) != 0) {
1480                         RTE_BOND_LOG(ERR, "bonded port (%d) failed to update MAC address",
1481                                         eth_dev->data->port_id);
1482                         return -1;
1483                 }
1484         }
1485
1486         /* Update all slave devices MACs*/
1487         if (mac_address_slaves_update(eth_dev) != 0)
1488                 return -1;
1489
1490         /* If bonded device is configure in promiscuous mode then re-apply config */
1491         if (internals->promiscuous_en)
1492                 bond_ethdev_promiscuous_enable(eth_dev);
1493
1494         /* Reconfigure each slave device if starting bonded device */
1495         for (i = 0; i < internals->slave_count; i++) {
1496                 if (slave_configure(eth_dev,
1497                                 &(rte_eth_devices[internals->slaves[i].port_id])) != 0) {
1498                         RTE_BOND_LOG(ERR,
1499                                         "bonded port (%d) failed to reconfigure slave device (%d)",
1500                                         eth_dev->data->port_id, internals->slaves[i].port_id);
1501                         return -1;
1502                 }
1503         }
1504
1505         if (internals->user_defined_primary_port)
1506                 bond_ethdev_primary_set(internals, internals->primary_port);
1507
1508         if (internals->mode == BONDING_MODE_8023AD)
1509                 bond_mode_8023ad_start(eth_dev);
1510
1511         if (internals->mode == BONDING_MODE_TLB ||
1512                         internals->mode == BONDING_MODE_ALB)
1513                 bond_tlb_enable(internals);
1514
1515         return 0;
1516 }
1517
1518 static void
1519 bond_ethdev_stop(struct rte_eth_dev *eth_dev)
1520 {
1521         struct bond_dev_private *internals = eth_dev->data->dev_private;
1522         uint8_t i;
1523
1524         if (internals->mode == BONDING_MODE_8023AD) {
1525                 struct port *port;
1526                 void *pkt = NULL;
1527
1528                 bond_mode_8023ad_stop(eth_dev);
1529
1530                 /* Discard all messages to/from mode 4 state machines */
1531                 for (i = 0; i < internals->slave_count; i++) {
1532                         port = &mode_8023ad_ports[internals->slaves[i].port_id];
1533
1534                         RTE_VERIFY(port->rx_ring != NULL);
1535                         while (rte_ring_dequeue(port->rx_ring, &pkt) != -ENOENT)
1536                                 rte_pktmbuf_free(pkt);
1537
1538                         RTE_VERIFY(port->tx_ring != NULL);
1539                         while (rte_ring_dequeue(port->tx_ring, &pkt) != -ENOENT)
1540                                 rte_pktmbuf_free(pkt);
1541                 }
1542         }
1543
1544         if (internals->mode == BONDING_MODE_TLB ||
1545                         internals->mode == BONDING_MODE_ALB) {
1546                 bond_tlb_disable(internals);
1547                 for (i = 0; i < internals->active_slave_count; i++)
1548                         tlb_last_obytets[internals->active_slaves[i]] = 0;
1549         }
1550
1551         internals->active_slave_count = 0;
1552         internals->link_status_polling_enabled = 0;
1553
1554         eth_dev->data->dev_link.link_status = 0;
1555         eth_dev->data->dev_started = 0;
1556 }
1557
1558 static void
1559 bond_ethdev_close(struct rte_eth_dev *dev __rte_unused)
1560 {
1561 }
1562
1563 /* forward declaration */
1564 static int bond_ethdev_configure(struct rte_eth_dev *dev);
1565
1566 static void
1567 bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1568 {
1569         struct bond_dev_private *internals = dev->data->dev_private;
1570
1571         dev_info->driver_name = driver_name;
1572         dev_info->max_mac_addrs = 1;
1573
1574         dev_info->max_rx_pktlen = (uint32_t)2048;
1575
1576         dev_info->max_rx_queues = (uint16_t)128;
1577         dev_info->max_tx_queues = (uint16_t)512;
1578
1579         dev_info->min_rx_bufsize = 0;
1580         dev_info->pci_dev = dev->pci_dev;
1581
1582         dev_info->rx_offload_capa = internals->rx_offload_capa;
1583         dev_info->tx_offload_capa = internals->tx_offload_capa;
1584 }
1585
1586 static int
1587 bond_ethdev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
1588                 uint16_t nb_rx_desc, unsigned int socket_id __rte_unused,
1589                 const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool)
1590 {
1591         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)
1592                         rte_zmalloc_socket(NULL, sizeof(struct bond_rx_queue),
1593                                         0, dev->pci_dev->numa_node);
1594         if (bd_rx_q == NULL)
1595                 return -1;
1596
1597         bd_rx_q->queue_id = rx_queue_id;
1598         bd_rx_q->dev_private = dev->data->dev_private;
1599
1600         bd_rx_q->nb_rx_desc = nb_rx_desc;
1601
1602         memcpy(&(bd_rx_q->rx_conf), rx_conf, sizeof(struct rte_eth_rxconf));
1603         bd_rx_q->mb_pool = mb_pool;
1604
1605         dev->data->rx_queues[rx_queue_id] = bd_rx_q;
1606
1607         return 0;
1608 }
1609
1610 static int
1611 bond_ethdev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
1612                 uint16_t nb_tx_desc, unsigned int socket_id __rte_unused,
1613                 const struct rte_eth_txconf *tx_conf)
1614 {
1615         struct bond_tx_queue *bd_tx_q  = (struct bond_tx_queue *)
1616                         rte_zmalloc_socket(NULL, sizeof(struct bond_tx_queue),
1617                                         0, dev->pci_dev->numa_node);
1618
1619         if (bd_tx_q == NULL)
1620                 return -1;
1621
1622         bd_tx_q->queue_id = tx_queue_id;
1623         bd_tx_q->dev_private = dev->data->dev_private;
1624
1625         bd_tx_q->nb_tx_desc = nb_tx_desc;
1626         memcpy(&(bd_tx_q->tx_conf), tx_conf, sizeof(bd_tx_q->tx_conf));
1627
1628         dev->data->tx_queues[tx_queue_id] = bd_tx_q;
1629
1630         return 0;
1631 }
1632
1633 static void
1634 bond_ethdev_rx_queue_release(void *queue)
1635 {
1636         if (queue == NULL)
1637                 return;
1638
1639         rte_free(queue);
1640 }
1641
1642 static void
1643 bond_ethdev_tx_queue_release(void *queue)
1644 {
1645         if (queue == NULL)
1646                 return;
1647
1648         rte_free(queue);
1649 }
1650
1651 static void
1652 bond_ethdev_slave_link_status_change_monitor(void *cb_arg)
1653 {
1654         struct rte_eth_dev *bonded_ethdev, *slave_ethdev;
1655         struct bond_dev_private *internals;
1656
1657         /* Default value for polling slave found is true as we don't want to
1658          * disable the polling thread if we cannot get the lock */
1659         int i, polling_slave_found = 1;
1660
1661         if (cb_arg == NULL)
1662                 return;
1663
1664         bonded_ethdev = (struct rte_eth_dev *)cb_arg;
1665         internals = (struct bond_dev_private *)bonded_ethdev->data->dev_private;
1666
1667         if (!bonded_ethdev->data->dev_started ||
1668                 !internals->link_status_polling_enabled)
1669                 return;
1670
1671         /* If device is currently being configured then don't check slaves link
1672          * status, wait until next period */
1673         if (rte_spinlock_trylock(&internals->lock)) {
1674                 if (internals->slave_count > 0)
1675                         polling_slave_found = 0;
1676
1677                 for (i = 0; i < internals->slave_count; i++) {
1678                         if (!internals->slaves[i].link_status_poll_enabled)
1679                                 continue;
1680
1681                         slave_ethdev = &rte_eth_devices[internals->slaves[i].port_id];
1682                         polling_slave_found = 1;
1683
1684                         /* Update slave link status */
1685                         (*slave_ethdev->dev_ops->link_update)(slave_ethdev,
1686                                         internals->slaves[i].link_status_wait_to_complete);
1687
1688                         /* if link status has changed since last checked then call lsc
1689                          * event callback */
1690                         if (slave_ethdev->data->dev_link.link_status !=
1691                                         internals->slaves[i].last_link_status) {
1692                                 internals->slaves[i].last_link_status =
1693                                                 slave_ethdev->data->dev_link.link_status;
1694
1695                                 bond_ethdev_lsc_event_callback(internals->slaves[i].port_id,
1696                                                 RTE_ETH_EVENT_INTR_LSC,
1697                                                 &bonded_ethdev->data->port_id);
1698                         }
1699                 }
1700                 rte_spinlock_unlock(&internals->lock);
1701         }
1702
1703         if (polling_slave_found)
1704                 /* Set alarm to continue monitoring link status of slave ethdev's */
1705                 rte_eal_alarm_set(internals->link_status_polling_interval_ms * 1000,
1706                                 bond_ethdev_slave_link_status_change_monitor, cb_arg);
1707 }
1708
1709 static int
1710 bond_ethdev_link_update(struct rte_eth_dev *bonded_eth_dev,
1711                 int wait_to_complete)
1712 {
1713         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
1714
1715         if (!bonded_eth_dev->data->dev_started ||
1716                 internals->active_slave_count == 0) {
1717                 bonded_eth_dev->data->dev_link.link_status = 0;
1718                 return 0;
1719         } else {
1720                 struct rte_eth_dev *slave_eth_dev;
1721                 int i, link_up = 0;
1722
1723                 for (i = 0; i < internals->active_slave_count; i++) {
1724                         slave_eth_dev = &rte_eth_devices[internals->active_slaves[i]];
1725
1726                         (*slave_eth_dev->dev_ops->link_update)(slave_eth_dev,
1727                                         wait_to_complete);
1728                         if (slave_eth_dev->data->dev_link.link_status == 1) {
1729                                 link_up = 1;
1730                                 break;
1731                         }
1732                 }
1733
1734                 bonded_eth_dev->data->dev_link.link_status = link_up;
1735         }
1736
1737         return 0;
1738 }
1739
1740 static void
1741 bond_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1742 {
1743         struct bond_dev_private *internals = dev->data->dev_private;
1744         struct rte_eth_stats slave_stats;
1745
1746         int i;
1747
1748         /* clear bonded stats before populating from slaves */
1749         memset(stats, 0, sizeof(*stats));
1750
1751         for (i = 0; i < internals->slave_count; i++) {
1752                 rte_eth_stats_get(internals->slaves[i].port_id, &slave_stats);
1753
1754                 stats->ipackets += slave_stats.ipackets;
1755                 stats->opackets += slave_stats.opackets;
1756                 stats->ibytes += slave_stats.ibytes;
1757                 stats->obytes += slave_stats.obytes;
1758                 stats->ierrors += slave_stats.ierrors;
1759                 stats->oerrors += slave_stats.oerrors;
1760                 stats->imcasts += slave_stats.imcasts;
1761                 stats->rx_nombuf += slave_stats.rx_nombuf;
1762                 stats->fdirmatch += slave_stats.fdirmatch;
1763                 stats->fdirmiss += slave_stats.fdirmiss;
1764                 stats->tx_pause_xon += slave_stats.tx_pause_xon;
1765                 stats->rx_pause_xon += slave_stats.rx_pause_xon;
1766                 stats->tx_pause_xoff += slave_stats.tx_pause_xoff;
1767                 stats->rx_pause_xoff += slave_stats.rx_pause_xoff;
1768         }
1769 }
1770
1771 static void
1772 bond_ethdev_stats_reset(struct rte_eth_dev *dev)
1773 {
1774         struct bond_dev_private *internals = dev->data->dev_private;
1775         int i;
1776
1777         for (i = 0; i < internals->slave_count; i++)
1778                 rte_eth_stats_reset(internals->slaves[i].port_id);
1779 }
1780
1781 static void
1782 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev)
1783 {
1784         struct bond_dev_private *internals = eth_dev->data->dev_private;
1785         int i;
1786
1787         internals->promiscuous_en = 1;
1788
1789         switch (internals->mode) {
1790         /* Promiscuous mode is propagated to all slaves */
1791         case BONDING_MODE_ROUND_ROBIN:
1792         case BONDING_MODE_BALANCE:
1793         case BONDING_MODE_BROADCAST:
1794                 for (i = 0; i < internals->slave_count; i++)
1795                         rte_eth_promiscuous_enable(internals->slaves[i].port_id);
1796                 break;
1797         /* In mode4 promiscus mode is managed when slave is added/removed */
1798         case BONDING_MODE_8023AD:
1799                 break;
1800         /* Promiscuous mode is propagated only to primary slave */
1801         case BONDING_MODE_ACTIVE_BACKUP:
1802         case BONDING_MODE_TLB:
1803         case BONDING_MODE_ALB:
1804         default:
1805                 rte_eth_promiscuous_enable(internals->current_primary_port);
1806         }
1807 }
1808
1809 static void
1810 bond_ethdev_promiscuous_disable(struct rte_eth_dev *dev)
1811 {
1812         struct bond_dev_private *internals = dev->data->dev_private;
1813         int i;
1814
1815         internals->promiscuous_en = 0;
1816
1817         switch (internals->mode) {
1818         /* Promiscuous mode is propagated to all slaves */
1819         case BONDING_MODE_ROUND_ROBIN:
1820         case BONDING_MODE_BALANCE:
1821         case BONDING_MODE_BROADCAST:
1822                 for (i = 0; i < internals->slave_count; i++)
1823                         rte_eth_promiscuous_disable(internals->slaves[i].port_id);
1824                 break;
1825         /* In mode4 promiscus mode is set managed when slave is added/removed */
1826         case BONDING_MODE_8023AD:
1827                 break;
1828         /* Promiscuous mode is propagated only to primary slave */
1829         case BONDING_MODE_ACTIVE_BACKUP:
1830         case BONDING_MODE_TLB:
1831         case BONDING_MODE_ALB:
1832         default:
1833                 rte_eth_promiscuous_disable(internals->current_primary_port);
1834         }
1835 }
1836
1837 static void
1838 bond_ethdev_delayed_lsc_propagation(void *arg)
1839 {
1840         if (arg == NULL)
1841                 return;
1842
1843         _rte_eth_dev_callback_process((struct rte_eth_dev *)arg,
1844                         RTE_ETH_EVENT_INTR_LSC);
1845 }
1846
1847 void
1848 bond_ethdev_lsc_event_callback(uint8_t port_id, enum rte_eth_event_type type,
1849                 void *param)
1850 {
1851         struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
1852         struct bond_dev_private *internals;
1853         struct rte_eth_link link;
1854
1855         int i, valid_slave = 0;
1856         uint8_t active_pos;
1857         uint8_t lsc_flag = 0;
1858
1859         if (type != RTE_ETH_EVENT_INTR_LSC || param == NULL)
1860                 return;
1861
1862         bonded_eth_dev = &rte_eth_devices[*(uint8_t *)param];
1863         slave_eth_dev = &rte_eth_devices[port_id];
1864
1865         if (valid_bonded_ethdev(bonded_eth_dev))
1866                 return;
1867
1868         internals = bonded_eth_dev->data->dev_private;
1869
1870         /* If the device isn't started don't handle interrupts */
1871         if (!bonded_eth_dev->data->dev_started)
1872                 return;
1873
1874         /* verify that port_id is a valid slave of bonded port */
1875         for (i = 0; i < internals->slave_count; i++) {
1876                 if (internals->slaves[i].port_id == port_id) {
1877                         valid_slave = 1;
1878                         break;
1879                 }
1880         }
1881
1882         if (!valid_slave)
1883                 return;
1884
1885         /* Search for port in active port list */
1886         active_pos = find_slave_by_id(internals->active_slaves,
1887                         internals->active_slave_count, port_id);
1888
1889         rte_eth_link_get_nowait(port_id, &link);
1890         if (link.link_status) {
1891                 if (active_pos < internals->active_slave_count)
1892                         return;
1893
1894                 /* if no active slave ports then set this port to be primary port */
1895                 if (internals->active_slave_count < 1) {
1896                         /* If first active slave, then change link status */
1897                         bonded_eth_dev->data->dev_link.link_status = 1;
1898                         internals->current_primary_port = port_id;
1899                         lsc_flag = 1;
1900
1901                         mac_address_slaves_update(bonded_eth_dev);
1902
1903                         /* Inherit eth dev link properties from first active slave */
1904                         link_properties_set(bonded_eth_dev,
1905                                         &(slave_eth_dev->data->dev_link));
1906                 }
1907
1908                 activate_slave(bonded_eth_dev, port_id);
1909
1910                 /* If user has defined the primary port then default to using it */
1911                 if (internals->user_defined_primary_port &&
1912                                 internals->primary_port == port_id)
1913                         bond_ethdev_primary_set(internals, port_id);
1914         } else {
1915                 if (active_pos == internals->active_slave_count)
1916                         return;
1917
1918                 /* Remove from active slave list */
1919                 deactivate_slave(bonded_eth_dev, port_id);
1920
1921                 /* No active slaves, change link status to down and reset other
1922                  * link properties */
1923                 if (internals->active_slave_count < 1) {
1924                         lsc_flag = 1;
1925                         bonded_eth_dev->data->dev_link.link_status = 0;
1926
1927                         link_properties_reset(bonded_eth_dev);
1928                 }
1929
1930                 /* Update primary id, take first active slave from list or if none
1931                  * available set to -1 */
1932                 if (port_id == internals->current_primary_port) {
1933                         if (internals->active_slave_count > 0)
1934                                 bond_ethdev_primary_set(internals,
1935                                                 internals->active_slaves[0]);
1936                         else
1937                                 internals->current_primary_port = internals->primary_port;
1938                 }
1939         }
1940
1941         if (lsc_flag) {
1942                 /* Cancel any possible outstanding interrupts if delays are enabled */
1943                 if (internals->link_up_delay_ms > 0 ||
1944                         internals->link_down_delay_ms > 0)
1945                         rte_eal_alarm_cancel(bond_ethdev_delayed_lsc_propagation,
1946                                         bonded_eth_dev);
1947
1948                 if (bonded_eth_dev->data->dev_link.link_status) {
1949                         if (internals->link_up_delay_ms > 0)
1950                                 rte_eal_alarm_set(internals->link_up_delay_ms * 1000,
1951                                                 bond_ethdev_delayed_lsc_propagation,
1952                                                 (void *)bonded_eth_dev);
1953                         else
1954                                 _rte_eth_dev_callback_process(bonded_eth_dev,
1955                                                 RTE_ETH_EVENT_INTR_LSC);
1956
1957                 } else {
1958                         if (internals->link_down_delay_ms > 0)
1959                                 rte_eal_alarm_set(internals->link_down_delay_ms * 1000,
1960                                                 bond_ethdev_delayed_lsc_propagation,
1961                                                 (void *)bonded_eth_dev);
1962                         else
1963                                 _rte_eth_dev_callback_process(bonded_eth_dev,
1964                                                 RTE_ETH_EVENT_INTR_LSC);
1965                 }
1966         }
1967 }
1968
1969 struct eth_dev_ops default_dev_ops = {
1970                 .dev_start = bond_ethdev_start,
1971                 .dev_stop = bond_ethdev_stop,
1972                 .dev_close = bond_ethdev_close,
1973                 .dev_configure = bond_ethdev_configure,
1974                 .dev_infos_get = bond_ethdev_info,
1975                 .rx_queue_setup = bond_ethdev_rx_queue_setup,
1976                 .tx_queue_setup = bond_ethdev_tx_queue_setup,
1977                 .rx_queue_release = bond_ethdev_rx_queue_release,
1978                 .tx_queue_release = bond_ethdev_tx_queue_release,
1979                 .link_update = bond_ethdev_link_update,
1980                 .stats_get = bond_ethdev_stats_get,
1981                 .stats_reset = bond_ethdev_stats_reset,
1982                 .promiscuous_enable = bond_ethdev_promiscuous_enable,
1983                 .promiscuous_disable = bond_ethdev_promiscuous_disable
1984 };
1985
1986 static int
1987 bond_init(const char *name, const char *params)
1988 {
1989         struct bond_dev_private *internals;
1990         struct rte_kvargs *kvlist;
1991         uint8_t bonding_mode, socket_id;
1992         int  arg_count, port_id;
1993
1994         RTE_LOG(INFO, EAL, "Initializing pmd_bond for %s\n", name);
1995
1996         kvlist = rte_kvargs_parse(params, pmd_bond_init_valid_arguments);
1997         if (kvlist == NULL)
1998                 return -1;
1999
2000         /* Parse link bonding mode */
2001         if (rte_kvargs_count(kvlist, PMD_BOND_MODE_KVARG) == 1) {
2002                 if (rte_kvargs_process(kvlist, PMD_BOND_MODE_KVARG,
2003                                 &bond_ethdev_parse_slave_mode_kvarg,
2004                                 &bonding_mode) != 0) {
2005                         RTE_LOG(ERR, EAL, "Invalid mode for bonded device %s\n",
2006                                         name);
2007                         goto parse_error;
2008                 }
2009         } else {
2010                 RTE_LOG(ERR, EAL, "Mode must be specified only once for bonded "
2011                                 "device %s\n", name);
2012                 goto parse_error;
2013         }
2014
2015         /* Parse socket id to create bonding device on */
2016         arg_count = rte_kvargs_count(kvlist, PMD_BOND_SOCKET_ID_KVARG);
2017         if (arg_count == 1) {
2018                 if (rte_kvargs_process(kvlist, PMD_BOND_SOCKET_ID_KVARG,
2019                                 &bond_ethdev_parse_socket_id_kvarg, &socket_id)
2020                                 != 0) {
2021                         RTE_LOG(ERR, EAL, "Invalid socket Id specified for "
2022                                         "bonded device %s\n", name);
2023                         goto parse_error;
2024                 }
2025         } else if (arg_count > 1) {
2026                 RTE_LOG(ERR, EAL, "Socket Id can be specified only once for "
2027                                 "bonded device %s\n", name);
2028                 goto parse_error;
2029         } else {
2030                 socket_id = rte_socket_id();
2031         }
2032
2033         /* Create link bonding eth device */
2034         port_id = rte_eth_bond_create(name, bonding_mode, socket_id);
2035         if (port_id < 0) {
2036                 RTE_LOG(ERR, EAL, "Failed to create socket %s in mode %u on "
2037                                 "socket %u.\n", name, bonding_mode, socket_id);
2038                 goto parse_error;
2039         }
2040         internals = rte_eth_devices[port_id].data->dev_private;
2041         internals->kvlist = kvlist;
2042
2043         RTE_LOG(INFO, EAL, "Create bonded device %s on port %d in mode %u on "
2044                         "socket %u.\n", name, port_id, bonding_mode, socket_id);
2045         return 0;
2046
2047 parse_error:
2048         rte_kvargs_free(kvlist);
2049
2050         return -1;
2051 }
2052
2053 /* this part will resolve the slave portids after all the other pdev and vdev
2054  * have been allocated */
2055 static int
2056 bond_ethdev_configure(struct rte_eth_dev *dev)
2057 {
2058         char *name = dev->data->name;
2059         struct bond_dev_private *internals = dev->data->dev_private;
2060         struct rte_kvargs *kvlist = internals->kvlist;
2061         int arg_count, port_id = dev - rte_eth_devices;
2062
2063         /*
2064          * if no kvlist, it means that this bonded device has been created
2065          * through the bonding api.
2066          */
2067         if (!kvlist)
2068                 return 0;
2069
2070         /* Parse MAC address for bonded device */
2071         arg_count = rte_kvargs_count(kvlist, PMD_BOND_MAC_ADDR_KVARG);
2072         if (arg_count == 1) {
2073                 struct ether_addr bond_mac;
2074
2075                 if (rte_kvargs_process(kvlist, PMD_BOND_MAC_ADDR_KVARG,
2076                                 &bond_ethdev_parse_bond_mac_addr_kvarg, &bond_mac) < 0) {
2077                         RTE_LOG(INFO, EAL, "Invalid mac address for bonded device %s\n",
2078                                         name);
2079                         return -1;
2080                 }
2081
2082                 /* Set MAC address */
2083                 if (rte_eth_bond_mac_address_set(port_id, &bond_mac) != 0) {
2084                         RTE_LOG(ERR, EAL,
2085                                         "Failed to set mac address on bonded device %s\n",
2086                                         name);
2087                         return -1;
2088                 }
2089         } else if (arg_count > 1) {
2090                 RTE_LOG(ERR, EAL,
2091                                 "MAC address can be specified only once for bonded device %s\n",
2092                                 name);
2093                 return -1;
2094         }
2095
2096         /* Parse/set balance mode transmit policy */
2097         arg_count = rte_kvargs_count(kvlist, PMD_BOND_XMIT_POLICY_KVARG);
2098         if (arg_count == 1) {
2099                 uint8_t xmit_policy;
2100
2101                 if (rte_kvargs_process(kvlist, PMD_BOND_XMIT_POLICY_KVARG,
2102                                 &bond_ethdev_parse_balance_xmit_policy_kvarg, &xmit_policy) !=
2103                                                 0) {
2104                         RTE_LOG(INFO, EAL,
2105                                         "Invalid xmit policy specified for bonded device %s\n",
2106                                         name);
2107                         return -1;
2108                 }
2109
2110                 /* Set balance mode transmit policy*/
2111                 if (rte_eth_bond_xmit_policy_set(port_id, xmit_policy) != 0) {
2112                         RTE_LOG(ERR, EAL,
2113                                         "Failed to set balance xmit policy on bonded device %s\n",
2114                                         name);
2115                         return -1;
2116                 }
2117         } else if (arg_count > 1) {
2118                 RTE_LOG(ERR, EAL,
2119                                 "Transmit policy can be specified only once for bonded device"
2120                                 " %s\n", name);
2121                 return -1;
2122         }
2123
2124         /* Parse/add slave ports to bonded device */
2125         if (rte_kvargs_count(kvlist, PMD_BOND_SLAVE_PORT_KVARG) > 0) {
2126                 struct bond_ethdev_slave_ports slave_ports;
2127                 unsigned i;
2128
2129                 memset(&slave_ports, 0, sizeof(slave_ports));
2130
2131                 if (rte_kvargs_process(kvlist, PMD_BOND_SLAVE_PORT_KVARG,
2132                                 &bond_ethdev_parse_slave_port_kvarg, &slave_ports) != 0) {
2133                         RTE_LOG(ERR, EAL,
2134                                         "Failed to parse slave ports for bonded device %s\n",
2135                                         name);
2136                         return -1;
2137                 }
2138
2139                 for (i = 0; i < slave_ports.slave_count; i++) {
2140                         if (rte_eth_bond_slave_add(port_id, slave_ports.slaves[i]) != 0) {
2141                                 RTE_LOG(ERR, EAL,
2142                                                 "Failed to add port %d as slave to bonded device %s\n",
2143                                                 slave_ports.slaves[i], name);
2144                         }
2145                 }
2146
2147         } else {
2148                 RTE_LOG(INFO, EAL, "No slaves specified for bonded device %s\n", name);
2149                 return -1;
2150         }
2151
2152         /* Parse/set primary slave port id*/
2153         arg_count = rte_kvargs_count(kvlist, PMD_BOND_PRIMARY_SLAVE_KVARG);
2154         if (arg_count == 1) {
2155                 uint8_t primary_slave_port_id;
2156
2157                 if (rte_kvargs_process(kvlist,
2158                                 PMD_BOND_PRIMARY_SLAVE_KVARG,
2159                                 &bond_ethdev_parse_primary_slave_port_id_kvarg,
2160                                 &primary_slave_port_id) < 0) {
2161                         RTE_LOG(INFO, EAL,
2162                                         "Invalid primary slave port id specified for bonded device"
2163                                         " %s\n", name);
2164                         return -1;
2165                 }
2166
2167                 /* Set balance mode transmit policy*/
2168                 if (rte_eth_bond_primary_set(port_id, (uint8_t)primary_slave_port_id)
2169                                 != 0) {
2170                         RTE_LOG(ERR, EAL,
2171                                         "Failed to set primary slave port %d on bonded device %s\n",
2172                                         primary_slave_port_id, name);
2173                         return -1;
2174                 }
2175         } else if (arg_count > 1) {
2176                 RTE_LOG(INFO, EAL,
2177                                 "Primary slave can be specified only once for bonded device"
2178                                 " %s\n", name);
2179                 return -1;
2180         }
2181
2182         /* Parse link status monitor polling interval */
2183         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LSC_POLL_PERIOD_KVARG);
2184         if (arg_count == 1) {
2185                 uint32_t lsc_poll_interval_ms;
2186
2187                 if (rte_kvargs_process(kvlist,
2188                                 PMD_BOND_LSC_POLL_PERIOD_KVARG,
2189                                 &bond_ethdev_parse_time_ms_kvarg,
2190                                 &lsc_poll_interval_ms) < 0) {
2191                         RTE_LOG(INFO, EAL,
2192                                         "Invalid lsc polling interval value specified for bonded"
2193                                         " device %s\n", name);
2194                         return -1;
2195                 }
2196
2197                 if (rte_eth_bond_link_monitoring_set(port_id, lsc_poll_interval_ms)
2198                                 != 0) {
2199                         RTE_LOG(ERR, EAL,
2200                                         "Failed to set lsc monitor polling interval (%u ms) on"
2201                                         " bonded device %s\n", lsc_poll_interval_ms, name);
2202                         return -1;
2203                 }
2204         } else if (arg_count > 1) {
2205                 RTE_LOG(INFO, EAL,
2206                                 "LSC polling interval can be specified only once for bonded"
2207                                 " device %s\n", name);
2208                 return -1;
2209         }
2210
2211         /* Parse link up interrupt propagation delay */
2212         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LINK_UP_PROP_DELAY_KVARG);
2213         if (arg_count == 1) {
2214                 uint32_t link_up_delay_ms;
2215
2216                 if (rte_kvargs_process(kvlist,
2217                                 PMD_BOND_LINK_UP_PROP_DELAY_KVARG,
2218                                 &bond_ethdev_parse_time_ms_kvarg,
2219                                 &link_up_delay_ms) < 0) {
2220                         RTE_LOG(INFO, EAL,
2221                                         "Invalid link up propagation delay value specified for"
2222                                         " bonded device %s\n", name);
2223                         return -1;
2224                 }
2225
2226                 /* Set balance mode transmit policy*/
2227                 if (rte_eth_bond_link_up_prop_delay_set(port_id, link_up_delay_ms)
2228                                 != 0) {
2229                         RTE_LOG(ERR, EAL,
2230                                         "Failed to set link up propagation delay (%u ms) on bonded"
2231                                         " device %s\n", link_up_delay_ms, name);
2232                         return -1;
2233                 }
2234         } else if (arg_count > 1) {
2235                 RTE_LOG(INFO, EAL,
2236                                 "Link up propagation delay can be specified only once for"
2237                                 " bonded device %s\n", name);
2238                 return -1;
2239         }
2240
2241         /* Parse link down interrupt propagation delay */
2242         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG);
2243         if (arg_count == 1) {
2244                 uint32_t link_down_delay_ms;
2245
2246                 if (rte_kvargs_process(kvlist,
2247                                 PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG,
2248                                 &bond_ethdev_parse_time_ms_kvarg,
2249                                 &link_down_delay_ms) < 0) {
2250                         RTE_LOG(INFO, EAL,
2251                                         "Invalid link down propagation delay value specified for"
2252                                         " bonded device %s\n", name);
2253                         return -1;
2254                 }
2255
2256                 /* Set balance mode transmit policy*/
2257                 if (rte_eth_bond_link_down_prop_delay_set(port_id, link_down_delay_ms)
2258                                 != 0) {
2259                         RTE_LOG(ERR, EAL,
2260                                         "Failed to set link down propagation delay (%u ms) on"
2261                                         " bonded device %s\n", link_down_delay_ms, name);
2262                         return -1;
2263                 }
2264         } else if (arg_count > 1) {
2265                 RTE_LOG(INFO, EAL,
2266                                 "Link down propagation delay can be specified only once for"
2267                                 " bonded device %s\n", name);
2268                 return -1;
2269         }
2270
2271         return 0;
2272 }
2273
2274 static struct rte_driver bond_drv = {
2275         .name = "eth_bond",
2276         .type = PMD_VDEV,
2277         .init = bond_init,
2278 };
2279
2280 PMD_REGISTER_DRIVER(bond_drv);