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