bond: add mode 5
[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];
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         mac_addr = eth_dev->data->mac_addrs;
768
769         if (eth_dev == NULL) {
770                 RTE_LOG(ERR, PMD, "%s: NULL pointer eth_dev specified\n", __func__);
771                 return -1;
772         }
773
774         if (dst_mac_addr == NULL) {
775                 RTE_LOG(ERR, PMD, "%s: NULL pointer MAC specified\n", __func__);
776                 return -1;
777         }
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         mac_addr = eth_dev->data->mac_addrs;
789
790         if (eth_dev == NULL) {
791                 RTE_BOND_LOG(ERR, "NULL pointer eth_dev specified");
792                 return -1;
793         }
794
795         if (new_mac_addr == NULL) {
796                 RTE_BOND_LOG(ERR, "NULL pointer MAC specified");
797                 return -1;
798         }
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         int i, found = 0;
986
987         for (i = 0; i < internals->slave_count; i++) {
988                 if (internals->slaves[i].port_id ==     slave_eth_dev->data->port_id)
989                         found = 1;
990
991                 if (found && i < (internals->slave_count - 1))
992                         memcpy(&internals->slaves[i], &internals->slaves[i+1],
993                                         sizeof(internals->slaves[i]));
994         }
995
996         internals->slave_count--;
997 }
998
999 static void
1000 bond_ethdev_slave_link_status_change_monitor(void *cb_arg);
1001
1002 void
1003 slave_add(struct bond_dev_private *internals,
1004                 struct rte_eth_dev *slave_eth_dev)
1005 {
1006         struct bond_slave_details *slave_details =
1007                         &internals->slaves[internals->slave_count];
1008
1009         slave_details->port_id = slave_eth_dev->data->port_id;
1010         slave_details->last_link_status = 0;
1011
1012         /* If slave device doesn't support interrupts then we need to enabled
1013          * polling to monitor link status */
1014         if (!(slave_eth_dev->pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
1015                 slave_details->link_status_poll_enabled = 1;
1016
1017                 if (!internals->link_status_polling_enabled) {
1018                         internals->link_status_polling_enabled = 1;
1019
1020                         rte_eal_alarm_set(internals->link_status_polling_interval_ms * 1000,
1021                                         bond_ethdev_slave_link_status_change_monitor,
1022                                         (void *)&rte_eth_devices[internals->port_id]);
1023                 }
1024         }
1025
1026         slave_details->link_status_wait_to_complete = 0;
1027         /* clean tlb_last_obytes when adding port for bonding device */
1028         memcpy(&(slave_details->persisted_mac_addr), slave_eth_dev->data->mac_addrs,
1029                         sizeof(struct ether_addr));
1030 }
1031
1032 void
1033 bond_ethdev_primary_set(struct bond_dev_private *internals,
1034                 uint8_t slave_port_id)
1035 {
1036         int i;
1037
1038         if (internals->active_slave_count < 1)
1039                 internals->current_primary_port = slave_port_id;
1040         else
1041                 /* Search bonded device slave ports for new proposed primary port */
1042                 for (i = 0; i < internals->active_slave_count; i++) {
1043                         if (internals->active_slaves[i] == slave_port_id)
1044                                 internals->current_primary_port = slave_port_id;
1045                 }
1046 }
1047
1048 static void
1049 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev);
1050
1051 static int
1052 bond_ethdev_start(struct rte_eth_dev *eth_dev)
1053 {
1054         struct bond_dev_private *internals;
1055         int i;
1056
1057         /* slave eth dev will be started by bonded device */
1058         if (valid_bonded_ethdev(eth_dev)) {
1059                 RTE_BOND_LOG(ERR, "User tried to explicitly start a slave eth_dev (%d)",
1060                                 eth_dev->data->port_id);
1061                 return -1;
1062         }
1063
1064         eth_dev->data->dev_link.link_status = 0;
1065         eth_dev->data->dev_started = 1;
1066
1067         internals = eth_dev->data->dev_private;
1068
1069         if (internals->slave_count == 0) {
1070                 RTE_BOND_LOG(ERR, "Cannot start port since there are no slave devices");
1071                 return -1;
1072         }
1073
1074         if (internals->user_defined_mac == 0) {
1075                 struct ether_addr *new_mac_addr = NULL;
1076
1077                 for (i = 0; i < internals->slave_count; i++)
1078                         if (internals->slaves[i].port_id == internals->primary_port)
1079                                 new_mac_addr = &internals->slaves[i].persisted_mac_addr;
1080
1081                 if (new_mac_addr == NULL)
1082                         return -1;
1083
1084                 if (mac_address_set(eth_dev, new_mac_addr) != 0) {
1085                         RTE_BOND_LOG(ERR, "bonded port (%d) failed to update MAC address",
1086                                         eth_dev->data->port_id);
1087                         return -1;
1088                 }
1089         }
1090
1091         /* Update all slave devices MACs*/
1092         if (mac_address_slaves_update(eth_dev) != 0)
1093                 return -1;
1094
1095         /* If bonded device is configure in promiscuous mode then re-apply config */
1096         if (internals->promiscuous_en)
1097                 bond_ethdev_promiscuous_enable(eth_dev);
1098
1099         /* Reconfigure each slave device if starting bonded device */
1100         for (i = 0; i < internals->slave_count; i++) {
1101                 if (slave_configure(eth_dev,
1102                                 &(rte_eth_devices[internals->slaves[i].port_id])) != 0) {
1103                         RTE_BOND_LOG(ERR,
1104                                         "bonded port (%d) failed to reconfigure slave device (%d)",
1105                                         eth_dev->data->port_id, internals->slaves[i].port_id);
1106                         return -1;
1107                 }
1108         }
1109
1110         if (internals->user_defined_primary_port)
1111                 bond_ethdev_primary_set(internals, internals->primary_port);
1112
1113         if (internals->mode == BONDING_MODE_8023AD)
1114                 bond_mode_8023ad_start(eth_dev);
1115
1116         if (internals->mode == BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING)
1117                 bond_ethdev_update_tlb_slave_cb(internals);
1118
1119         return 0;
1120 }
1121
1122 static void
1123 bond_ethdev_stop(struct rte_eth_dev *eth_dev)
1124 {
1125         struct bond_dev_private *internals = eth_dev->data->dev_private;
1126         uint8_t i;
1127
1128         if (internals->mode == BONDING_MODE_8023AD) {
1129                 struct port *port;
1130                 void *pkt = NULL;
1131
1132                 bond_mode_8023ad_stop(eth_dev);
1133
1134                 /* Discard all messages to/from mode 4 state machines */
1135                 for (i = 0; i < internals->slave_count; i++) {
1136                         port = &mode_8023ad_ports[internals->slaves[i].port_id];
1137
1138                         RTE_VERIFY(port->rx_ring != NULL);
1139                         while (rte_ring_dequeue(port->rx_ring, &pkt) != -ENOENT)
1140                                 rte_pktmbuf_free(pkt);
1141
1142                         RTE_VERIFY(port->tx_ring != NULL);
1143                         while (rte_ring_dequeue(port->tx_ring, &pkt) != -ENOENT)
1144                                 rte_pktmbuf_free(pkt);
1145                 }
1146         }
1147
1148         if (internals->mode == BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING) {
1149                 rte_eal_alarm_cancel(bond_ethdev_update_tlb_slave_cb, internals);
1150         }
1151
1152         internals->active_slave_count = 0;
1153         internals->link_status_polling_enabled = 0;
1154
1155         eth_dev->data->dev_link.link_status = 0;
1156         eth_dev->data->dev_started = 0;
1157 }
1158
1159 static void
1160 bond_ethdev_close(struct rte_eth_dev *dev __rte_unused)
1161 {
1162 }
1163
1164 /* forward declaration */
1165 static int bond_ethdev_configure(struct rte_eth_dev *dev);
1166
1167 static void
1168 bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1169 {
1170         dev_info->driver_name = driver_name;
1171         dev_info->max_mac_addrs = 1;
1172
1173         dev_info->max_rx_pktlen = (uint32_t)2048;
1174
1175         dev_info->max_rx_queues = (uint16_t)128;
1176         dev_info->max_tx_queues = (uint16_t)512;
1177
1178         dev_info->min_rx_bufsize = 0;
1179         dev_info->pci_dev = dev->pci_dev;
1180 }
1181
1182 static int
1183 bond_ethdev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
1184                 uint16_t nb_rx_desc, unsigned int socket_id __rte_unused,
1185                 const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool)
1186 {
1187         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)
1188                         rte_zmalloc_socket(NULL, sizeof(struct bond_rx_queue),
1189                                         0, dev->pci_dev->numa_node);
1190         if (bd_rx_q == NULL)
1191                 return -1;
1192
1193         bd_rx_q->queue_id = rx_queue_id;
1194         bd_rx_q->dev_private = dev->data->dev_private;
1195
1196         bd_rx_q->nb_rx_desc = nb_rx_desc;
1197
1198         memcpy(&(bd_rx_q->rx_conf), rx_conf, sizeof(struct rte_eth_rxconf));
1199         bd_rx_q->mb_pool = mb_pool;
1200
1201         dev->data->rx_queues[rx_queue_id] = bd_rx_q;
1202
1203         return 0;
1204 }
1205
1206 static int
1207 bond_ethdev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
1208                 uint16_t nb_tx_desc, unsigned int socket_id __rte_unused,
1209                 const struct rte_eth_txconf *tx_conf)
1210 {
1211         struct bond_tx_queue *bd_tx_q  = (struct bond_tx_queue *)
1212                         rte_zmalloc_socket(NULL, sizeof(struct bond_tx_queue),
1213                                         0, dev->pci_dev->numa_node);
1214
1215         if (bd_tx_q == NULL)
1216                 return -1;
1217
1218         bd_tx_q->queue_id = tx_queue_id;
1219         bd_tx_q->dev_private = dev->data->dev_private;
1220
1221         bd_tx_q->nb_tx_desc = nb_tx_desc;
1222         memcpy(&(bd_tx_q->tx_conf), tx_conf, sizeof(bd_tx_q->tx_conf));
1223
1224         dev->data->tx_queues[tx_queue_id] = bd_tx_q;
1225
1226         return 0;
1227 }
1228
1229 static void
1230 bond_ethdev_rx_queue_release(void *queue)
1231 {
1232         if (queue == NULL)
1233                 return;
1234
1235         rte_free(queue);
1236 }
1237
1238 static void
1239 bond_ethdev_tx_queue_release(void *queue)
1240 {
1241         if (queue == NULL)
1242                 return;
1243
1244         rte_free(queue);
1245 }
1246
1247 static void
1248 bond_ethdev_slave_link_status_change_monitor(void *cb_arg)
1249 {
1250         struct rte_eth_dev *bonded_ethdev, *slave_ethdev;
1251         struct bond_dev_private *internals;
1252
1253         /* Default value for polling slave found is true as we don't want to
1254          * disable the polling thread if we cannot get the lock */
1255         int i, polling_slave_found = 1;
1256
1257         if (cb_arg == NULL)
1258                 return;
1259
1260         bonded_ethdev = (struct rte_eth_dev *)cb_arg;
1261         internals = (struct bond_dev_private *)bonded_ethdev->data->dev_private;
1262
1263         if (!bonded_ethdev->data->dev_started ||
1264                 !internals->link_status_polling_enabled)
1265                 return;
1266
1267         /* If device is currently being configured then don't check slaves link
1268          * status, wait until next period */
1269         if (rte_spinlock_trylock(&internals->lock)) {
1270                 if (internals->slave_count > 0)
1271                         polling_slave_found = 0;
1272
1273                 for (i = 0; i < internals->slave_count; i++) {
1274                         if (!internals->slaves[i].link_status_poll_enabled)
1275                                 continue;
1276
1277                         slave_ethdev = &rte_eth_devices[internals->slaves[i].port_id];
1278                         polling_slave_found = 1;
1279
1280                         /* Update slave link status */
1281                         (*slave_ethdev->dev_ops->link_update)(slave_ethdev,
1282                                         internals->slaves[i].link_status_wait_to_complete);
1283
1284                         /* if link status has changed since last checked then call lsc
1285                          * event callback */
1286                         if (slave_ethdev->data->dev_link.link_status !=
1287                                         internals->slaves[i].last_link_status) {
1288                                 internals->slaves[i].last_link_status =
1289                                                 slave_ethdev->data->dev_link.link_status;
1290
1291                                 bond_ethdev_lsc_event_callback(internals->slaves[i].port_id,
1292                                                 RTE_ETH_EVENT_INTR_LSC,
1293                                                 &bonded_ethdev->data->port_id);
1294                         }
1295                 }
1296                 rte_spinlock_unlock(&internals->lock);
1297         }
1298
1299         if (polling_slave_found)
1300                 /* Set alarm to continue monitoring link status of slave ethdev's */
1301                 rte_eal_alarm_set(internals->link_status_polling_interval_ms * 1000,
1302                                 bond_ethdev_slave_link_status_change_monitor, cb_arg);
1303 }
1304
1305 static int
1306 bond_ethdev_link_update(struct rte_eth_dev *bonded_eth_dev,
1307                 int wait_to_complete)
1308 {
1309         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
1310
1311         if (!bonded_eth_dev->data->dev_started ||
1312                 internals->active_slave_count == 0) {
1313                 bonded_eth_dev->data->dev_link.link_status = 0;
1314                 return 0;
1315         } else {
1316                 struct rte_eth_dev *slave_eth_dev;
1317                 int i, link_up = 0;
1318
1319                 for (i = 0; i < internals->active_slave_count; i++) {
1320                         slave_eth_dev = &rte_eth_devices[internals->active_slaves[i]];
1321
1322                         (*slave_eth_dev->dev_ops->link_update)(slave_eth_dev,
1323                                         wait_to_complete);
1324                         if (slave_eth_dev->data->dev_link.link_status == 1) {
1325                                 link_up = 1;
1326                                 break;
1327                         }
1328                 }
1329
1330                 bonded_eth_dev->data->dev_link.link_status = link_up;
1331         }
1332
1333         return 0;
1334 }
1335
1336 static void
1337 bond_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1338 {
1339         struct bond_dev_private *internals = dev->data->dev_private;
1340         struct rte_eth_stats slave_stats;
1341
1342         int i;
1343
1344         /* clear bonded stats before populating from slaves */
1345         memset(stats, 0, sizeof(*stats));
1346
1347         for (i = 0; i < internals->slave_count; i++) {
1348                 rte_eth_stats_get(internals->slaves[i].port_id, &slave_stats);
1349
1350                 stats->ipackets += slave_stats.ipackets;
1351                 stats->opackets += slave_stats.opackets;
1352                 stats->ibytes += slave_stats.ibytes;
1353                 stats->obytes += slave_stats.obytes;
1354                 stats->ierrors += slave_stats.ierrors;
1355                 stats->oerrors += slave_stats.oerrors;
1356                 stats->imcasts += slave_stats.imcasts;
1357                 stats->rx_nombuf += slave_stats.rx_nombuf;
1358                 stats->fdirmatch += slave_stats.fdirmatch;
1359                 stats->fdirmiss += slave_stats.fdirmiss;
1360                 stats->tx_pause_xon += slave_stats.tx_pause_xon;
1361                 stats->rx_pause_xon += slave_stats.rx_pause_xon;
1362                 stats->tx_pause_xoff += slave_stats.tx_pause_xoff;
1363                 stats->rx_pause_xoff += slave_stats.rx_pause_xoff;
1364         }
1365 }
1366
1367 static void
1368 bond_ethdev_stats_reset(struct rte_eth_dev *dev)
1369 {
1370         struct bond_dev_private *internals = dev->data->dev_private;
1371         int i;
1372
1373         for (i = 0; i < internals->slave_count; i++)
1374                 rte_eth_stats_reset(internals->slaves[i].port_id);
1375 }
1376
1377 static void
1378 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev)
1379 {
1380         struct bond_dev_private *internals = eth_dev->data->dev_private;
1381         int i;
1382
1383         internals->promiscuous_en = 1;
1384
1385         switch (internals->mode) {
1386         /* Promiscuous mode is propagated to all slaves */
1387         case BONDING_MODE_ROUND_ROBIN:
1388         case BONDING_MODE_BALANCE:
1389 #ifdef RTE_MBUF_REFCNT
1390         case BONDING_MODE_BROADCAST:
1391 #endif
1392                 for (i = 0; i < internals->slave_count; i++)
1393                         rte_eth_promiscuous_enable(internals->slaves[i].port_id);
1394                 break;
1395         /* In mode4 promiscus mode is managed when slave is added/removed */
1396         case BONDING_MODE_8023AD:
1397                 break;
1398         /* Promiscuous mode is propagated only to primary slave */
1399         case BONDING_MODE_ACTIVE_BACKUP:
1400         case BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING:
1401         default:
1402                 rte_eth_promiscuous_enable(internals->current_primary_port);
1403         }
1404 }
1405
1406 static void
1407 bond_ethdev_promiscuous_disable(struct rte_eth_dev *dev)
1408 {
1409         struct bond_dev_private *internals = dev->data->dev_private;
1410         int i;
1411
1412         internals->promiscuous_en = 0;
1413
1414         switch (internals->mode) {
1415         /* Promiscuous mode is propagated to all slaves */
1416         case BONDING_MODE_ROUND_ROBIN:
1417         case BONDING_MODE_BALANCE:
1418 #ifdef RTE_MBUF_REFCNT
1419         case BONDING_MODE_BROADCAST:
1420 #endif
1421                 for (i = 0; i < internals->slave_count; i++)
1422                         rte_eth_promiscuous_disable(internals->slaves[i].port_id);
1423                 break;
1424         /* In mode4 promiscus mode is set managed when slave is added/removed */
1425         case BONDING_MODE_8023AD:
1426                 break;
1427         /* Promiscuous mode is propagated only to primary slave */
1428         case BONDING_MODE_ACTIVE_BACKUP:
1429         case BONDING_MODE_ADAPTIVE_TRANSMIT_LOAD_BALANCING:
1430         default:
1431                 rte_eth_promiscuous_disable(internals->current_primary_port);
1432         }
1433 }
1434
1435 static void
1436 bond_ethdev_delayed_lsc_propagation(void *arg)
1437 {
1438         if (arg == NULL)
1439                 return;
1440
1441         _rte_eth_dev_callback_process((struct rte_eth_dev *)arg,
1442                         RTE_ETH_EVENT_INTR_LSC);
1443 }
1444
1445 void
1446 bond_ethdev_lsc_event_callback(uint8_t port_id, enum rte_eth_event_type type,
1447                 void *param)
1448 {
1449         struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
1450         struct bond_dev_private *internals;
1451         struct rte_eth_link link;
1452
1453         int i, valid_slave = 0;
1454         uint8_t active_pos;
1455         uint8_t lsc_flag = 0;
1456
1457         if (type != RTE_ETH_EVENT_INTR_LSC || param == NULL)
1458                 return;
1459
1460         bonded_eth_dev = &rte_eth_devices[*(uint8_t *)param];
1461         slave_eth_dev = &rte_eth_devices[port_id];
1462
1463         if (valid_bonded_ethdev(bonded_eth_dev))
1464                 return;
1465
1466         internals = bonded_eth_dev->data->dev_private;
1467
1468         /* If the device isn't started don't handle interrupts */
1469         if (!bonded_eth_dev->data->dev_started)
1470                 return;
1471
1472         /* verify that port_id is a valid slave of bonded port */
1473         for (i = 0; i < internals->slave_count; i++) {
1474                 if (internals->slaves[i].port_id == port_id) {
1475                         valid_slave = 1;
1476                         break;
1477                 }
1478         }
1479
1480         if (!valid_slave)
1481                 return;
1482
1483         /* Search for port in active port list */
1484         active_pos = find_slave_by_id(internals->active_slaves,
1485                         internals->active_slave_count, port_id);
1486
1487         rte_eth_link_get_nowait(port_id, &link);
1488         if (link.link_status) {
1489                 if (active_pos < internals->active_slave_count)
1490                         return;
1491
1492                 /* if no active slave ports then set this port to be primary port */
1493                 if (internals->active_slave_count < 1) {
1494                         /* If first active slave, then change link status */
1495                         bonded_eth_dev->data->dev_link.link_status = 1;
1496                         internals->current_primary_port = port_id;
1497                         lsc_flag = 1;
1498
1499                         /* Inherit eth dev link properties from first active slave */
1500                         link_properties_set(bonded_eth_dev,
1501                                         &(slave_eth_dev->data->dev_link));
1502                 }
1503
1504                 activate_slave(bonded_eth_dev, port_id);
1505
1506                 /* If user has defined the primary port then default to using it */
1507                 if (internals->user_defined_primary_port &&
1508                                 internals->primary_port == port_id)
1509                         bond_ethdev_primary_set(internals, port_id);
1510         } else {
1511                 if (active_pos == internals->active_slave_count)
1512                         return;
1513
1514                 /* Remove from active slave list */
1515                 deactivate_slave(bonded_eth_dev, port_id);
1516
1517                 /* No active slaves, change link status to down and reset other
1518                  * link properties */
1519                 if (internals->active_slave_count < 1) {
1520                         lsc_flag = 1;
1521                         bonded_eth_dev->data->dev_link.link_status = 0;
1522
1523                         link_properties_reset(bonded_eth_dev);
1524                 }
1525
1526                 /* Update primary id, take first active slave from list or if none
1527                  * available set to -1 */
1528                 if (port_id == internals->current_primary_port) {
1529                         if (internals->active_slave_count > 0)
1530                                 bond_ethdev_primary_set(internals,
1531                                                 internals->active_slaves[0]);
1532                         else
1533                                 internals->current_primary_port = internals->primary_port;
1534                 }
1535         }
1536
1537         if (lsc_flag) {
1538                 /* Cancel any possible outstanding interrupts if delays are enabled */
1539                 if (internals->link_up_delay_ms > 0 ||
1540                         internals->link_down_delay_ms > 0)
1541                         rte_eal_alarm_cancel(bond_ethdev_delayed_lsc_propagation,
1542                                         bonded_eth_dev);
1543
1544                 if (bonded_eth_dev->data->dev_link.link_status) {
1545                         if (internals->link_up_delay_ms > 0)
1546                                 rte_eal_alarm_set(internals->link_up_delay_ms * 1000,
1547                                                 bond_ethdev_delayed_lsc_propagation,
1548                                                 (void *)bonded_eth_dev);
1549                         else
1550                                 _rte_eth_dev_callback_process(bonded_eth_dev,
1551                                                 RTE_ETH_EVENT_INTR_LSC);
1552
1553                 } else {
1554                         if (internals->link_down_delay_ms > 0)
1555                                 rte_eal_alarm_set(internals->link_down_delay_ms * 1000,
1556                                                 bond_ethdev_delayed_lsc_propagation,
1557                                                 (void *)bonded_eth_dev);
1558                         else
1559                                 _rte_eth_dev_callback_process(bonded_eth_dev,
1560                                                 RTE_ETH_EVENT_INTR_LSC);
1561                 }
1562         }
1563 }
1564
1565 struct eth_dev_ops default_dev_ops = {
1566                 .dev_start = bond_ethdev_start,
1567                 .dev_stop = bond_ethdev_stop,
1568                 .dev_close = bond_ethdev_close,
1569                 .dev_configure = bond_ethdev_configure,
1570                 .dev_infos_get = bond_ethdev_info,
1571                 .rx_queue_setup = bond_ethdev_rx_queue_setup,
1572                 .tx_queue_setup = bond_ethdev_tx_queue_setup,
1573                 .rx_queue_release = bond_ethdev_rx_queue_release,
1574                 .tx_queue_release = bond_ethdev_tx_queue_release,
1575                 .link_update = bond_ethdev_link_update,
1576                 .stats_get = bond_ethdev_stats_get,
1577                 .stats_reset = bond_ethdev_stats_reset,
1578                 .promiscuous_enable = bond_ethdev_promiscuous_enable,
1579                 .promiscuous_disable = bond_ethdev_promiscuous_disable
1580 };
1581
1582 static int
1583 bond_init(const char *name, const char *params)
1584 {
1585         struct bond_dev_private *internals;
1586         struct rte_kvargs *kvlist;
1587         uint8_t bonding_mode, socket_id;
1588         int  arg_count, port_id;
1589
1590         RTE_LOG(INFO, EAL, "Initializing pmd_bond for %s\n", name);
1591
1592         kvlist = rte_kvargs_parse(params, pmd_bond_init_valid_arguments);
1593         if (kvlist == NULL)
1594                 return -1;
1595
1596         /* Parse link bonding mode */
1597         if (rte_kvargs_count(kvlist, PMD_BOND_MODE_KVARG) == 1) {
1598                 if (rte_kvargs_process(kvlist, PMD_BOND_MODE_KVARG,
1599                                 &bond_ethdev_parse_slave_mode_kvarg, &bonding_mode) != 0) {
1600                         RTE_LOG(ERR, EAL, "Invalid mode for bonded device %s\n", name);
1601                         return -1;
1602                 }
1603         } else {
1604                 RTE_LOG(ERR, EAL,
1605                                 "Mode must be specified only once for bonded device %s\n",
1606                                 name);
1607                 return -1;
1608         }
1609
1610         /* Parse socket id to create bonding device on */
1611         arg_count = rte_kvargs_count(kvlist, PMD_BOND_SOCKET_ID_KVARG);
1612         if (arg_count == 1) {
1613                 if (rte_kvargs_process(kvlist, PMD_BOND_SOCKET_ID_KVARG,
1614                                 &bond_ethdev_parse_socket_id_kvarg, &socket_id) != 0) {
1615                         RTE_LOG(ERR, EAL,
1616                                         "Invalid socket Id specified for bonded device %s\n",
1617                                         name);
1618                         return -1;
1619                 }
1620         } else if (arg_count > 1) {
1621                 RTE_LOG(ERR, EAL,
1622                                 "Socket Id can be specified only once for bonded device %s\n",
1623                                 name);
1624                 return -1;
1625         } else {
1626                 socket_id = rte_socket_id();
1627         }
1628
1629         /* Create link bonding eth device */
1630         port_id = rte_eth_bond_create(name, bonding_mode, socket_id);
1631         if (port_id < 0) {
1632                 RTE_LOG(ERR, EAL,
1633                                 "Failed to create socket %s in mode %u on socket %u.\n",
1634                                 name, bonding_mode, socket_id);
1635                 return -1;
1636         }
1637         internals = rte_eth_devices[port_id].data->dev_private;
1638         internals->kvlist = kvlist;
1639
1640         RTE_LOG(INFO, EAL,
1641                         "Create bonded device %s on port %d in mode %u on socket %u.\n",
1642                         name, port_id, bonding_mode, socket_id);
1643         return 0;
1644 }
1645
1646 /* this part will resolve the slave portids after all the other pdev and vdev
1647  * have been allocated */
1648 static int
1649 bond_ethdev_configure(struct rte_eth_dev *dev)
1650 {
1651         char *name = dev->data->name;
1652         struct bond_dev_private *internals = dev->data->dev_private;
1653         struct rte_kvargs *kvlist = internals->kvlist;
1654         int arg_count, port_id = dev - rte_eth_devices;
1655
1656         /*
1657          * if no kvlist, it means that this bonded device has been created
1658          * through the bonding api.
1659          */
1660         if (!kvlist)
1661                 return 0;
1662
1663         /* Parse MAC address for bonded device */
1664         arg_count = rte_kvargs_count(kvlist, PMD_BOND_MAC_ADDR_KVARG);
1665         if (arg_count == 1) {
1666                 struct ether_addr bond_mac;
1667
1668                 if (rte_kvargs_process(kvlist, PMD_BOND_MAC_ADDR_KVARG,
1669                                 &bond_ethdev_parse_bond_mac_addr_kvarg, &bond_mac) < 0) {
1670                         RTE_LOG(INFO, EAL, "Invalid mac address for bonded device %s\n",
1671                                         name);
1672                         return -1;
1673                 }
1674
1675                 /* Set MAC address */
1676                 if (rte_eth_bond_mac_address_set(port_id, &bond_mac) != 0) {
1677                         RTE_LOG(ERR, EAL,
1678                                         "Failed to set mac address on bonded device %s\n",
1679                                         name);
1680                         return -1;
1681                 }
1682         } else if (arg_count > 1) {
1683                 RTE_LOG(ERR, EAL,
1684                                 "MAC address can be specified only once for bonded device %s\n",
1685                                 name);
1686                 return -1;
1687         }
1688
1689         /* Parse/set balance mode transmit policy */
1690         arg_count = rte_kvargs_count(kvlist, PMD_BOND_XMIT_POLICY_KVARG);
1691         if (arg_count == 1) {
1692                 uint8_t xmit_policy;
1693
1694                 if (rte_kvargs_process(kvlist, PMD_BOND_XMIT_POLICY_KVARG,
1695                                 &bond_ethdev_parse_balance_xmit_policy_kvarg, &xmit_policy) !=
1696                                                 0) {
1697                         RTE_LOG(INFO, EAL,
1698                                         "Invalid xmit policy specified for bonded device %s\n",
1699                                         name);
1700                         return -1;
1701                 }
1702
1703                 /* Set balance mode transmit policy*/
1704                 if (rte_eth_bond_xmit_policy_set(port_id, xmit_policy) != 0) {
1705                         RTE_LOG(ERR, EAL,
1706                                         "Failed to set balance xmit policy on bonded device %s\n",
1707                                         name);
1708                         return -1;
1709                 }
1710         } else if (arg_count > 1) {
1711                 RTE_LOG(ERR, EAL,
1712                                 "Transmit policy can be specified only once for bonded device"
1713                                 " %s\n", name);
1714                 return -1;
1715         }
1716
1717         /* Parse/add slave ports to bonded device */
1718         if (rte_kvargs_count(kvlist, PMD_BOND_SLAVE_PORT_KVARG) > 0) {
1719                 struct bond_ethdev_slave_ports slave_ports;
1720                 unsigned i;
1721
1722                 memset(&slave_ports, 0, sizeof(slave_ports));
1723
1724                 if (rte_kvargs_process(kvlist, PMD_BOND_SLAVE_PORT_KVARG,
1725                                 &bond_ethdev_parse_slave_port_kvarg, &slave_ports) != 0) {
1726                         RTE_LOG(ERR, EAL,
1727                                         "Failed to parse slave ports for bonded device %s\n",
1728                                         name);
1729                         return -1;
1730                 }
1731
1732                 for (i = 0; i < slave_ports.slave_count; i++) {
1733                         if (rte_eth_bond_slave_add(port_id, slave_ports.slaves[i]) != 0) {
1734                                 RTE_LOG(ERR, EAL,
1735                                                 "Failed to add port %d as slave to bonded device %s\n",
1736                                                 slave_ports.slaves[i], name);
1737                         }
1738                 }
1739
1740         } else {
1741                 RTE_LOG(INFO, EAL, "No slaves specified for bonded device %s\n", name);
1742                 return -1;
1743         }
1744
1745         /* Parse/set primary slave port id*/
1746         arg_count = rte_kvargs_count(kvlist, PMD_BOND_PRIMARY_SLAVE_KVARG);
1747         if (arg_count == 1) {
1748                 uint8_t primary_slave_port_id;
1749
1750                 if (rte_kvargs_process(kvlist,
1751                                 PMD_BOND_PRIMARY_SLAVE_KVARG,
1752                                 &bond_ethdev_parse_primary_slave_port_id_kvarg,
1753                                 &primary_slave_port_id) < 0) {
1754                         RTE_LOG(INFO, EAL,
1755                                         "Invalid primary slave port id specified for bonded device"
1756                                         " %s\n", name);
1757                         return -1;
1758                 }
1759
1760                 /* Set balance mode transmit policy*/
1761                 if (rte_eth_bond_primary_set(port_id, (uint8_t)primary_slave_port_id)
1762                                 != 0) {
1763                         RTE_LOG(ERR, EAL,
1764                                         "Failed to set primary slave port %d on bonded device %s\n",
1765                                         primary_slave_port_id, name);
1766                         return -1;
1767                 }
1768         } else if (arg_count > 1) {
1769                 RTE_LOG(INFO, EAL,
1770                                 "Primary slave can be specified only once for bonded device"
1771                                 " %s\n", name);
1772                 return -1;
1773         }
1774
1775         /* Parse link status monitor polling interval */
1776         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LSC_POLL_PERIOD_KVARG);
1777         if (arg_count == 1) {
1778                 uint32_t lsc_poll_interval_ms;
1779
1780                 if (rte_kvargs_process(kvlist,
1781                                 PMD_BOND_LSC_POLL_PERIOD_KVARG,
1782                                 &bond_ethdev_parse_time_ms_kvarg,
1783                                 &lsc_poll_interval_ms) < 0) {
1784                         RTE_LOG(INFO, EAL,
1785                                         "Invalid lsc polling interval value specified for bonded"
1786                                         " device %s\n", name);
1787                         return -1;
1788                 }
1789
1790                 if (rte_eth_bond_link_monitoring_set(port_id, lsc_poll_interval_ms)
1791                                 != 0) {
1792                         RTE_LOG(ERR, EAL,
1793                                         "Failed to set lsc monitor polling interval (%u ms) on"
1794                                         " bonded device %s\n", lsc_poll_interval_ms, name);
1795                         return -1;
1796                 }
1797         } else if (arg_count > 1) {
1798                 RTE_LOG(INFO, EAL,
1799                                 "LSC polling interval can be specified only once for bonded"
1800                                 " device %s\n", name);
1801                 return -1;
1802         }
1803
1804         /* Parse link up interrupt propagation delay */
1805         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LINK_UP_PROP_DELAY_KVARG);
1806         if (arg_count == 1) {
1807                 uint32_t link_up_delay_ms;
1808
1809                 if (rte_kvargs_process(kvlist,
1810                                 PMD_BOND_LINK_UP_PROP_DELAY_KVARG,
1811                                 &bond_ethdev_parse_time_ms_kvarg,
1812                                 &link_up_delay_ms) < 0) {
1813                         RTE_LOG(INFO, EAL,
1814                                         "Invalid link up propagation delay value specified for"
1815                                         " bonded device %s\n", name);
1816                         return -1;
1817                 }
1818
1819                 /* Set balance mode transmit policy*/
1820                 if (rte_eth_bond_link_up_prop_delay_set(port_id, link_up_delay_ms)
1821                                 != 0) {
1822                         RTE_LOG(ERR, EAL,
1823                                         "Failed to set link up propagation delay (%u ms) on bonded"
1824                                         " device %s\n", link_up_delay_ms, name);
1825                         return -1;
1826                 }
1827         } else if (arg_count > 1) {
1828                 RTE_LOG(INFO, EAL,
1829                                 "Link up propagation delay can be specified only once for"
1830                                 " bonded device %s\n", name);
1831                 return -1;
1832         }
1833
1834         /* Parse link down interrupt propagation delay */
1835         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG);
1836         if (arg_count == 1) {
1837                 uint32_t link_down_delay_ms;
1838
1839                 if (rte_kvargs_process(kvlist,
1840                                 PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG,
1841                                 &bond_ethdev_parse_time_ms_kvarg,
1842                                 &link_down_delay_ms) < 0) {
1843                         RTE_LOG(INFO, EAL,
1844                                         "Invalid link down propagation delay value specified for"
1845                                         " bonded device %s\n", name);
1846                         return -1;
1847                 }
1848
1849                 /* Set balance mode transmit policy*/
1850                 if (rte_eth_bond_link_down_prop_delay_set(port_id, link_down_delay_ms)
1851                                 != 0) {
1852                         RTE_LOG(ERR, EAL,
1853                                         "Failed to set link down propagation delay (%u ms) on"
1854                                         " bonded device %s\n", link_down_delay_ms, name);
1855                         return -1;
1856                 }
1857         } else if (arg_count > 1) {
1858                 RTE_LOG(INFO, EAL,
1859                                 "Link down propagation delay can be specified only once for"
1860                                 " bonded device %s\n", name);
1861                 return -1;
1862         }
1863
1864         return 0;
1865 }
1866
1867 static struct rte_driver bond_drv = {
1868         .name = "eth_bond",
1869         .type = PMD_VDEV,
1870         .init = bond_init,
1871 };
1872
1873 PMD_REGISTER_DRIVER(bond_drv);