bb4a53793d07046eed1df805d4008b36869178da
[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 <rte_mbuf.h>
35 #include <rte_malloc.h>
36 #include <rte_ethdev.h>
37 #include <rte_tcp.h>
38 #include <rte_udp.h>
39 #include <rte_ip.h>
40 #include <rte_devargs.h>
41 #include <rte_kvargs.h>
42 #include <rte_dev.h>
43 #include <rte_alarm.h>
44 #include <rte_cycles.h>
45
46 #include "rte_eth_bond.h"
47 #include "rte_eth_bond_private.h"
48 #include "rte_eth_bond_8023ad_private.h"
49
50 #define REORDER_PERIOD_MS 10
51 /* Table for statistics in mode 5 TLB */
52 static uint64_t tlb_last_obytets[RTE_MAX_ETHPORTS];
53
54 static uint16_t
55 bond_ethdev_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
56 {
57         struct bond_dev_private *internals;
58
59         uint16_t num_rx_slave = 0;
60         uint16_t num_rx_total = 0;
61
62         int i;
63
64         /* Cast to structure, containing bonded device's port id and queue id */
65         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
66
67         internals = bd_rx_q->dev_private;
68
69
70         for (i = 0; i < internals->active_slave_count && nb_pkts; i++) {
71                 /* Offset of pointer to *bufs increases as packets are received
72                  * from other slaves */
73                 num_rx_slave = rte_eth_rx_burst(internals->active_slaves[i],
74                                 bd_rx_q->queue_id, bufs + num_rx_total, nb_pkts);
75                 if (num_rx_slave) {
76                         num_rx_total += num_rx_slave;
77                         nb_pkts -= num_rx_slave;
78                 }
79         }
80
81         return num_rx_total;
82 }
83
84 static uint16_t
85 bond_ethdev_rx_burst_active_backup(void *queue, struct rte_mbuf **bufs,
86                 uint16_t nb_pkts)
87 {
88         struct bond_dev_private *internals;
89
90         /* Cast to structure, containing bonded device's port id and queue id */
91         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
92
93         internals = bd_rx_q->dev_private;
94
95         return rte_eth_rx_burst(internals->current_primary_port,
96                         bd_rx_q->queue_id, bufs, nb_pkts);
97 }
98
99 static uint16_t
100 bond_ethdev_rx_burst_8023ad(void *queue, struct rte_mbuf **bufs,
101                 uint16_t nb_pkts)
102 {
103         /* Cast to structure, containing bonded device's port id and queue id */
104         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
105         struct bond_dev_private *internals = bd_rx_q->dev_private;
106         struct ether_addr bond_mac;
107
108         struct ether_hdr *hdr;
109
110         const uint16_t ether_type_slow_be = rte_be_to_cpu_16(ETHER_TYPE_SLOW);
111         uint16_t num_rx_total = 0;      /* Total number of received packets */
112         uint8_t slaves[RTE_MAX_ETHPORTS];
113         uint8_t slave_count;
114
115         uint8_t collecting;  /* current slave collecting status */
116         const uint8_t promisc = internals->promiscuous_en;
117         uint8_t i, j, k;
118
119         rte_eth_macaddr_get(internals->port_id, &bond_mac);
120         /* Copy slave list to protect against slave up/down changes during tx
121          * bursting */
122         slave_count = internals->active_slave_count;
123         memcpy(slaves, internals->active_slaves,
124                         sizeof(internals->active_slaves[0]) * slave_count);
125
126         for (i = 0; i < slave_count && num_rx_total < nb_pkts; i++) {
127                 j = num_rx_total;
128                 collecting = ACTOR_STATE(&mode_8023ad_ports[slaves[i]], COLLECTING);
129
130                 /* Read packets from this slave */
131                 num_rx_total += rte_eth_rx_burst(slaves[i], bd_rx_q->queue_id,
132                                 &bufs[num_rx_total], nb_pkts - num_rx_total);
133
134                 for (k = j; k < 2 && k < num_rx_total; k++)
135                         rte_prefetch0(rte_pktmbuf_mtod(bufs[k], void *));
136
137                 /* Handle slow protocol packets. */
138                 while (j < num_rx_total) {
139                         if (j + 3 < num_rx_total)
140                                 rte_prefetch0(rte_pktmbuf_mtod(bufs[j + 3], void *));
141
142                         hdr = rte_pktmbuf_mtod(bufs[j], struct ether_hdr *);
143                         /* Remove packet from array if it is slow packet or slave is not
144                          * in collecting state or bondign interface is not in promiscus
145                          * mode and packet address does not match. */
146                         if (unlikely(hdr->ether_type == ether_type_slow_be ||
147                                 !collecting || (!promisc &&
148                                         !is_same_ether_addr(&bond_mac, &hdr->d_addr)))) {
149
150                                 if (hdr->ether_type == ether_type_slow_be) {
151                                         bond_mode_8023ad_handle_slow_pkt(internals, slaves[i],
152                                                 bufs[j]);
153                                 } else
154                                         rte_pktmbuf_free(bufs[j]);
155
156                                 /* Packet is managed by mode 4 or dropped, shift the array */
157                                 num_rx_total--;
158                                 if (j < num_rx_total) {
159                                         memmove(&bufs[j], &bufs[j + 1], sizeof(bufs[0]) *
160                                                 (num_rx_total - j));
161                                 }
162                         } else
163                                 j++;
164                 }
165         }
166
167         return num_rx_total;
168 }
169
170 static uint16_t
171 bond_ethdev_tx_burst_round_robin(void *queue, struct rte_mbuf **bufs,
172                 uint16_t nb_pkts)
173 {
174         struct bond_dev_private *internals;
175         struct bond_tx_queue *bd_tx_q;
176
177         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
178         uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
179
180         uint8_t num_of_slaves;
181         uint8_t slaves[RTE_MAX_ETHPORTS];
182
183         uint16_t num_tx_total = 0, num_tx_slave;
184
185         static int slave_idx = 0;
186         int i, cslave_idx = 0, tx_fail_total = 0;
187
188         bd_tx_q = (struct bond_tx_queue *)queue;
189         internals = bd_tx_q->dev_private;
190
191         /* Copy slave list to protect against slave up/down changes during tx
192          * bursting */
193         num_of_slaves = internals->active_slave_count;
194         memcpy(slaves, internals->active_slaves,
195                         sizeof(internals->active_slaves[0]) * num_of_slaves);
196
197         if (num_of_slaves < 1)
198                 return num_tx_total;
199
200         /* Populate slaves mbuf with which packets are to be sent on it  */
201         for (i = 0; i < nb_pkts; i++) {
202                 cslave_idx = (slave_idx + i) % num_of_slaves;
203                 slave_bufs[cslave_idx][(slave_nb_pkts[cslave_idx])++] = bufs[i];
204         }
205
206         /* increment current slave index so the next call to tx burst starts on the
207          * next slave */
208         slave_idx = ++cslave_idx;
209
210         /* Send packet burst on each slave device */
211         for (i = 0; i < num_of_slaves; i++) {
212                 if (slave_nb_pkts[i] > 0) {
213                         num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
214                                         slave_bufs[i], slave_nb_pkts[i]);
215
216                         /* if tx burst fails move packets to end of bufs */
217                         if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
218                                 int tx_fail_slave = slave_nb_pkts[i] - num_tx_slave;
219
220                                 tx_fail_total += tx_fail_slave;
221
222                                 memcpy(&bufs[nb_pkts - tx_fail_total],
223                                                 &slave_bufs[i][num_tx_slave],
224                                                 tx_fail_slave * sizeof(bufs[0]));
225                         }
226                         num_tx_total += num_tx_slave;
227                 }
228         }
229
230         return num_tx_total;
231 }
232
233 static uint16_t
234 bond_ethdev_tx_burst_active_backup(void *queue,
235                 struct rte_mbuf **bufs, uint16_t nb_pkts)
236 {
237         struct bond_dev_private *internals;
238         struct bond_tx_queue *bd_tx_q;
239
240         bd_tx_q = (struct bond_tx_queue *)queue;
241         internals = bd_tx_q->dev_private;
242
243         if (internals->active_slave_count < 1)
244                 return 0;
245
246         return rte_eth_tx_burst(internals->current_primary_port, bd_tx_q->queue_id,
247                         bufs, nb_pkts);
248 }
249
250 static inline uint16_t
251 ether_hash(struct ether_hdr *eth_hdr)
252 {
253         uint16_t *word_src_addr = (uint16_t *)eth_hdr->s_addr.addr_bytes;
254         uint16_t *word_dst_addr = (uint16_t *)eth_hdr->d_addr.addr_bytes;
255
256         return (word_src_addr[0] ^ word_dst_addr[0]) ^
257                         (word_src_addr[1] ^ word_dst_addr[1]) ^
258                         (word_src_addr[2] ^ word_dst_addr[2]);
259 }
260
261 static inline uint32_t
262 ipv4_hash(struct ipv4_hdr *ipv4_hdr)
263 {
264         return (ipv4_hdr->src_addr ^ ipv4_hdr->dst_addr);
265 }
266
267 static inline uint32_t
268 ipv6_hash(struct ipv6_hdr *ipv6_hdr)
269 {
270         uint32_t *word_src_addr = (uint32_t *)&(ipv6_hdr->src_addr[0]);
271         uint32_t *word_dst_addr = (uint32_t *)&(ipv6_hdr->dst_addr[0]);
272
273         return (word_src_addr[0] ^ word_dst_addr[0]) ^
274                         (word_src_addr[1] ^ word_dst_addr[1]) ^
275                         (word_src_addr[2] ^ word_dst_addr[2]) ^
276                         (word_src_addr[3] ^ word_dst_addr[3]);
277 }
278
279 static uint32_t
280 udp_hash(struct udp_hdr *hdr)
281 {
282         return hdr->src_port ^ hdr->dst_port;
283 }
284
285 static inline uint16_t
286 xmit_slave_hash(const struct rte_mbuf *buf, uint8_t slave_count, uint8_t policy)
287 {
288         struct ether_hdr *eth_hdr;
289         struct udp_hdr *udp_hdr;
290         size_t eth_offset = 0;
291         uint32_t hash = 0;
292
293         if (slave_count == 1)
294                 return 0;
295
296         switch (policy) {
297         case BALANCE_XMIT_POLICY_LAYER2:
298                 eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
299
300                 hash = ether_hash(eth_hdr);
301                 hash ^= hash >> 8;
302                 return hash % slave_count;
303
304         case BALANCE_XMIT_POLICY_LAYER23:
305                 eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
306
307                 if (buf->ol_flags & PKT_RX_VLAN_PKT)
308                         eth_offset = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
309                 else
310                         eth_offset = sizeof(struct ether_hdr);
311
312                 if (buf->ol_flags & PKT_RX_IPV4_HDR) {
313                         struct ipv4_hdr *ipv4_hdr;
314                         ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(buf,
315                                         unsigned char *) + eth_offset);
316
317                         hash = ether_hash(eth_hdr) ^ ipv4_hash(ipv4_hdr);
318
319                 } else {
320                         struct ipv6_hdr *ipv6_hdr;
321
322                         ipv6_hdr = (struct ipv6_hdr *)(rte_pktmbuf_mtod(buf,
323                                         unsigned char *) + eth_offset);
324
325                         hash = ether_hash(eth_hdr) ^ ipv6_hash(ipv6_hdr);
326                 }
327                 break;
328
329         case BALANCE_XMIT_POLICY_LAYER34:
330                 if (buf->ol_flags & PKT_RX_VLAN_PKT)
331                         eth_offset = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
332                 else
333                         eth_offset = sizeof(struct ether_hdr);
334
335                 if (buf->ol_flags & PKT_RX_IPV4_HDR) {
336                         struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)
337                                         (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset);
338
339                         if (ipv4_hdr->next_proto_id == IPPROTO_UDP) {
340                                 udp_hdr = (struct udp_hdr *)
341                                                 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset +
342                                                                 sizeof(struct ipv4_hdr));
343                                 hash = ipv4_hash(ipv4_hdr) ^ udp_hash(udp_hdr);
344                         } else {
345                                 hash = ipv4_hash(ipv4_hdr);
346                         }
347                 } else {
348                         struct ipv6_hdr *ipv6_hdr = (struct ipv6_hdr *)
349                                         (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset);
350
351                         if (ipv6_hdr->proto == IPPROTO_UDP) {
352                                 udp_hdr = (struct udp_hdr *)
353                                                 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset +
354                                                                 sizeof(struct ipv6_hdr));
355                                 hash = ipv6_hash(ipv6_hdr) ^ udp_hash(udp_hdr);
356                         } else {
357                                 hash = ipv6_hash(ipv6_hdr);
358                         }
359                 }
360                 break;
361         }
362
363         hash ^= hash >> 16;
364         hash ^= hash >> 8;
365
366         return hash % slave_count;
367 }
368
369 struct bwg_slave {
370         uint64_t bwg_left_int;
371         uint64_t bwg_left_remainder;
372         uint8_t slave;
373 };
374
375 static int
376 bandwidth_cmp(const void *a, const void *b)
377 {
378         const struct bwg_slave *bwg_a = a;
379         const struct bwg_slave *bwg_b = b;
380         int64_t diff = (int64_t)bwg_b->bwg_left_int - (int64_t)bwg_a->bwg_left_int;
381         int64_t diff2 = (int64_t)bwg_b->bwg_left_remainder -
382                         (int64_t)bwg_a->bwg_left_remainder;
383         if (diff > 0)
384                 return 1;
385         else if (diff < 0)
386                 return -1;
387         else if (diff2 > 0)
388                 return 1;
389         else if (diff2 < 0)
390                 return -1;
391         else
392                 return 0;
393 }
394
395 static void
396 bandwidth_left(int port_id, uint64_t load, uint8_t update_idx,
397                 struct bwg_slave *bwg_slave)
398 {
399         struct rte_eth_link link_status;
400
401         rte_eth_link_get(port_id, &link_status);
402         uint64_t link_bwg = link_status.link_speed * 1000000ULL / 8;
403         if (link_bwg == 0)
404                 return;
405         link_bwg = (link_bwg * (update_idx+1) * REORDER_PERIOD_MS);
406         bwg_slave->bwg_left_int = (link_bwg - 1000*load) / link_bwg;
407         bwg_slave->bwg_left_remainder = (link_bwg - 1000*load) % link_bwg;
408 }
409
410 static void
411 bond_ethdev_update_tlb_slave_cb(void *arg)
412 {
413         struct bond_dev_private *internals = arg;
414         struct rte_eth_stats slave_stats;
415         struct bwg_slave bwg_array[RTE_MAX_ETHPORTS];
416         uint8_t slave_count;
417         uint64_t tx_bytes;
418
419         uint8_t update_stats = 0;
420         uint8_t i, slave_id;
421
422         internals->slave_update_idx++;
423
424
425         if (internals->slave_update_idx >= REORDER_PERIOD_MS)
426                 update_stats = 1;
427
428         for (i = 0; i < internals->active_slave_count; i++) {
429                 slave_id = internals->active_slaves[i];
430                 rte_eth_stats_get(slave_id, &slave_stats);
431                 tx_bytes = slave_stats.obytes - tlb_last_obytets[slave_id];
432                 bandwidth_left(slave_id, tx_bytes,
433                                 internals->slave_update_idx, &bwg_array[i]);
434                 bwg_array[i].slave = slave_id;
435
436                 if (update_stats)
437                         tlb_last_obytets[slave_id] = slave_stats.obytes;
438         }
439
440         if (update_stats == 1)
441                 internals->slave_update_idx = 0;
442
443         slave_count = i;
444         qsort(bwg_array, slave_count, sizeof(bwg_array[0]), bandwidth_cmp);
445         for (i = 0; i < slave_count; i++)
446                 internals->active_slaves[i] = bwg_array[i].slave;
447
448         rte_eal_alarm_set(REORDER_PERIOD_MS * 1000, bond_ethdev_update_tlb_slave_cb,
449                         (struct bond_dev_private *)internals);
450 }
451
452 static uint16_t
453 bond_ethdev_tx_burst_tlb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
454 {
455         struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
456         struct bond_dev_private *internals = bd_tx_q->dev_private;
457
458         struct rte_eth_dev *primary_port =
459                         &rte_eth_devices[internals->primary_port];
460         uint16_t num_tx_total = 0;
461         uint8_t i, j;
462
463         uint8_t num_of_slaves = internals->active_slave_count;
464         uint8_t slaves[RTE_MAX_ETHPORTS];
465
466         struct ether_hdr *ether_hdr;
467         struct ether_addr primary_slave_addr;
468         struct ether_addr active_slave_addr;
469
470         if (num_of_slaves < 1)
471                 return num_tx_total;
472
473         memcpy(slaves, internals->active_slaves,
474                                 sizeof(internals->active_slaves[0]) * num_of_slaves);
475
476
477         ether_addr_copy(primary_port->data->mac_addrs, &primary_slave_addr);
478
479         if (nb_pkts > 3) {
480                 for (i = 0; i < 3; i++)
481                         rte_prefetch0(rte_pktmbuf_mtod(bufs[i], void*));
482         }
483
484         for (i = 0; i < num_of_slaves; i++) {
485                 ether_addr_copy(&internals->slaves[slaves[i]].persisted_mac_addr,
486                                 &active_slave_addr);
487
488                 for (j = num_tx_total; j < nb_pkts; j++) {
489                         if (j + 3 < nb_pkts)
490                                 rte_prefetch0(rte_pktmbuf_mtod(bufs[j+3], void*));
491
492                         ether_hdr = rte_pktmbuf_mtod(bufs[j], struct ether_hdr *);
493                         if (is_same_ether_addr(&ether_hdr->s_addr, &primary_slave_addr))
494                                 ether_addr_copy(&active_slave_addr, &ether_hdr->s_addr);
495                 }
496
497                 num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
498                                 bufs + num_tx_total, nb_pkts - num_tx_total);
499
500                 if (num_tx_total == nb_pkts)
501                         break;
502         }
503
504         return num_tx_total;
505 }
506
507 static uint16_t
508 bond_ethdev_tx_burst_balance(void *queue, struct rte_mbuf **bufs,
509                 uint16_t nb_pkts)
510 {
511         struct bond_dev_private *internals;
512         struct bond_tx_queue *bd_tx_q;
513
514         uint8_t num_of_slaves;
515         uint8_t slaves[RTE_MAX_ETHPORTS];
516
517         uint16_t num_tx_total = 0, num_tx_slave = 0, tx_fail_total = 0;
518
519         int i, op_slave_id;
520
521         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
522         uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
523
524         bd_tx_q = (struct bond_tx_queue *)queue;
525         internals = bd_tx_q->dev_private;
526
527         /* Copy slave list to protect against slave up/down changes during tx
528          * bursting */
529         num_of_slaves = internals->active_slave_count;
530         memcpy(slaves, internals->active_slaves,
531                         sizeof(internals->active_slaves[0]) * num_of_slaves);
532
533         if (num_of_slaves < 1)
534                 return num_tx_total;
535
536         /* Populate slaves mbuf with the packets which are to be sent on it  */
537         for (i = 0; i < nb_pkts; i++) {
538                 /* Select output slave using hash based on xmit policy */
539                 op_slave_id = xmit_slave_hash(bufs[i], num_of_slaves,
540                                 internals->balance_xmit_policy);
541
542                 /* Populate slave mbuf arrays with mbufs for that slave */
543                 slave_bufs[op_slave_id][slave_nb_pkts[op_slave_id]++] = bufs[i];
544         }
545
546         /* Send packet burst on each slave device */
547         for (i = 0; i < num_of_slaves; i++) {
548                 if (slave_nb_pkts[i] > 0) {
549                         num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
550                                         slave_bufs[i], slave_nb_pkts[i]);
551
552                         /* if tx burst fails move packets to end of bufs */
553                         if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
554                                 int slave_tx_fail_count = slave_nb_pkts[i] - num_tx_slave;
555
556                                 tx_fail_total += slave_tx_fail_count;
557                                 memcpy(&bufs[nb_pkts - tx_fail_total],
558                                                 &slave_bufs[i][num_tx_slave],
559                                                 slave_tx_fail_count * sizeof(bufs[0]));
560                         }
561
562                         num_tx_total += num_tx_slave;
563                 }
564         }
565
566         return num_tx_total;
567 }
568
569 static uint16_t
570 bond_ethdev_tx_burst_8023ad(void *queue, struct rte_mbuf **bufs,
571                 uint16_t nb_pkts)
572 {
573         struct bond_dev_private *internals;
574         struct bond_tx_queue *bd_tx_q;
575
576         uint8_t num_of_slaves;
577         uint8_t slaves[RTE_MAX_ETHPORTS];
578          /* possitions in slaves, not ID */
579         uint8_t distributing_offsets[RTE_MAX_ETHPORTS];
580         uint8_t distributing_count;
581
582         uint16_t num_tx_slave, num_tx_total = 0, num_tx_fail_total = 0;
583         uint16_t i, j, op_slave_idx;
584         const uint16_t buffs_size = nb_pkts + BOND_MODE_8023AX_SLAVE_TX_PKTS + 1;
585
586         /* Allocate additional packets in case 8023AD mode. */
587         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][buffs_size];
588         void *slow_pkts[BOND_MODE_8023AX_SLAVE_TX_PKTS] = { NULL };
589
590         /* Total amount of packets in slave_bufs */
591         uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
592         /* Slow packets placed in each slave */
593         uint8_t slave_slow_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
594
595         bd_tx_q = (struct bond_tx_queue *)queue;
596         internals = bd_tx_q->dev_private;
597
598         /* Copy slave list to protect against slave up/down changes during tx
599          * bursting */
600         num_of_slaves = internals->active_slave_count;
601         if (num_of_slaves < 1)
602                 return num_tx_total;
603
604         memcpy(slaves, internals->active_slaves, sizeof(slaves[0]) * num_of_slaves);
605
606         distributing_count = 0;
607         for (i = 0; i < num_of_slaves; i++) {
608                 struct port *port = &mode_8023ad_ports[slaves[i]];
609
610                 slave_slow_nb_pkts[i] = rte_ring_dequeue_burst(port->tx_ring,
611                                 slow_pkts, BOND_MODE_8023AX_SLAVE_TX_PKTS);
612                 slave_nb_pkts[i] = slave_slow_nb_pkts[i];
613
614                 for (j = 0; j < slave_slow_nb_pkts[i]; j++)
615                         slave_bufs[i][j] = slow_pkts[j];
616
617                 if (ACTOR_STATE(port, DISTRIBUTING))
618                         distributing_offsets[distributing_count++] = i;
619         }
620
621         if (likely(distributing_count > 0)) {
622                 /* Populate slaves mbuf with the packets which are to be sent on it */
623                 for (i = 0; i < nb_pkts; i++) {
624                         /* Select output slave using hash based on xmit policy */
625                         op_slave_idx = xmit_slave_hash(bufs[i], distributing_count,
626                                         internals->balance_xmit_policy);
627
628                         /* Populate slave mbuf arrays with mbufs for that slave. Use only
629                          * slaves that are currently distributing. */
630                         uint8_t slave_offset = distributing_offsets[op_slave_idx];
631                         slave_bufs[slave_offset][slave_nb_pkts[slave_offset]] = bufs[i];
632                         slave_nb_pkts[slave_offset]++;
633                 }
634         }
635
636         /* Send packet burst on each slave device */
637         for (i = 0; i < num_of_slaves; i++) {
638                 if (slave_nb_pkts[i] == 0)
639                         continue;
640
641                 num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
642                                 slave_bufs[i], slave_nb_pkts[i]);
643
644                 /* If tx burst fails drop slow packets */
645                 for ( ; num_tx_slave < slave_slow_nb_pkts[i]; num_tx_slave++)
646                         rte_pktmbuf_free(slave_bufs[i][num_tx_slave]);
647
648                 num_tx_total += num_tx_slave - slave_slow_nb_pkts[i];
649                 num_tx_fail_total += slave_nb_pkts[i] - num_tx_slave;
650
651                 /* If tx burst fails move packets to end of bufs */
652                 if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
653                         uint16_t j = nb_pkts - num_tx_fail_total;
654                         for ( ; num_tx_slave < slave_nb_pkts[i]; j++, num_tx_slave++)
655                                 bufs[j] = slave_bufs[i][num_tx_slave];
656                 }
657         }
658
659         return num_tx_total;
660 }
661
662 #ifdef RTE_MBUF_REFCNT
663 static uint16_t
664 bond_ethdev_tx_burst_broadcast(void *queue, struct rte_mbuf **bufs,
665                 uint16_t nb_pkts)
666 {
667         struct bond_dev_private *internals;
668         struct bond_tx_queue *bd_tx_q;
669
670         uint8_t tx_failed_flag = 0, num_of_slaves;
671         uint8_t slaves[RTE_MAX_ETHPORTS];
672
673         uint16_t max_nb_of_tx_pkts = 0;
674
675         int slave_tx_total[RTE_MAX_ETHPORTS];
676         int i, most_successful_tx_slave = -1;
677
678         bd_tx_q = (struct bond_tx_queue *)queue;
679         internals = bd_tx_q->dev_private;
680
681         /* Copy slave list to protect against slave up/down changes during tx
682          * bursting */
683         num_of_slaves = internals->active_slave_count;
684         memcpy(slaves, internals->active_slaves,
685                         sizeof(internals->active_slaves[0]) * num_of_slaves);
686
687         if (num_of_slaves < 1)
688                 return 0;
689
690         /* Increment reference count on mbufs */
691         for (i = 0; i < nb_pkts; i++)
692                 rte_mbuf_refcnt_update(bufs[i], num_of_slaves - 1);
693
694         /* Transmit burst on each active slave */
695         for (i = 0; i < num_of_slaves; i++) {
696                 slave_tx_total[i] = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
697                                         bufs, nb_pkts);
698
699                 if (unlikely(slave_tx_total[i] < nb_pkts))
700                         tx_failed_flag = 1;
701
702                 /* record the value and slave index for the slave which transmits the
703                  * maximum number of packets */
704                 if (slave_tx_total[i] > max_nb_of_tx_pkts) {
705                         max_nb_of_tx_pkts = slave_tx_total[i];
706                         most_successful_tx_slave = i;
707                 }
708         }
709
710         /* if slaves fail to transmit packets from burst, the calling application
711          * is not expected to know about multiple references to packets so we must
712          * handle failures of all packets except those of the most successful slave
713          */
714         if (unlikely(tx_failed_flag))
715                 for (i = 0; i < num_of_slaves; i++)
716                         if (i != most_successful_tx_slave)
717                                 while (slave_tx_total[i] < nb_pkts)
718                                         rte_pktmbuf_free(bufs[slave_tx_total[i]++]);
719
720         return max_nb_of_tx_pkts;
721 }
722 #endif
723
724 void
725 link_properties_set(struct rte_eth_dev *bonded_eth_dev,
726                 struct rte_eth_link *slave_dev_link)
727 {
728         struct rte_eth_link *bonded_dev_link = &bonded_eth_dev->data->dev_link;
729         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
730
731         if (slave_dev_link->link_status &&
732                 bonded_eth_dev->data->dev_started) {
733                 bonded_dev_link->link_duplex = slave_dev_link->link_duplex;
734                 bonded_dev_link->link_speed = slave_dev_link->link_speed;
735
736                 internals->link_props_set = 1;
737         }
738 }
739
740 void
741 link_properties_reset(struct rte_eth_dev *bonded_eth_dev)
742 {
743         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
744
745         memset(&(bonded_eth_dev->data->dev_link), 0,
746                         sizeof(bonded_eth_dev->data->dev_link));
747
748         internals->link_props_set = 0;
749 }
750
751 int
752 link_properties_valid(struct rte_eth_link *bonded_dev_link,
753                 struct rte_eth_link *slave_dev_link)
754 {
755         if (bonded_dev_link->link_duplex != slave_dev_link->link_duplex ||
756                 bonded_dev_link->link_speed !=  slave_dev_link->link_speed)
757                 return -1;
758
759         return 0;
760 }
761
762 int
763 mac_address_get(struct rte_eth_dev *eth_dev, struct ether_addr *dst_mac_addr)
764 {
765         struct ether_addr *mac_addr;
766
767         if (eth_dev == NULL) {
768                 RTE_LOG(ERR, PMD, "%s: NULL pointer eth_dev specified\n", __func__);
769                 return -1;
770         }
771
772         if (dst_mac_addr == NULL) {
773                 RTE_LOG(ERR, PMD, "%s: NULL pointer MAC specified\n", __func__);
774                 return -1;
775         }
776
777         mac_addr = eth_dev->data->mac_addrs;
778
779         ether_addr_copy(mac_addr, dst_mac_addr);
780         return 0;
781 }
782
783 int
784 mac_address_set(struct rte_eth_dev *eth_dev, struct ether_addr *new_mac_addr)
785 {
786         struct ether_addr *mac_addr;
787
788         if (eth_dev == NULL) {
789                 RTE_BOND_LOG(ERR, "NULL pointer eth_dev specified");
790                 return -1;
791         }
792
793         if (new_mac_addr == NULL) {
794                 RTE_BOND_LOG(ERR, "NULL pointer MAC specified");
795                 return -1;
796         }
797
798         mac_addr = eth_dev->data->mac_addrs;
799
800         /* If new MAC is different to current MAC then update */
801         if (memcmp(mac_addr, new_mac_addr, sizeof(*mac_addr)) != 0)
802                 memcpy(mac_addr, new_mac_addr, sizeof(*mac_addr));
803
804         return 0;
805 }
806
807 int
808 mac_address_slaves_update(struct rte_eth_dev *bonded_eth_dev)
809 {
810         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
811         int i;
812
813         /* Update slave devices MAC addresses */
814         if (internals->slave_count < 1)
815                 return -1;
816
817         switch (internals->mode) {
818         case BONDING_MODE_ROUND_ROBIN:
819         case BONDING_MODE_BALANCE:
820 #ifdef RTE_MBUF_REFCNT
821         case BONDING_MODE_BROADCAST:
822 #endif
823                 for (i = 0; i < internals->slave_count; i++) {
824                         if (mac_address_set(&rte_eth_devices[internals->slaves[i].port_id],
825                                         bonded_eth_dev->data->mac_addrs)) {
826                                 RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
827                                                 internals->slaves[i].port_id);
828                                 return -1;
829                         }
830                 }
831                 break;
832         case BONDING_MODE_8023AD:
833                 bond_mode_8023ad_mac_address_update(bonded_eth_dev);
834                 break;
835         case BONDING_MODE_ACTIVE_BACKUP:
836         case BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING:
837         default:
838                 for (i = 0; i < internals->slave_count; i++) {
839                         if (internals->slaves[i].port_id ==
840                                         internals->current_primary_port) {
841                                 if (mac_address_set(&rte_eth_devices[internals->primary_port],
842                                                 bonded_eth_dev->data->mac_addrs)) {
843                                         RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
844                                                         internals->current_primary_port);
845                                         return -1;
846                                 }
847                         } else {
848                                 if (mac_address_set(
849                                                 &rte_eth_devices[internals->slaves[i].port_id],
850                                                 &internals->slaves[i].persisted_mac_addr)) {
851                                         RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
852                                                         internals->slaves[i].port_id);
853                                         return -1;
854                                 }
855                         }
856                 }
857         }
858
859         return 0;
860 }
861
862 int
863 bond_ethdev_mode_set(struct rte_eth_dev *eth_dev, int mode)
864 {
865         struct bond_dev_private *internals;
866
867         internals = eth_dev->data->dev_private;
868
869         switch (mode) {
870         case BONDING_MODE_ROUND_ROBIN:
871                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_round_robin;
872                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
873                 break;
874         case BONDING_MODE_ACTIVE_BACKUP:
875                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_active_backup;
876                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_active_backup;
877                 break;
878         case BONDING_MODE_BALANCE:
879                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_balance;
880                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
881                 break;
882 #ifdef RTE_MBUF_REFCNT
883         case BONDING_MODE_BROADCAST:
884                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_broadcast;
885                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
886                 break;
887 #endif
888         case BONDING_MODE_8023AD:
889                 if (bond_mode_8023ad_enable(eth_dev) != 0)
890                         return -1;
891
892                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_8023ad;
893                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_8023ad;
894                 RTE_BOND_LOG(WARNING,
895                                 "Using mode 4, it is necessary to do TX burst and RX burst "
896                                 "at least every 100ms.");
897                 break;
898         case BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING:
899                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_tlb;
900                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_active_backup;
901                 break;
902         default:
903                 return -1;
904         }
905
906         internals->mode = mode;
907
908         return 0;
909 }
910
911 int
912 slave_configure(struct rte_eth_dev *bonded_eth_dev,
913                 struct rte_eth_dev *slave_eth_dev)
914 {
915         struct bond_rx_queue *bd_rx_q;
916         struct bond_tx_queue *bd_tx_q;
917
918         int errval, q_id;
919
920         /* Stop slave */
921         rte_eth_dev_stop(slave_eth_dev->data->port_id);
922
923         /* Enable interrupts on slave device if supported */
924         if (slave_eth_dev->driver->pci_drv.drv_flags & RTE_PCI_DRV_INTR_LSC)
925                 slave_eth_dev->data->dev_conf.intr_conf.lsc = 1;
926
927         /* Configure device */
928         errval = rte_eth_dev_configure(slave_eth_dev->data->port_id,
929                         bonded_eth_dev->data->nb_rx_queues,
930                         bonded_eth_dev->data->nb_tx_queues,
931                         &(slave_eth_dev->data->dev_conf));
932         if (errval != 0) {
933                 RTE_BOND_LOG(ERR, "Cannot configure slave device: port %u , err (%d)",
934                                 slave_eth_dev->data->port_id, errval);
935                 return errval;
936         }
937
938         /* Setup Rx Queues */
939         for (q_id = 0; q_id < bonded_eth_dev->data->nb_rx_queues; q_id++) {
940                 bd_rx_q = (struct bond_rx_queue *)bonded_eth_dev->data->rx_queues[q_id];
941
942                 errval = rte_eth_rx_queue_setup(slave_eth_dev->data->port_id, q_id,
943                                 bd_rx_q->nb_rx_desc,
944                                 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
945                                 &(bd_rx_q->rx_conf), bd_rx_q->mb_pool);
946                 if (errval != 0) {
947                         RTE_BOND_LOG(ERR,
948                                         "rte_eth_rx_queue_setup: port=%d queue_id %d, err (%d)",
949                                         slave_eth_dev->data->port_id, q_id, errval);
950                         return errval;
951                 }
952         }
953
954         /* Setup Tx Queues */
955         for (q_id = 0; q_id < bonded_eth_dev->data->nb_tx_queues; q_id++) {
956                 bd_tx_q = (struct bond_tx_queue *)bonded_eth_dev->data->tx_queues[q_id];
957
958                 errval = rte_eth_tx_queue_setup(slave_eth_dev->data->port_id, q_id,
959                                 bd_tx_q->nb_tx_desc,
960                                 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
961                                 &bd_tx_q->tx_conf);
962                 if (errval != 0) {
963                         RTE_BOND_LOG(ERR,
964                                         "rte_eth_tx_queue_setup: port=%d queue_id %d, err (%d)",
965                                         slave_eth_dev->data->port_id, q_id, errval);
966                         return errval;
967                 }
968         }
969
970         /* Start device */
971         errval = rte_eth_dev_start(slave_eth_dev->data->port_id);
972         if (errval != 0) {
973                 RTE_BOND_LOG(ERR, "rte_eth_dev_start: port=%u, err (%d)",
974                                 slave_eth_dev->data->port_id, errval);
975                 return -1;
976         }
977
978         return 0;
979 }
980
981 void
982 slave_remove(struct bond_dev_private *internals,
983                 struct rte_eth_dev *slave_eth_dev)
984 {
985         uint8_t i;
986
987         for (i = 0; i < internals->slave_count; i++)
988                 if (internals->slaves[i].port_id ==
989                                 slave_eth_dev->data->port_id)
990                         break;
991
992         if (i < (internals->slave_count - 1))
993                 memmove(&internals->slaves[i], &internals->slaves[i + 1],
994                                 sizeof(internals->slaves[0]) *
995                                 (internals->slave_count - i - 1));
996
997         internals->slave_count--;
998 }
999
1000 static void
1001 bond_ethdev_slave_link_status_change_monitor(void *cb_arg);
1002
1003 void
1004 slave_add(struct bond_dev_private *internals,
1005                 struct rte_eth_dev *slave_eth_dev)
1006 {
1007         struct bond_slave_details *slave_details =
1008                         &internals->slaves[internals->slave_count];
1009
1010         slave_details->port_id = slave_eth_dev->data->port_id;
1011         slave_details->last_link_status = 0;
1012
1013         /* If slave device doesn't support interrupts then we need to enabled
1014          * polling to monitor link status */
1015         if (!(slave_eth_dev->pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
1016                 slave_details->link_status_poll_enabled = 1;
1017
1018                 if (!internals->link_status_polling_enabled) {
1019                         internals->link_status_polling_enabled = 1;
1020
1021                         rte_eal_alarm_set(internals->link_status_polling_interval_ms * 1000,
1022                                         bond_ethdev_slave_link_status_change_monitor,
1023                                         (void *)&rte_eth_devices[internals->port_id]);
1024                 }
1025         }
1026
1027         slave_details->link_status_wait_to_complete = 0;
1028         /* clean tlb_last_obytes when adding port for bonding device */
1029         memcpy(&(slave_details->persisted_mac_addr), slave_eth_dev->data->mac_addrs,
1030                         sizeof(struct ether_addr));
1031 }
1032
1033 void
1034 bond_ethdev_primary_set(struct bond_dev_private *internals,
1035                 uint8_t slave_port_id)
1036 {
1037         int i;
1038
1039         if (internals->active_slave_count < 1)
1040                 internals->current_primary_port = slave_port_id;
1041         else
1042                 /* Search bonded device slave ports for new proposed primary port */
1043                 for (i = 0; i < internals->active_slave_count; i++) {
1044                         if (internals->active_slaves[i] == slave_port_id)
1045                                 internals->current_primary_port = slave_port_id;
1046                 }
1047 }
1048
1049 static void
1050 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev);
1051
1052 static int
1053 bond_ethdev_start(struct rte_eth_dev *eth_dev)
1054 {
1055         struct bond_dev_private *internals;
1056         int i;
1057
1058         /* slave eth dev will be started by bonded device */
1059         if (valid_bonded_ethdev(eth_dev)) {
1060                 RTE_BOND_LOG(ERR, "User tried to explicitly start a slave eth_dev (%d)",
1061                                 eth_dev->data->port_id);
1062                 return -1;
1063         }
1064
1065         eth_dev->data->dev_link.link_status = 0;
1066         eth_dev->data->dev_started = 1;
1067
1068         internals = eth_dev->data->dev_private;
1069
1070         if (internals->slave_count == 0) {
1071                 RTE_BOND_LOG(ERR, "Cannot start port since there are no slave devices");
1072                 return -1;
1073         }
1074
1075         if (internals->user_defined_mac == 0) {
1076                 struct ether_addr *new_mac_addr = NULL;
1077
1078                 for (i = 0; i < internals->slave_count; i++)
1079                         if (internals->slaves[i].port_id == internals->primary_port)
1080                                 new_mac_addr = &internals->slaves[i].persisted_mac_addr;
1081
1082                 if (new_mac_addr == NULL)
1083                         return -1;
1084
1085                 if (mac_address_set(eth_dev, new_mac_addr) != 0) {
1086                         RTE_BOND_LOG(ERR, "bonded port (%d) failed to update MAC address",
1087                                         eth_dev->data->port_id);
1088                         return -1;
1089                 }
1090         }
1091
1092         /* Update all slave devices MACs*/
1093         if (mac_address_slaves_update(eth_dev) != 0)
1094                 return -1;
1095
1096         /* If bonded device is configure in promiscuous mode then re-apply config */
1097         if (internals->promiscuous_en)
1098                 bond_ethdev_promiscuous_enable(eth_dev);
1099
1100         /* Reconfigure each slave device if starting bonded device */
1101         for (i = 0; i < internals->slave_count; i++) {
1102                 if (slave_configure(eth_dev,
1103                                 &(rte_eth_devices[internals->slaves[i].port_id])) != 0) {
1104                         RTE_BOND_LOG(ERR,
1105                                         "bonded port (%d) failed to reconfigure slave device (%d)",
1106                                         eth_dev->data->port_id, internals->slaves[i].port_id);
1107                         return -1;
1108                 }
1109         }
1110
1111         if (internals->user_defined_primary_port)
1112                 bond_ethdev_primary_set(internals, internals->primary_port);
1113
1114         if (internals->mode == BONDING_MODE_8023AD)
1115                 bond_mode_8023ad_start(eth_dev);
1116
1117         if (internals->mode == BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING)
1118                 bond_ethdev_update_tlb_slave_cb(internals);
1119
1120         return 0;
1121 }
1122
1123 static void
1124 bond_ethdev_stop(struct rte_eth_dev *eth_dev)
1125 {
1126         struct bond_dev_private *internals = eth_dev->data->dev_private;
1127         uint8_t i;
1128
1129         if (internals->mode == BONDING_MODE_8023AD) {
1130                 struct port *port;
1131                 void *pkt = NULL;
1132
1133                 bond_mode_8023ad_stop(eth_dev);
1134
1135                 /* Discard all messages to/from mode 4 state machines */
1136                 for (i = 0; i < internals->slave_count; i++) {
1137                         port = &mode_8023ad_ports[internals->slaves[i].port_id];
1138
1139                         RTE_VERIFY(port->rx_ring != NULL);
1140                         while (rte_ring_dequeue(port->rx_ring, &pkt) != -ENOENT)
1141                                 rte_pktmbuf_free(pkt);
1142
1143                         RTE_VERIFY(port->tx_ring != NULL);
1144                         while (rte_ring_dequeue(port->tx_ring, &pkt) != -ENOENT)
1145                                 rte_pktmbuf_free(pkt);
1146                 }
1147         }
1148
1149         if (internals->mode == BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING) {
1150                 rte_eal_alarm_cancel(bond_ethdev_update_tlb_slave_cb, internals);
1151         }
1152
1153         internals->active_slave_count = 0;
1154         internals->link_status_polling_enabled = 0;
1155
1156         eth_dev->data->dev_link.link_status = 0;
1157         eth_dev->data->dev_started = 0;
1158 }
1159
1160 static void
1161 bond_ethdev_close(struct rte_eth_dev *dev __rte_unused)
1162 {
1163 }
1164
1165 /* forward declaration */
1166 static int bond_ethdev_configure(struct rte_eth_dev *dev);
1167
1168 static void
1169 bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1170 {
1171         struct bond_dev_private *internals = dev->data->dev_private;
1172
1173         dev_info->driver_name = driver_name;
1174         dev_info->max_mac_addrs = 1;
1175
1176         dev_info->max_rx_pktlen = (uint32_t)2048;
1177
1178         dev_info->max_rx_queues = (uint16_t)128;
1179         dev_info->max_tx_queues = (uint16_t)512;
1180
1181         dev_info->min_rx_bufsize = 0;
1182         dev_info->pci_dev = dev->pci_dev;
1183
1184         dev_info->rx_offload_capa = internals->rx_offload_capa;
1185         dev_info->tx_offload_capa = internals->tx_offload_capa;
1186 }
1187
1188 static int
1189 bond_ethdev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
1190                 uint16_t nb_rx_desc, unsigned int socket_id __rte_unused,
1191                 const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool)
1192 {
1193         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)
1194                         rte_zmalloc_socket(NULL, sizeof(struct bond_rx_queue),
1195                                         0, dev->pci_dev->numa_node);
1196         if (bd_rx_q == NULL)
1197                 return -1;
1198
1199         bd_rx_q->queue_id = rx_queue_id;
1200         bd_rx_q->dev_private = dev->data->dev_private;
1201
1202         bd_rx_q->nb_rx_desc = nb_rx_desc;
1203
1204         memcpy(&(bd_rx_q->rx_conf), rx_conf, sizeof(struct rte_eth_rxconf));
1205         bd_rx_q->mb_pool = mb_pool;
1206
1207         dev->data->rx_queues[rx_queue_id] = bd_rx_q;
1208
1209         return 0;
1210 }
1211
1212 static int
1213 bond_ethdev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
1214                 uint16_t nb_tx_desc, unsigned int socket_id __rte_unused,
1215                 const struct rte_eth_txconf *tx_conf)
1216 {
1217         struct bond_tx_queue *bd_tx_q  = (struct bond_tx_queue *)
1218                         rte_zmalloc_socket(NULL, sizeof(struct bond_tx_queue),
1219                                         0, dev->pci_dev->numa_node);
1220
1221         if (bd_tx_q == NULL)
1222                 return -1;
1223
1224         bd_tx_q->queue_id = tx_queue_id;
1225         bd_tx_q->dev_private = dev->data->dev_private;
1226
1227         bd_tx_q->nb_tx_desc = nb_tx_desc;
1228         memcpy(&(bd_tx_q->tx_conf), tx_conf, sizeof(bd_tx_q->tx_conf));
1229
1230         dev->data->tx_queues[tx_queue_id] = bd_tx_q;
1231
1232         return 0;
1233 }
1234
1235 static void
1236 bond_ethdev_rx_queue_release(void *queue)
1237 {
1238         if (queue == NULL)
1239                 return;
1240
1241         rte_free(queue);
1242 }
1243
1244 static void
1245 bond_ethdev_tx_queue_release(void *queue)
1246 {
1247         if (queue == NULL)
1248                 return;
1249
1250         rte_free(queue);
1251 }
1252
1253 static void
1254 bond_ethdev_slave_link_status_change_monitor(void *cb_arg)
1255 {
1256         struct rte_eth_dev *bonded_ethdev, *slave_ethdev;
1257         struct bond_dev_private *internals;
1258
1259         /* Default value for polling slave found is true as we don't want to
1260          * disable the polling thread if we cannot get the lock */
1261         int i, polling_slave_found = 1;
1262
1263         if (cb_arg == NULL)
1264                 return;
1265
1266         bonded_ethdev = (struct rte_eth_dev *)cb_arg;
1267         internals = (struct bond_dev_private *)bonded_ethdev->data->dev_private;
1268
1269         if (!bonded_ethdev->data->dev_started ||
1270                 !internals->link_status_polling_enabled)
1271                 return;
1272
1273         /* If device is currently being configured then don't check slaves link
1274          * status, wait until next period */
1275         if (rte_spinlock_trylock(&internals->lock)) {
1276                 if (internals->slave_count > 0)
1277                         polling_slave_found = 0;
1278
1279                 for (i = 0; i < internals->slave_count; i++) {
1280                         if (!internals->slaves[i].link_status_poll_enabled)
1281                                 continue;
1282
1283                         slave_ethdev = &rte_eth_devices[internals->slaves[i].port_id];
1284                         polling_slave_found = 1;
1285
1286                         /* Update slave link status */
1287                         (*slave_ethdev->dev_ops->link_update)(slave_ethdev,
1288                                         internals->slaves[i].link_status_wait_to_complete);
1289
1290                         /* if link status has changed since last checked then call lsc
1291                          * event callback */
1292                         if (slave_ethdev->data->dev_link.link_status !=
1293                                         internals->slaves[i].last_link_status) {
1294                                 internals->slaves[i].last_link_status =
1295                                                 slave_ethdev->data->dev_link.link_status;
1296
1297                                 bond_ethdev_lsc_event_callback(internals->slaves[i].port_id,
1298                                                 RTE_ETH_EVENT_INTR_LSC,
1299                                                 &bonded_ethdev->data->port_id);
1300                         }
1301                 }
1302                 rte_spinlock_unlock(&internals->lock);
1303         }
1304
1305         if (polling_slave_found)
1306                 /* Set alarm to continue monitoring link status of slave ethdev's */
1307                 rte_eal_alarm_set(internals->link_status_polling_interval_ms * 1000,
1308                                 bond_ethdev_slave_link_status_change_monitor, cb_arg);
1309 }
1310
1311 static int
1312 bond_ethdev_link_update(struct rte_eth_dev *bonded_eth_dev,
1313                 int wait_to_complete)
1314 {
1315         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
1316
1317         if (!bonded_eth_dev->data->dev_started ||
1318                 internals->active_slave_count == 0) {
1319                 bonded_eth_dev->data->dev_link.link_status = 0;
1320                 return 0;
1321         } else {
1322                 struct rte_eth_dev *slave_eth_dev;
1323                 int i, link_up = 0;
1324
1325                 for (i = 0; i < internals->active_slave_count; i++) {
1326                         slave_eth_dev = &rte_eth_devices[internals->active_slaves[i]];
1327
1328                         (*slave_eth_dev->dev_ops->link_update)(slave_eth_dev,
1329                                         wait_to_complete);
1330                         if (slave_eth_dev->data->dev_link.link_status == 1) {
1331                                 link_up = 1;
1332                                 break;
1333                         }
1334                 }
1335
1336                 bonded_eth_dev->data->dev_link.link_status = link_up;
1337         }
1338
1339         return 0;
1340 }
1341
1342 static void
1343 bond_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1344 {
1345         struct bond_dev_private *internals = dev->data->dev_private;
1346         struct rte_eth_stats slave_stats;
1347
1348         int i;
1349
1350         /* clear bonded stats before populating from slaves */
1351         memset(stats, 0, sizeof(*stats));
1352
1353         for (i = 0; i < internals->slave_count; i++) {
1354                 rte_eth_stats_get(internals->slaves[i].port_id, &slave_stats);
1355
1356                 stats->ipackets += slave_stats.ipackets;
1357                 stats->opackets += slave_stats.opackets;
1358                 stats->ibytes += slave_stats.ibytes;
1359                 stats->obytes += slave_stats.obytes;
1360                 stats->ierrors += slave_stats.ierrors;
1361                 stats->oerrors += slave_stats.oerrors;
1362                 stats->imcasts += slave_stats.imcasts;
1363                 stats->rx_nombuf += slave_stats.rx_nombuf;
1364                 stats->fdirmatch += slave_stats.fdirmatch;
1365                 stats->fdirmiss += slave_stats.fdirmiss;
1366                 stats->tx_pause_xon += slave_stats.tx_pause_xon;
1367                 stats->rx_pause_xon += slave_stats.rx_pause_xon;
1368                 stats->tx_pause_xoff += slave_stats.tx_pause_xoff;
1369                 stats->rx_pause_xoff += slave_stats.rx_pause_xoff;
1370         }
1371 }
1372
1373 static void
1374 bond_ethdev_stats_reset(struct rte_eth_dev *dev)
1375 {
1376         struct bond_dev_private *internals = dev->data->dev_private;
1377         int i;
1378
1379         for (i = 0; i < internals->slave_count; i++)
1380                 rte_eth_stats_reset(internals->slaves[i].port_id);
1381 }
1382
1383 static void
1384 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev)
1385 {
1386         struct bond_dev_private *internals = eth_dev->data->dev_private;
1387         int i;
1388
1389         internals->promiscuous_en = 1;
1390
1391         switch (internals->mode) {
1392         /* Promiscuous mode is propagated to all slaves */
1393         case BONDING_MODE_ROUND_ROBIN:
1394         case BONDING_MODE_BALANCE:
1395 #ifdef RTE_MBUF_REFCNT
1396         case BONDING_MODE_BROADCAST:
1397 #endif
1398                 for (i = 0; i < internals->slave_count; i++)
1399                         rte_eth_promiscuous_enable(internals->slaves[i].port_id);
1400                 break;
1401         /* In mode4 promiscus mode is managed when slave is added/removed */
1402         case BONDING_MODE_8023AD:
1403                 break;
1404         /* Promiscuous mode is propagated only to primary slave */
1405         case BONDING_MODE_ACTIVE_BACKUP:
1406         case BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING:
1407         default:
1408                 rte_eth_promiscuous_enable(internals->current_primary_port);
1409         }
1410 }
1411
1412 static void
1413 bond_ethdev_promiscuous_disable(struct rte_eth_dev *dev)
1414 {
1415         struct bond_dev_private *internals = dev->data->dev_private;
1416         int i;
1417
1418         internals->promiscuous_en = 0;
1419
1420         switch (internals->mode) {
1421         /* Promiscuous mode is propagated to all slaves */
1422         case BONDING_MODE_ROUND_ROBIN:
1423         case BONDING_MODE_BALANCE:
1424 #ifdef RTE_MBUF_REFCNT
1425         case BONDING_MODE_BROADCAST:
1426 #endif
1427                 for (i = 0; i < internals->slave_count; i++)
1428                         rte_eth_promiscuous_disable(internals->slaves[i].port_id);
1429                 break;
1430         /* In mode4 promiscus mode is set managed when slave is added/removed */
1431         case BONDING_MODE_8023AD:
1432                 break;
1433         /* Promiscuous mode is propagated only to primary slave */
1434         case BONDING_MODE_ACTIVE_BACKUP:
1435         case BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING:
1436         default:
1437                 rte_eth_promiscuous_disable(internals->current_primary_port);
1438         }
1439 }
1440
1441 static void
1442 bond_ethdev_delayed_lsc_propagation(void *arg)
1443 {
1444         if (arg == NULL)
1445                 return;
1446
1447         _rte_eth_dev_callback_process((struct rte_eth_dev *)arg,
1448                         RTE_ETH_EVENT_INTR_LSC);
1449 }
1450
1451 void
1452 bond_ethdev_lsc_event_callback(uint8_t port_id, enum rte_eth_event_type type,
1453                 void *param)
1454 {
1455         struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
1456         struct bond_dev_private *internals;
1457         struct rte_eth_link link;
1458
1459         int i, valid_slave = 0;
1460         uint8_t active_pos;
1461         uint8_t lsc_flag = 0;
1462
1463         if (type != RTE_ETH_EVENT_INTR_LSC || param == NULL)
1464                 return;
1465
1466         bonded_eth_dev = &rte_eth_devices[*(uint8_t *)param];
1467         slave_eth_dev = &rte_eth_devices[port_id];
1468
1469         if (valid_bonded_ethdev(bonded_eth_dev))
1470                 return;
1471
1472         internals = bonded_eth_dev->data->dev_private;
1473
1474         /* If the device isn't started don't handle interrupts */
1475         if (!bonded_eth_dev->data->dev_started)
1476                 return;
1477
1478         /* verify that port_id is a valid slave of bonded port */
1479         for (i = 0; i < internals->slave_count; i++) {
1480                 if (internals->slaves[i].port_id == port_id) {
1481                         valid_slave = 1;
1482                         break;
1483                 }
1484         }
1485
1486         if (!valid_slave)
1487                 return;
1488
1489         /* Search for port in active port list */
1490         active_pos = find_slave_by_id(internals->active_slaves,
1491                         internals->active_slave_count, port_id);
1492
1493         rte_eth_link_get_nowait(port_id, &link);
1494         if (link.link_status) {
1495                 if (active_pos < internals->active_slave_count)
1496                         return;
1497
1498                 /* if no active slave ports then set this port to be primary port */
1499                 if (internals->active_slave_count < 1) {
1500                         /* If first active slave, then change link status */
1501                         bonded_eth_dev->data->dev_link.link_status = 1;
1502                         internals->current_primary_port = port_id;
1503                         lsc_flag = 1;
1504
1505                         mac_address_slaves_update(bonded_eth_dev);
1506
1507                         /* Inherit eth dev link properties from first active slave */
1508                         link_properties_set(bonded_eth_dev,
1509                                         &(slave_eth_dev->data->dev_link));
1510                 }
1511
1512                 activate_slave(bonded_eth_dev, port_id);
1513
1514                 /* If user has defined the primary port then default to using it */
1515                 if (internals->user_defined_primary_port &&
1516                                 internals->primary_port == port_id)
1517                         bond_ethdev_primary_set(internals, port_id);
1518         } else {
1519                 if (active_pos == internals->active_slave_count)
1520                         return;
1521
1522                 /* Remove from active slave list */
1523                 deactivate_slave(bonded_eth_dev, port_id);
1524
1525                 /* No active slaves, change link status to down and reset other
1526                  * link properties */
1527                 if (internals->active_slave_count < 1) {
1528                         lsc_flag = 1;
1529                         bonded_eth_dev->data->dev_link.link_status = 0;
1530
1531                         link_properties_reset(bonded_eth_dev);
1532                 }
1533
1534                 /* Update primary id, take first active slave from list or if none
1535                  * available set to -1 */
1536                 if (port_id == internals->current_primary_port) {
1537                         if (internals->active_slave_count > 0)
1538                                 bond_ethdev_primary_set(internals,
1539                                                 internals->active_slaves[0]);
1540                         else
1541                                 internals->current_primary_port = internals->primary_port;
1542                 }
1543         }
1544
1545         if (lsc_flag) {
1546                 /* Cancel any possible outstanding interrupts if delays are enabled */
1547                 if (internals->link_up_delay_ms > 0 ||
1548                         internals->link_down_delay_ms > 0)
1549                         rte_eal_alarm_cancel(bond_ethdev_delayed_lsc_propagation,
1550                                         bonded_eth_dev);
1551
1552                 if (bonded_eth_dev->data->dev_link.link_status) {
1553                         if (internals->link_up_delay_ms > 0)
1554                                 rte_eal_alarm_set(internals->link_up_delay_ms * 1000,
1555                                                 bond_ethdev_delayed_lsc_propagation,
1556                                                 (void *)bonded_eth_dev);
1557                         else
1558                                 _rte_eth_dev_callback_process(bonded_eth_dev,
1559                                                 RTE_ETH_EVENT_INTR_LSC);
1560
1561                 } else {
1562                         if (internals->link_down_delay_ms > 0)
1563                                 rte_eal_alarm_set(internals->link_down_delay_ms * 1000,
1564                                                 bond_ethdev_delayed_lsc_propagation,
1565                                                 (void *)bonded_eth_dev);
1566                         else
1567                                 _rte_eth_dev_callback_process(bonded_eth_dev,
1568                                                 RTE_ETH_EVENT_INTR_LSC);
1569                 }
1570         }
1571 }
1572
1573 struct eth_dev_ops default_dev_ops = {
1574                 .dev_start = bond_ethdev_start,
1575                 .dev_stop = bond_ethdev_stop,
1576                 .dev_close = bond_ethdev_close,
1577                 .dev_configure = bond_ethdev_configure,
1578                 .dev_infos_get = bond_ethdev_info,
1579                 .rx_queue_setup = bond_ethdev_rx_queue_setup,
1580                 .tx_queue_setup = bond_ethdev_tx_queue_setup,
1581                 .rx_queue_release = bond_ethdev_rx_queue_release,
1582                 .tx_queue_release = bond_ethdev_tx_queue_release,
1583                 .link_update = bond_ethdev_link_update,
1584                 .stats_get = bond_ethdev_stats_get,
1585                 .stats_reset = bond_ethdev_stats_reset,
1586                 .promiscuous_enable = bond_ethdev_promiscuous_enable,
1587                 .promiscuous_disable = bond_ethdev_promiscuous_disable
1588 };
1589
1590 static int
1591 bond_init(const char *name, const char *params)
1592 {
1593         struct bond_dev_private *internals;
1594         struct rte_kvargs *kvlist;
1595         uint8_t bonding_mode, socket_id;
1596         int  arg_count, port_id;
1597
1598         RTE_LOG(INFO, EAL, "Initializing pmd_bond for %s\n", name);
1599
1600         kvlist = rte_kvargs_parse(params, pmd_bond_init_valid_arguments);
1601         if (kvlist == NULL)
1602                 return -1;
1603
1604         /* Parse link bonding mode */
1605         if (rte_kvargs_count(kvlist, PMD_BOND_MODE_KVARG) == 1) {
1606                 if (rte_kvargs_process(kvlist, PMD_BOND_MODE_KVARG,
1607                                 &bond_ethdev_parse_slave_mode_kvarg, &bonding_mode) != 0) {
1608                         RTE_LOG(ERR, EAL, "Invalid mode for bonded device %s\n", name);
1609                         return -1;
1610                 }
1611         } else {
1612                 RTE_LOG(ERR, EAL,
1613                                 "Mode must be specified only once for bonded device %s\n",
1614                                 name);
1615                 return -1;
1616         }
1617
1618         /* Parse socket id to create bonding device on */
1619         arg_count = rte_kvargs_count(kvlist, PMD_BOND_SOCKET_ID_KVARG);
1620         if (arg_count == 1) {
1621                 if (rte_kvargs_process(kvlist, PMD_BOND_SOCKET_ID_KVARG,
1622                                 &bond_ethdev_parse_socket_id_kvarg, &socket_id) != 0) {
1623                         RTE_LOG(ERR, EAL,
1624                                         "Invalid socket Id specified for bonded device %s\n",
1625                                         name);
1626                         return -1;
1627                 }
1628         } else if (arg_count > 1) {
1629                 RTE_LOG(ERR, EAL,
1630                                 "Socket Id can be specified only once for bonded device %s\n",
1631                                 name);
1632                 return -1;
1633         } else {
1634                 socket_id = rte_socket_id();
1635         }
1636
1637         /* Create link bonding eth device */
1638         port_id = rte_eth_bond_create(name, bonding_mode, socket_id);
1639         if (port_id < 0) {
1640                 RTE_LOG(ERR, EAL,
1641                                 "Failed to create socket %s in mode %u on socket %u.\n",
1642                                 name, bonding_mode, socket_id);
1643                 return -1;
1644         }
1645         internals = rte_eth_devices[port_id].data->dev_private;
1646         internals->kvlist = kvlist;
1647
1648         RTE_LOG(INFO, EAL,
1649                         "Create bonded device %s on port %d in mode %u on socket %u.\n",
1650                         name, port_id, bonding_mode, socket_id);
1651         return 0;
1652 }
1653
1654 /* this part will resolve the slave portids after all the other pdev and vdev
1655  * have been allocated */
1656 static int
1657 bond_ethdev_configure(struct rte_eth_dev *dev)
1658 {
1659         char *name = dev->data->name;
1660         struct bond_dev_private *internals = dev->data->dev_private;
1661         struct rte_kvargs *kvlist = internals->kvlist;
1662         int arg_count, port_id = dev - rte_eth_devices;
1663
1664         /*
1665          * if no kvlist, it means that this bonded device has been created
1666          * through the bonding api.
1667          */
1668         if (!kvlist)
1669                 return 0;
1670
1671         /* Parse MAC address for bonded device */
1672         arg_count = rte_kvargs_count(kvlist, PMD_BOND_MAC_ADDR_KVARG);
1673         if (arg_count == 1) {
1674                 struct ether_addr bond_mac;
1675
1676                 if (rte_kvargs_process(kvlist, PMD_BOND_MAC_ADDR_KVARG,
1677                                 &bond_ethdev_parse_bond_mac_addr_kvarg, &bond_mac) < 0) {
1678                         RTE_LOG(INFO, EAL, "Invalid mac address for bonded device %s\n",
1679                                         name);
1680                         return -1;
1681                 }
1682
1683                 /* Set MAC address */
1684                 if (rte_eth_bond_mac_address_set(port_id, &bond_mac) != 0) {
1685                         RTE_LOG(ERR, EAL,
1686                                         "Failed to set mac address on bonded device %s\n",
1687                                         name);
1688                         return -1;
1689                 }
1690         } else if (arg_count > 1) {
1691                 RTE_LOG(ERR, EAL,
1692                                 "MAC address can be specified only once for bonded device %s\n",
1693                                 name);
1694                 return -1;
1695         }
1696
1697         /* Parse/set balance mode transmit policy */
1698         arg_count = rte_kvargs_count(kvlist, PMD_BOND_XMIT_POLICY_KVARG);
1699         if (arg_count == 1) {
1700                 uint8_t xmit_policy;
1701
1702                 if (rte_kvargs_process(kvlist, PMD_BOND_XMIT_POLICY_KVARG,
1703                                 &bond_ethdev_parse_balance_xmit_policy_kvarg, &xmit_policy) !=
1704                                                 0) {
1705                         RTE_LOG(INFO, EAL,
1706                                         "Invalid xmit policy specified for bonded device %s\n",
1707                                         name);
1708                         return -1;
1709                 }
1710
1711                 /* Set balance mode transmit policy*/
1712                 if (rte_eth_bond_xmit_policy_set(port_id, xmit_policy) != 0) {
1713                         RTE_LOG(ERR, EAL,
1714                                         "Failed to set balance xmit policy on bonded device %s\n",
1715                                         name);
1716                         return -1;
1717                 }
1718         } else if (arg_count > 1) {
1719                 RTE_LOG(ERR, EAL,
1720                                 "Transmit policy can be specified only once for bonded device"
1721                                 " %s\n", name);
1722                 return -1;
1723         }
1724
1725         /* Parse/add slave ports to bonded device */
1726         if (rte_kvargs_count(kvlist, PMD_BOND_SLAVE_PORT_KVARG) > 0) {
1727                 struct bond_ethdev_slave_ports slave_ports;
1728                 unsigned i;
1729
1730                 memset(&slave_ports, 0, sizeof(slave_ports));
1731
1732                 if (rte_kvargs_process(kvlist, PMD_BOND_SLAVE_PORT_KVARG,
1733                                 &bond_ethdev_parse_slave_port_kvarg, &slave_ports) != 0) {
1734                         RTE_LOG(ERR, EAL,
1735                                         "Failed to parse slave ports for bonded device %s\n",
1736                                         name);
1737                         return -1;
1738                 }
1739
1740                 for (i = 0; i < slave_ports.slave_count; i++) {
1741                         if (rte_eth_bond_slave_add(port_id, slave_ports.slaves[i]) != 0) {
1742                                 RTE_LOG(ERR, EAL,
1743                                                 "Failed to add port %d as slave to bonded device %s\n",
1744                                                 slave_ports.slaves[i], name);
1745                         }
1746                 }
1747
1748         } else {
1749                 RTE_LOG(INFO, EAL, "No slaves specified for bonded device %s\n", name);
1750                 return -1;
1751         }
1752
1753         /* Parse/set primary slave port id*/
1754         arg_count = rte_kvargs_count(kvlist, PMD_BOND_PRIMARY_SLAVE_KVARG);
1755         if (arg_count == 1) {
1756                 uint8_t primary_slave_port_id;
1757
1758                 if (rte_kvargs_process(kvlist,
1759                                 PMD_BOND_PRIMARY_SLAVE_KVARG,
1760                                 &bond_ethdev_parse_primary_slave_port_id_kvarg,
1761                                 &primary_slave_port_id) < 0) {
1762                         RTE_LOG(INFO, EAL,
1763                                         "Invalid primary slave port id specified for bonded device"
1764                                         " %s\n", name);
1765                         return -1;
1766                 }
1767
1768                 /* Set balance mode transmit policy*/
1769                 if (rte_eth_bond_primary_set(port_id, (uint8_t)primary_slave_port_id)
1770                                 != 0) {
1771                         RTE_LOG(ERR, EAL,
1772                                         "Failed to set primary slave port %d on bonded device %s\n",
1773                                         primary_slave_port_id, name);
1774                         return -1;
1775                 }
1776         } else if (arg_count > 1) {
1777                 RTE_LOG(INFO, EAL,
1778                                 "Primary slave can be specified only once for bonded device"
1779                                 " %s\n", name);
1780                 return -1;
1781         }
1782
1783         /* Parse link status monitor polling interval */
1784         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LSC_POLL_PERIOD_KVARG);
1785         if (arg_count == 1) {
1786                 uint32_t lsc_poll_interval_ms;
1787
1788                 if (rte_kvargs_process(kvlist,
1789                                 PMD_BOND_LSC_POLL_PERIOD_KVARG,
1790                                 &bond_ethdev_parse_time_ms_kvarg,
1791                                 &lsc_poll_interval_ms) < 0) {
1792                         RTE_LOG(INFO, EAL,
1793                                         "Invalid lsc polling interval value specified for bonded"
1794                                         " device %s\n", name);
1795                         return -1;
1796                 }
1797
1798                 if (rte_eth_bond_link_monitoring_set(port_id, lsc_poll_interval_ms)
1799                                 != 0) {
1800                         RTE_LOG(ERR, EAL,
1801                                         "Failed to set lsc monitor polling interval (%u ms) on"
1802                                         " bonded device %s\n", lsc_poll_interval_ms, name);
1803                         return -1;
1804                 }
1805         } else if (arg_count > 1) {
1806                 RTE_LOG(INFO, EAL,
1807                                 "LSC polling interval can be specified only once for bonded"
1808                                 " device %s\n", name);
1809                 return -1;
1810         }
1811
1812         /* Parse link up interrupt propagation delay */
1813         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LINK_UP_PROP_DELAY_KVARG);
1814         if (arg_count == 1) {
1815                 uint32_t link_up_delay_ms;
1816
1817                 if (rte_kvargs_process(kvlist,
1818                                 PMD_BOND_LINK_UP_PROP_DELAY_KVARG,
1819                                 &bond_ethdev_parse_time_ms_kvarg,
1820                                 &link_up_delay_ms) < 0) {
1821                         RTE_LOG(INFO, EAL,
1822                                         "Invalid link up propagation delay value specified for"
1823                                         " bonded device %s\n", name);
1824                         return -1;
1825                 }
1826
1827                 /* Set balance mode transmit policy*/
1828                 if (rte_eth_bond_link_up_prop_delay_set(port_id, link_up_delay_ms)
1829                                 != 0) {
1830                         RTE_LOG(ERR, EAL,
1831                                         "Failed to set link up propagation delay (%u ms) on bonded"
1832                                         " device %s\n", link_up_delay_ms, name);
1833                         return -1;
1834                 }
1835         } else if (arg_count > 1) {
1836                 RTE_LOG(INFO, EAL,
1837                                 "Link up propagation delay can be specified only once for"
1838                                 " bonded device %s\n", name);
1839                 return -1;
1840         }
1841
1842         /* Parse link down interrupt propagation delay */
1843         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG);
1844         if (arg_count == 1) {
1845                 uint32_t link_down_delay_ms;
1846
1847                 if (rte_kvargs_process(kvlist,
1848                                 PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG,
1849                                 &bond_ethdev_parse_time_ms_kvarg,
1850                                 &link_down_delay_ms) < 0) {
1851                         RTE_LOG(INFO, EAL,
1852                                         "Invalid link down propagation delay value specified for"
1853                                         " bonded device %s\n", name);
1854                         return -1;
1855                 }
1856
1857                 /* Set balance mode transmit policy*/
1858                 if (rte_eth_bond_link_down_prop_delay_set(port_id, link_down_delay_ms)
1859                                 != 0) {
1860                         RTE_LOG(ERR, EAL,
1861                                         "Failed to set link down propagation delay (%u ms) on"
1862                                         " bonded device %s\n", link_down_delay_ms, name);
1863                         return -1;
1864                 }
1865         } else if (arg_count > 1) {
1866                 RTE_LOG(INFO, EAL,
1867                                 "Link down propagation delay can be specified only once for"
1868                                 " bonded device %s\n", name);
1869                 return -1;
1870         }
1871
1872         return 0;
1873 }
1874
1875 static struct rte_driver bond_drv = {
1876         .name = "eth_bond",
1877         .type = PMD_VDEV,
1878         .init = bond_init,
1879 };
1880
1881 PMD_REGISTER_DRIVER(bond_drv);