ethdev: change promiscuous callbacks to return status
[dpdk.git] / drivers / net / bonding / rte_eth_bond_pmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4 #include <stdlib.h>
5 #include <stdbool.h>
6 #include <netinet/in.h>
7
8 #include <rte_mbuf.h>
9 #include <rte_malloc.h>
10 #include <rte_ethdev_driver.h>
11 #include <rte_ethdev_vdev.h>
12 #include <rte_tcp.h>
13 #include <rte_udp.h>
14 #include <rte_ip.h>
15 #include <rte_ip_frag.h>
16 #include <rte_devargs.h>
17 #include <rte_kvargs.h>
18 #include <rte_bus_vdev.h>
19 #include <rte_alarm.h>
20 #include <rte_cycles.h>
21 #include <rte_string_fns.h>
22
23 #include "rte_eth_bond.h"
24 #include "rte_eth_bond_private.h"
25 #include "rte_eth_bond_8023ad_private.h"
26
27 #define REORDER_PERIOD_MS 10
28 #define DEFAULT_POLLING_INTERVAL_10_MS (10)
29 #define BOND_MAX_MAC_ADDRS 16
30
31 #define HASH_L4_PORTS(h) ((h)->src_port ^ (h)->dst_port)
32
33 /* Table for statistics in mode 5 TLB */
34 static uint64_t tlb_last_obytets[RTE_MAX_ETHPORTS];
35
36 static inline size_t
37 get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto)
38 {
39         size_t vlan_offset = 0;
40
41         if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto ||
42                 rte_cpu_to_be_16(RTE_ETHER_TYPE_QINQ) == *proto) {
43                 struct rte_vlan_hdr *vlan_hdr =
44                         (struct rte_vlan_hdr *)(eth_hdr + 1);
45
46                 vlan_offset = sizeof(struct rte_vlan_hdr);
47                 *proto = vlan_hdr->eth_proto;
48
49                 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
50                         vlan_hdr = vlan_hdr + 1;
51                         *proto = vlan_hdr->eth_proto;
52                         vlan_offset += sizeof(struct rte_vlan_hdr);
53                 }
54         }
55         return vlan_offset;
56 }
57
58 static uint16_t
59 bond_ethdev_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
60 {
61         struct bond_dev_private *internals;
62
63         uint16_t num_rx_total = 0;
64         uint16_t slave_count;
65         uint16_t active_slave;
66         int i;
67
68         /* Cast to structure, containing bonded device's port id and queue id */
69         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
70         internals = bd_rx_q->dev_private;
71         slave_count = internals->active_slave_count;
72         active_slave = internals->active_slave;
73
74         for (i = 0; i < slave_count && nb_pkts; i++) {
75                 uint16_t num_rx_slave;
76
77                 /* Offset of pointer to *bufs increases as packets are received
78                  * from other slaves */
79                 num_rx_slave =
80                         rte_eth_rx_burst(internals->active_slaves[active_slave],
81                                          bd_rx_q->queue_id,
82                                          bufs + num_rx_total, nb_pkts);
83                 num_rx_total += num_rx_slave;
84                 nb_pkts -= num_rx_slave;
85                 if (++active_slave == slave_count)
86                         active_slave = 0;
87         }
88
89         if (++internals->active_slave >= slave_count)
90                 internals->active_slave = 0;
91         return num_rx_total;
92 }
93
94 static uint16_t
95 bond_ethdev_rx_burst_active_backup(void *queue, struct rte_mbuf **bufs,
96                 uint16_t nb_pkts)
97 {
98         struct bond_dev_private *internals;
99
100         /* Cast to structure, containing bonded device's port id and queue id */
101         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
102
103         internals = bd_rx_q->dev_private;
104
105         return rte_eth_rx_burst(internals->current_primary_port,
106                         bd_rx_q->queue_id, bufs, nb_pkts);
107 }
108
109 static inline uint8_t
110 is_lacp_packets(uint16_t ethertype, uint8_t subtype, struct rte_mbuf *mbuf)
111 {
112         const uint16_t ether_type_slow_be =
113                 rte_be_to_cpu_16(RTE_ETHER_TYPE_SLOW);
114
115         return !((mbuf->ol_flags & PKT_RX_VLAN) ? mbuf->vlan_tci : 0) &&
116                 (ethertype == ether_type_slow_be &&
117                 (subtype == SLOW_SUBTYPE_MARKER || subtype == SLOW_SUBTYPE_LACP));
118 }
119
120 /*****************************************************************************
121  * Flow director's setup for mode 4 optimization
122  */
123
124 static struct rte_flow_item_eth flow_item_eth_type_8023ad = {
125         .dst.addr_bytes = { 0 },
126         .src.addr_bytes = { 0 },
127         .type = RTE_BE16(RTE_ETHER_TYPE_SLOW),
128 };
129
130 static struct rte_flow_item_eth flow_item_eth_mask_type_8023ad = {
131         .dst.addr_bytes = { 0 },
132         .src.addr_bytes = { 0 },
133         .type = 0xFFFF,
134 };
135
136 static struct rte_flow_item flow_item_8023ad[] = {
137         {
138                 .type = RTE_FLOW_ITEM_TYPE_ETH,
139                 .spec = &flow_item_eth_type_8023ad,
140                 .last = NULL,
141                 .mask = &flow_item_eth_mask_type_8023ad,
142         },
143         {
144                 .type = RTE_FLOW_ITEM_TYPE_END,
145                 .spec = NULL,
146                 .last = NULL,
147                 .mask = NULL,
148         }
149 };
150
151 const struct rte_flow_attr flow_attr_8023ad = {
152         .group = 0,
153         .priority = 0,
154         .ingress = 1,
155         .egress = 0,
156         .reserved = 0,
157 };
158
159 int
160 bond_ethdev_8023ad_flow_verify(struct rte_eth_dev *bond_dev,
161                 uint16_t slave_port) {
162         struct rte_eth_dev_info slave_info;
163         struct rte_flow_error error;
164         struct bond_dev_private *internals = bond_dev->data->dev_private;
165
166         const struct rte_flow_action_queue lacp_queue_conf = {
167                 .index = 0,
168         };
169
170         const struct rte_flow_action actions[] = {
171                 {
172                         .type = RTE_FLOW_ACTION_TYPE_QUEUE,
173                         .conf = &lacp_queue_conf
174                 },
175                 {
176                         .type = RTE_FLOW_ACTION_TYPE_END,
177                 }
178         };
179
180         int ret = rte_flow_validate(slave_port, &flow_attr_8023ad,
181                         flow_item_8023ad, actions, &error);
182         if (ret < 0) {
183                 RTE_BOND_LOG(ERR, "%s: %s (slave_port=%d queue_id=%d)",
184                                 __func__, error.message, slave_port,
185                                 internals->mode4.dedicated_queues.rx_qid);
186                 return -1;
187         }
188
189         ret = rte_eth_dev_info_get(slave_port, &slave_info);
190         if (ret != 0) {
191                 RTE_BOND_LOG(ERR,
192                         "%s: Error during getting device (port %u) info: %s\n",
193                         __func__, slave_port, strerror(-ret));
194
195                 return ret;
196         }
197
198         if (slave_info.max_rx_queues < bond_dev->data->nb_rx_queues ||
199                         slave_info.max_tx_queues < bond_dev->data->nb_tx_queues) {
200                 RTE_BOND_LOG(ERR,
201                         "%s: Slave %d capabilities doesn't allow to allocate additional queues",
202                         __func__, slave_port);
203                 return -1;
204         }
205
206         return 0;
207 }
208
209 int
210 bond_8023ad_slow_pkt_hw_filter_supported(uint16_t port_id) {
211         struct rte_eth_dev *bond_dev = &rte_eth_devices[port_id];
212         struct bond_dev_private *internals = bond_dev->data->dev_private;
213         struct rte_eth_dev_info bond_info;
214         uint16_t idx;
215         int ret;
216
217         /* Verify if all slaves in bonding supports flow director and */
218         if (internals->slave_count > 0) {
219                 ret = rte_eth_dev_info_get(bond_dev->data->port_id, &bond_info);
220                 if (ret != 0) {
221                         RTE_BOND_LOG(ERR,
222                                 "%s: Error during getting device (port %u) info: %s\n",
223                                 __func__, bond_dev->data->port_id,
224                                 strerror(-ret));
225
226                         return ret;
227                 }
228
229                 internals->mode4.dedicated_queues.rx_qid = bond_info.nb_rx_queues;
230                 internals->mode4.dedicated_queues.tx_qid = bond_info.nb_tx_queues;
231
232                 for (idx = 0; idx < internals->slave_count; idx++) {
233                         if (bond_ethdev_8023ad_flow_verify(bond_dev,
234                                         internals->slaves[idx].port_id) != 0)
235                                 return -1;
236                 }
237         }
238
239         return 0;
240 }
241
242 int
243 bond_ethdev_8023ad_flow_set(struct rte_eth_dev *bond_dev, uint16_t slave_port) {
244
245         struct rte_flow_error error;
246         struct bond_dev_private *internals = bond_dev->data->dev_private;
247         struct rte_flow_action_queue lacp_queue_conf = {
248                 .index = internals->mode4.dedicated_queues.rx_qid,
249         };
250
251         const struct rte_flow_action actions[] = {
252                 {
253                         .type = RTE_FLOW_ACTION_TYPE_QUEUE,
254                         .conf = &lacp_queue_conf
255                 },
256                 {
257                         .type = RTE_FLOW_ACTION_TYPE_END,
258                 }
259         };
260
261         internals->mode4.dedicated_queues.flow[slave_port] = rte_flow_create(slave_port,
262                         &flow_attr_8023ad, flow_item_8023ad, actions, &error);
263         if (internals->mode4.dedicated_queues.flow[slave_port] == NULL) {
264                 RTE_BOND_LOG(ERR, "bond_ethdev_8023ad_flow_set: %s "
265                                 "(slave_port=%d queue_id=%d)",
266                                 error.message, slave_port,
267                                 internals->mode4.dedicated_queues.rx_qid);
268                 return -1;
269         }
270
271         return 0;
272 }
273
274 static inline uint16_t
275 rx_burst_8023ad(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts,
276                 bool dedicated_rxq)
277 {
278         /* Cast to structure, containing bonded device's port id and queue id */
279         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
280         struct bond_dev_private *internals = bd_rx_q->dev_private;
281         struct rte_eth_dev *bonded_eth_dev =
282                                         &rte_eth_devices[internals->port_id];
283         struct rte_ether_addr *bond_mac = bonded_eth_dev->data->mac_addrs;
284         struct rte_ether_hdr *hdr;
285
286         const uint16_t ether_type_slow_be =
287                 rte_be_to_cpu_16(RTE_ETHER_TYPE_SLOW);
288         uint16_t num_rx_total = 0;      /* Total number of received packets */
289         uint16_t slaves[RTE_MAX_ETHPORTS];
290         uint16_t slave_count, idx;
291
292         uint8_t collecting;  /* current slave collecting status */
293         const uint8_t promisc = rte_eth_promiscuous_get(internals->port_id);
294         const uint8_t allmulti = rte_eth_allmulticast_get(internals->port_id);
295         uint8_t subtype;
296         uint16_t i;
297         uint16_t j;
298         uint16_t k;
299
300         /* Copy slave list to protect against slave up/down changes during tx
301          * bursting */
302         slave_count = internals->active_slave_count;
303         memcpy(slaves, internals->active_slaves,
304                         sizeof(internals->active_slaves[0]) * slave_count);
305
306         idx = internals->active_slave;
307         if (idx >= slave_count) {
308                 internals->active_slave = 0;
309                 idx = 0;
310         }
311         for (i = 0; i < slave_count && num_rx_total < nb_pkts; i++) {
312                 j = num_rx_total;
313                 collecting = ACTOR_STATE(&bond_mode_8023ad_ports[slaves[idx]],
314                                          COLLECTING);
315
316                 /* Read packets from this slave */
317                 num_rx_total += rte_eth_rx_burst(slaves[idx], bd_rx_q->queue_id,
318                                 &bufs[num_rx_total], nb_pkts - num_rx_total);
319
320                 for (k = j; k < 2 && k < num_rx_total; k++)
321                         rte_prefetch0(rte_pktmbuf_mtod(bufs[k], void *));
322
323                 /* Handle slow protocol packets. */
324                 while (j < num_rx_total) {
325                         if (j + 3 < num_rx_total)
326                                 rte_prefetch0(rte_pktmbuf_mtod(bufs[j + 3], void *));
327
328                         hdr = rte_pktmbuf_mtod(bufs[j], struct rte_ether_hdr *);
329                         subtype = ((struct slow_protocol_frame *)hdr)->slow_protocol.subtype;
330
331                         /* Remove packet from array if:
332                          * - it is slow packet but no dedicated rxq is present,
333                          * - slave is not in collecting state,
334                          * - bonding interface is not in promiscuous mode:
335                          *   - packet is unicast and address does not match,
336                          *   - packet is multicast and bonding interface
337                          *     is not in allmulti,
338                          */
339                         if (unlikely(
340                                 (!dedicated_rxq &&
341                                  is_lacp_packets(hdr->ether_type, subtype,
342                                                  bufs[j])) ||
343                                 !collecting ||
344                                 (!promisc &&
345                                  ((rte_is_unicast_ether_addr(&hdr->d_addr) &&
346                                    !rte_is_same_ether_addr(bond_mac,
347                                                        &hdr->d_addr)) ||
348                                   (!allmulti &&
349                                    rte_is_multicast_ether_addr(&hdr->d_addr)))))) {
350
351                                 if (hdr->ether_type == ether_type_slow_be) {
352                                         bond_mode_8023ad_handle_slow_pkt(
353                                             internals, slaves[idx], bufs[j]);
354                                 } else
355                                         rte_pktmbuf_free(bufs[j]);
356
357                                 /* Packet is managed by mode 4 or dropped, shift the array */
358                                 num_rx_total--;
359                                 if (j < num_rx_total) {
360                                         memmove(&bufs[j], &bufs[j + 1], sizeof(bufs[0]) *
361                                                 (num_rx_total - j));
362                                 }
363                         } else
364                                 j++;
365                 }
366                 if (unlikely(++idx == slave_count))
367                         idx = 0;
368         }
369
370         if (++internals->active_slave >= slave_count)
371                 internals->active_slave = 0;
372
373         return num_rx_total;
374 }
375
376 static uint16_t
377 bond_ethdev_rx_burst_8023ad(void *queue, struct rte_mbuf **bufs,
378                 uint16_t nb_pkts)
379 {
380         return rx_burst_8023ad(queue, bufs, nb_pkts, false);
381 }
382
383 static uint16_t
384 bond_ethdev_rx_burst_8023ad_fast_queue(void *queue, struct rte_mbuf **bufs,
385                 uint16_t nb_pkts)
386 {
387         return rx_burst_8023ad(queue, bufs, nb_pkts, true);
388 }
389
390 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
391 uint32_t burstnumberRX;
392 uint32_t burstnumberTX;
393
394 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
395
396 static void
397 arp_op_name(uint16_t arp_op, char *buf, size_t buf_len)
398 {
399         switch (arp_op) {
400         case RTE_ARP_OP_REQUEST:
401                 strlcpy(buf, "ARP Request", buf_len);
402                 return;
403         case RTE_ARP_OP_REPLY:
404                 strlcpy(buf, "ARP Reply", buf_len);
405                 return;
406         case RTE_ARP_OP_REVREQUEST:
407                 strlcpy(buf, "Reverse ARP Request", buf_len);
408                 return;
409         case RTE_ARP_OP_REVREPLY:
410                 strlcpy(buf, "Reverse ARP Reply", buf_len);
411                 return;
412         case RTE_ARP_OP_INVREQUEST:
413                 strlcpy(buf, "Peer Identify Request", buf_len);
414                 return;
415         case RTE_ARP_OP_INVREPLY:
416                 strlcpy(buf, "Peer Identify Reply", buf_len);
417                 return;
418         default:
419                 break;
420         }
421         strlcpy(buf, "Unknown", buf_len);
422         return;
423 }
424 #endif
425 #define MaxIPv4String   16
426 static void
427 ipv4_addr_to_dot(uint32_t be_ipv4_addr, char *buf, uint8_t buf_size)
428 {
429         uint32_t ipv4_addr;
430
431         ipv4_addr = rte_be_to_cpu_32(be_ipv4_addr);
432         snprintf(buf, buf_size, "%d.%d.%d.%d", (ipv4_addr >> 24) & 0xFF,
433                 (ipv4_addr >> 16) & 0xFF, (ipv4_addr >> 8) & 0xFF,
434                 ipv4_addr & 0xFF);
435 }
436
437 #define MAX_CLIENTS_NUMBER      128
438 uint8_t active_clients;
439 struct client_stats_t {
440         uint16_t port;
441         uint32_t ipv4_addr;
442         uint32_t ipv4_rx_packets;
443         uint32_t ipv4_tx_packets;
444 };
445 struct client_stats_t client_stats[MAX_CLIENTS_NUMBER];
446
447 static void
448 update_client_stats(uint32_t addr, uint16_t port, uint32_t *TXorRXindicator)
449 {
450         int i = 0;
451
452         for (; i < MAX_CLIENTS_NUMBER; i++)     {
453                 if ((client_stats[i].ipv4_addr == addr) && (client_stats[i].port == port))      {
454                         /* Just update RX packets number for this client */
455                         if (TXorRXindicator == &burstnumberRX)
456                                 client_stats[i].ipv4_rx_packets++;
457                         else
458                                 client_stats[i].ipv4_tx_packets++;
459                         return;
460                 }
461         }
462         /* We have a new client. Insert him to the table, and increment stats */
463         if (TXorRXindicator == &burstnumberRX)
464                 client_stats[active_clients].ipv4_rx_packets++;
465         else
466                 client_stats[active_clients].ipv4_tx_packets++;
467         client_stats[active_clients].ipv4_addr = addr;
468         client_stats[active_clients].port = port;
469         active_clients++;
470
471 }
472
473 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
474 #define MODE6_DEBUG(info, src_ip, dst_ip, eth_h, arp_op, port, burstnumber) \
475         rte_log(RTE_LOG_DEBUG, bond_logtype,                            \
476                 "%s port:%d SrcMAC:%02X:%02X:%02X:%02X:%02X:%02X SrcIP:%s " \
477                 "DstMAC:%02X:%02X:%02X:%02X:%02X:%02X DstIP:%s %s %d\n", \
478                 info,                                                   \
479                 port,                                                   \
480                 eth_h->s_addr.addr_bytes[0], eth_h->s_addr.addr_bytes[1], \
481                 eth_h->s_addr.addr_bytes[2], eth_h->s_addr.addr_bytes[3], \
482                 eth_h->s_addr.addr_bytes[4], eth_h->s_addr.addr_bytes[5], \
483                 src_ip,                                                 \
484                 eth_h->d_addr.addr_bytes[0], eth_h->d_addr.addr_bytes[1], \
485                 eth_h->d_addr.addr_bytes[2], eth_h->d_addr.addr_bytes[3], \
486                 eth_h->d_addr.addr_bytes[4], eth_h->d_addr.addr_bytes[5], \
487                 dst_ip,                                                 \
488                 arp_op, ++burstnumber)
489 #endif
490
491 static void
492 mode6_debug(const char __attribute__((unused)) *info,
493         struct rte_ether_hdr *eth_h, uint16_t port,
494         uint32_t __attribute__((unused)) *burstnumber)
495 {
496         struct rte_ipv4_hdr *ipv4_h;
497 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
498         struct rte_arp_hdr *arp_h;
499         char dst_ip[16];
500         char ArpOp[24];
501         char buf[16];
502 #endif
503         char src_ip[16];
504
505         uint16_t ether_type = eth_h->ether_type;
506         uint16_t offset = get_vlan_offset(eth_h, &ether_type);
507
508 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
509         strlcpy(buf, info, 16);
510 #endif
511
512         if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
513                 ipv4_h = (struct rte_ipv4_hdr *)((char *)(eth_h + 1) + offset);
514                 ipv4_addr_to_dot(ipv4_h->src_addr, src_ip, MaxIPv4String);
515 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
516                 ipv4_addr_to_dot(ipv4_h->dst_addr, dst_ip, MaxIPv4String);
517                 MODE6_DEBUG(buf, src_ip, dst_ip, eth_h, "", port, *burstnumber);
518 #endif
519                 update_client_stats(ipv4_h->src_addr, port, burstnumber);
520         }
521 #ifdef RTE_LIBRTE_BOND_DEBUG_ALB
522         else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
523                 arp_h = (struct rte_arp_hdr *)((char *)(eth_h + 1) + offset);
524                 ipv4_addr_to_dot(arp_h->arp_data.arp_sip, src_ip, MaxIPv4String);
525                 ipv4_addr_to_dot(arp_h->arp_data.arp_tip, dst_ip, MaxIPv4String);
526                 arp_op_name(rte_be_to_cpu_16(arp_h->arp_opcode),
527                                 ArpOp, sizeof(ArpOp));
528                 MODE6_DEBUG(buf, src_ip, dst_ip, eth_h, ArpOp, port, *burstnumber);
529         }
530 #endif
531 }
532 #endif
533
534 static uint16_t
535 bond_ethdev_rx_burst_alb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
536 {
537         struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
538         struct bond_dev_private *internals = bd_tx_q->dev_private;
539         struct rte_ether_hdr *eth_h;
540         uint16_t ether_type, offset;
541         uint16_t nb_recv_pkts;
542         int i;
543
544         nb_recv_pkts = bond_ethdev_rx_burst(queue, bufs, nb_pkts);
545
546         for (i = 0; i < nb_recv_pkts; i++) {
547                 eth_h = rte_pktmbuf_mtod(bufs[i], struct rte_ether_hdr *);
548                 ether_type = eth_h->ether_type;
549                 offset = get_vlan_offset(eth_h, &ether_type);
550
551                 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
552 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
553                         mode6_debug("RX ARP:", eth_h, bufs[i]->port, &burstnumberRX);
554 #endif
555                         bond_mode_alb_arp_recv(eth_h, offset, internals);
556                 }
557 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
558                 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
559                         mode6_debug("RX IPv4:", eth_h, bufs[i]->port, &burstnumberRX);
560 #endif
561         }
562
563         return nb_recv_pkts;
564 }
565
566 static uint16_t
567 bond_ethdev_tx_burst_round_robin(void *queue, struct rte_mbuf **bufs,
568                 uint16_t nb_pkts)
569 {
570         struct bond_dev_private *internals;
571         struct bond_tx_queue *bd_tx_q;
572
573         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
574         uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
575
576         uint16_t num_of_slaves;
577         uint16_t slaves[RTE_MAX_ETHPORTS];
578
579         uint16_t num_tx_total = 0, num_tx_slave;
580
581         static int slave_idx = 0;
582         int i, cslave_idx = 0, tx_fail_total = 0;
583
584         bd_tx_q = (struct bond_tx_queue *)queue;
585         internals = bd_tx_q->dev_private;
586
587         /* Copy slave list to protect against slave up/down changes during tx
588          * bursting */
589         num_of_slaves = internals->active_slave_count;
590         memcpy(slaves, internals->active_slaves,
591                         sizeof(internals->active_slaves[0]) * num_of_slaves);
592
593         if (num_of_slaves < 1)
594                 return num_tx_total;
595
596         /* Populate slaves mbuf with which packets are to be sent on it  */
597         for (i = 0; i < nb_pkts; i++) {
598                 cslave_idx = (slave_idx + i) % num_of_slaves;
599                 slave_bufs[cslave_idx][(slave_nb_pkts[cslave_idx])++] = bufs[i];
600         }
601
602         /* increment current slave index so the next call to tx burst starts on the
603          * next slave */
604         slave_idx = ++cslave_idx;
605
606         /* Send packet burst on each slave device */
607         for (i = 0; i < num_of_slaves; i++) {
608                 if (slave_nb_pkts[i] > 0) {
609                         num_tx_slave = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
610                                         slave_bufs[i], slave_nb_pkts[i]);
611
612                         /* if tx burst fails move packets to end of bufs */
613                         if (unlikely(num_tx_slave < slave_nb_pkts[i])) {
614                                 int tx_fail_slave = slave_nb_pkts[i] - num_tx_slave;
615
616                                 tx_fail_total += tx_fail_slave;
617
618                                 memcpy(&bufs[nb_pkts - tx_fail_total],
619                                        &slave_bufs[i][num_tx_slave],
620                                        tx_fail_slave * sizeof(bufs[0]));
621                         }
622                         num_tx_total += num_tx_slave;
623                 }
624         }
625
626         return num_tx_total;
627 }
628
629 static uint16_t
630 bond_ethdev_tx_burst_active_backup(void *queue,
631                 struct rte_mbuf **bufs, uint16_t nb_pkts)
632 {
633         struct bond_dev_private *internals;
634         struct bond_tx_queue *bd_tx_q;
635
636         bd_tx_q = (struct bond_tx_queue *)queue;
637         internals = bd_tx_q->dev_private;
638
639         if (internals->active_slave_count < 1)
640                 return 0;
641
642         return rte_eth_tx_burst(internals->current_primary_port, bd_tx_q->queue_id,
643                         bufs, nb_pkts);
644 }
645
646 static inline uint16_t
647 ether_hash(struct rte_ether_hdr *eth_hdr)
648 {
649         unaligned_uint16_t *word_src_addr =
650                 (unaligned_uint16_t *)eth_hdr->s_addr.addr_bytes;
651         unaligned_uint16_t *word_dst_addr =
652                 (unaligned_uint16_t *)eth_hdr->d_addr.addr_bytes;
653
654         return (word_src_addr[0] ^ word_dst_addr[0]) ^
655                         (word_src_addr[1] ^ word_dst_addr[1]) ^
656                         (word_src_addr[2] ^ word_dst_addr[2]);
657 }
658
659 static inline uint32_t
660 ipv4_hash(struct rte_ipv4_hdr *ipv4_hdr)
661 {
662         return ipv4_hdr->src_addr ^ ipv4_hdr->dst_addr;
663 }
664
665 static inline uint32_t
666 ipv6_hash(struct rte_ipv6_hdr *ipv6_hdr)
667 {
668         unaligned_uint32_t *word_src_addr =
669                 (unaligned_uint32_t *)&(ipv6_hdr->src_addr[0]);
670         unaligned_uint32_t *word_dst_addr =
671                 (unaligned_uint32_t *)&(ipv6_hdr->dst_addr[0]);
672
673         return (word_src_addr[0] ^ word_dst_addr[0]) ^
674                         (word_src_addr[1] ^ word_dst_addr[1]) ^
675                         (word_src_addr[2] ^ word_dst_addr[2]) ^
676                         (word_src_addr[3] ^ word_dst_addr[3]);
677 }
678
679
680 void
681 burst_xmit_l2_hash(struct rte_mbuf **buf, uint16_t nb_pkts,
682                 uint16_t slave_count, uint16_t *slaves)
683 {
684         struct rte_ether_hdr *eth_hdr;
685         uint32_t hash;
686         int i;
687
688         for (i = 0; i < nb_pkts; i++) {
689                 eth_hdr = rte_pktmbuf_mtod(buf[i], struct rte_ether_hdr *);
690
691                 hash = ether_hash(eth_hdr);
692
693                 slaves[i] = (hash ^= hash >> 8) % slave_count;
694         }
695 }
696
697 void
698 burst_xmit_l23_hash(struct rte_mbuf **buf, uint16_t nb_pkts,
699                 uint16_t slave_count, uint16_t *slaves)
700 {
701         uint16_t i;
702         struct rte_ether_hdr *eth_hdr;
703         uint16_t proto;
704         size_t vlan_offset;
705         uint32_t hash, l3hash;
706
707         for (i = 0; i < nb_pkts; i++) {
708                 eth_hdr = rte_pktmbuf_mtod(buf[i], struct rte_ether_hdr *);
709                 l3hash = 0;
710
711                 proto = eth_hdr->ether_type;
712                 hash = ether_hash(eth_hdr);
713
714                 vlan_offset = get_vlan_offset(eth_hdr, &proto);
715
716                 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4) == proto) {
717                         struct rte_ipv4_hdr *ipv4_hdr = (struct rte_ipv4_hdr *)
718                                         ((char *)(eth_hdr + 1) + vlan_offset);
719                         l3hash = ipv4_hash(ipv4_hdr);
720
721                 } else if (rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6) == proto) {
722                         struct rte_ipv6_hdr *ipv6_hdr = (struct rte_ipv6_hdr *)
723                                         ((char *)(eth_hdr + 1) + vlan_offset);
724                         l3hash = ipv6_hash(ipv6_hdr);
725                 }
726
727                 hash = hash ^ l3hash;
728                 hash ^= hash >> 16;
729                 hash ^= hash >> 8;
730
731                 slaves[i] = hash % slave_count;
732         }
733 }
734
735 void
736 burst_xmit_l34_hash(struct rte_mbuf **buf, uint16_t nb_pkts,
737                 uint16_t slave_count, uint16_t *slaves)
738 {
739         struct rte_ether_hdr *eth_hdr;
740         uint16_t proto;
741         size_t vlan_offset;
742         int i;
743
744         struct rte_udp_hdr *udp_hdr;
745         struct rte_tcp_hdr *tcp_hdr;
746         uint32_t hash, l3hash, l4hash;
747
748         for (i = 0; i < nb_pkts; i++) {
749                 eth_hdr = rte_pktmbuf_mtod(buf[i], struct rte_ether_hdr *);
750                 size_t pkt_end = (size_t)eth_hdr + rte_pktmbuf_data_len(buf[i]);
751                 proto = eth_hdr->ether_type;
752                 vlan_offset = get_vlan_offset(eth_hdr, &proto);
753                 l3hash = 0;
754                 l4hash = 0;
755
756                 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4) == proto) {
757                         struct rte_ipv4_hdr *ipv4_hdr = (struct rte_ipv4_hdr *)
758                                         ((char *)(eth_hdr + 1) + vlan_offset);
759                         size_t ip_hdr_offset;
760
761                         l3hash = ipv4_hash(ipv4_hdr);
762
763                         /* there is no L4 header in fragmented packet */
764                         if (likely(rte_ipv4_frag_pkt_is_fragmented(ipv4_hdr)
765                                                                 == 0)) {
766                                 ip_hdr_offset = (ipv4_hdr->version_ihl
767                                         & RTE_IPV4_HDR_IHL_MASK) *
768                                         RTE_IPV4_IHL_MULTIPLIER;
769
770                                 if (ipv4_hdr->next_proto_id == IPPROTO_TCP) {
771                                         tcp_hdr = (struct rte_tcp_hdr *)
772                                                 ((char *)ipv4_hdr +
773                                                         ip_hdr_offset);
774                                         if ((size_t)tcp_hdr + sizeof(*tcp_hdr)
775                                                         < pkt_end)
776                                                 l4hash = HASH_L4_PORTS(tcp_hdr);
777                                 } else if (ipv4_hdr->next_proto_id ==
778                                                                 IPPROTO_UDP) {
779                                         udp_hdr = (struct rte_udp_hdr *)
780                                                 ((char *)ipv4_hdr +
781                                                         ip_hdr_offset);
782                                         if ((size_t)udp_hdr + sizeof(*udp_hdr)
783                                                         < pkt_end)
784                                                 l4hash = HASH_L4_PORTS(udp_hdr);
785                                 }
786                         }
787                 } else if  (rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6) == proto) {
788                         struct rte_ipv6_hdr *ipv6_hdr = (struct rte_ipv6_hdr *)
789                                         ((char *)(eth_hdr + 1) + vlan_offset);
790                         l3hash = ipv6_hash(ipv6_hdr);
791
792                         if (ipv6_hdr->proto == IPPROTO_TCP) {
793                                 tcp_hdr = (struct rte_tcp_hdr *)(ipv6_hdr + 1);
794                                 l4hash = HASH_L4_PORTS(tcp_hdr);
795                         } else if (ipv6_hdr->proto == IPPROTO_UDP) {
796                                 udp_hdr = (struct rte_udp_hdr *)(ipv6_hdr + 1);
797                                 l4hash = HASH_L4_PORTS(udp_hdr);
798                         }
799                 }
800
801                 hash = l3hash ^ l4hash;
802                 hash ^= hash >> 16;
803                 hash ^= hash >> 8;
804
805                 slaves[i] = hash % slave_count;
806         }
807 }
808
809 struct bwg_slave {
810         uint64_t bwg_left_int;
811         uint64_t bwg_left_remainder;
812         uint16_t slave;
813 };
814
815 void
816 bond_tlb_activate_slave(struct bond_dev_private *internals) {
817         int i;
818
819         for (i = 0; i < internals->active_slave_count; i++) {
820                 tlb_last_obytets[internals->active_slaves[i]] = 0;
821         }
822 }
823
824 static int
825 bandwidth_cmp(const void *a, const void *b)
826 {
827         const struct bwg_slave *bwg_a = a;
828         const struct bwg_slave *bwg_b = b;
829         int64_t diff = (int64_t)bwg_b->bwg_left_int - (int64_t)bwg_a->bwg_left_int;
830         int64_t diff2 = (int64_t)bwg_b->bwg_left_remainder -
831                         (int64_t)bwg_a->bwg_left_remainder;
832         if (diff > 0)
833                 return 1;
834         else if (diff < 0)
835                 return -1;
836         else if (diff2 > 0)
837                 return 1;
838         else if (diff2 < 0)
839                 return -1;
840         else
841                 return 0;
842 }
843
844 static void
845 bandwidth_left(uint16_t port_id, uint64_t load, uint8_t update_idx,
846                 struct bwg_slave *bwg_slave)
847 {
848         struct rte_eth_link link_status;
849
850         rte_eth_link_get_nowait(port_id, &link_status);
851         uint64_t link_bwg = link_status.link_speed * 1000000ULL / 8;
852         if (link_bwg == 0)
853                 return;
854         link_bwg = link_bwg * (update_idx+1) * REORDER_PERIOD_MS;
855         bwg_slave->bwg_left_int = (link_bwg - 1000*load) / link_bwg;
856         bwg_slave->bwg_left_remainder = (link_bwg - 1000*load) % link_bwg;
857 }
858
859 static void
860 bond_ethdev_update_tlb_slave_cb(void *arg)
861 {
862         struct bond_dev_private *internals = arg;
863         struct rte_eth_stats slave_stats;
864         struct bwg_slave bwg_array[RTE_MAX_ETHPORTS];
865         uint16_t slave_count;
866         uint64_t tx_bytes;
867
868         uint8_t update_stats = 0;
869         uint16_t slave_id;
870         uint16_t i;
871
872         internals->slave_update_idx++;
873
874
875         if (internals->slave_update_idx >= REORDER_PERIOD_MS)
876                 update_stats = 1;
877
878         for (i = 0; i < internals->active_slave_count; i++) {
879                 slave_id = internals->active_slaves[i];
880                 rte_eth_stats_get(slave_id, &slave_stats);
881                 tx_bytes = slave_stats.obytes - tlb_last_obytets[slave_id];
882                 bandwidth_left(slave_id, tx_bytes,
883                                 internals->slave_update_idx, &bwg_array[i]);
884                 bwg_array[i].slave = slave_id;
885
886                 if (update_stats) {
887                         tlb_last_obytets[slave_id] = slave_stats.obytes;
888                 }
889         }
890
891         if (update_stats == 1)
892                 internals->slave_update_idx = 0;
893
894         slave_count = i;
895         qsort(bwg_array, slave_count, sizeof(bwg_array[0]), bandwidth_cmp);
896         for (i = 0; i < slave_count; i++)
897                 internals->tlb_slaves_order[i] = bwg_array[i].slave;
898
899         rte_eal_alarm_set(REORDER_PERIOD_MS * 1000, bond_ethdev_update_tlb_slave_cb,
900                         (struct bond_dev_private *)internals);
901 }
902
903 static uint16_t
904 bond_ethdev_tx_burst_tlb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
905 {
906         struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
907         struct bond_dev_private *internals = bd_tx_q->dev_private;
908
909         struct rte_eth_dev *primary_port =
910                         &rte_eth_devices[internals->primary_port];
911         uint16_t num_tx_total = 0;
912         uint16_t i, j;
913
914         uint16_t num_of_slaves = internals->active_slave_count;
915         uint16_t slaves[RTE_MAX_ETHPORTS];
916
917         struct rte_ether_hdr *ether_hdr;
918         struct rte_ether_addr primary_slave_addr;
919         struct rte_ether_addr active_slave_addr;
920
921         if (num_of_slaves < 1)
922                 return num_tx_total;
923
924         memcpy(slaves, internals->tlb_slaves_order,
925                                 sizeof(internals->tlb_slaves_order[0]) * num_of_slaves);
926
927
928         rte_ether_addr_copy(primary_port->data->mac_addrs, &primary_slave_addr);
929
930         if (nb_pkts > 3) {
931                 for (i = 0; i < 3; i++)
932                         rte_prefetch0(rte_pktmbuf_mtod(bufs[i], void*));
933         }
934
935         for (i = 0; i < num_of_slaves; i++) {
936                 rte_eth_macaddr_get(slaves[i], &active_slave_addr);
937                 for (j = num_tx_total; j < nb_pkts; j++) {
938                         if (j + 3 < nb_pkts)
939                                 rte_prefetch0(rte_pktmbuf_mtod(bufs[j+3], void*));
940
941                         ether_hdr = rte_pktmbuf_mtod(bufs[j],
942                                                 struct rte_ether_hdr *);
943                         if (rte_is_same_ether_addr(&ether_hdr->s_addr,
944                                                         &primary_slave_addr))
945                                 rte_ether_addr_copy(&active_slave_addr,
946                                                 &ether_hdr->s_addr);
947 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
948                                         mode6_debug("TX IPv4:", ether_hdr, slaves[i], &burstnumberTX);
949 #endif
950                 }
951
952                 num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
953                                 bufs + num_tx_total, nb_pkts - num_tx_total);
954
955                 if (num_tx_total == nb_pkts)
956                         break;
957         }
958
959         return num_tx_total;
960 }
961
962 void
963 bond_tlb_disable(struct bond_dev_private *internals)
964 {
965         rte_eal_alarm_cancel(bond_ethdev_update_tlb_slave_cb, internals);
966 }
967
968 void
969 bond_tlb_enable(struct bond_dev_private *internals)
970 {
971         bond_ethdev_update_tlb_slave_cb(internals);
972 }
973
974 static uint16_t
975 bond_ethdev_tx_burst_alb(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
976 {
977         struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
978         struct bond_dev_private *internals = bd_tx_q->dev_private;
979
980         struct rte_ether_hdr *eth_h;
981         uint16_t ether_type, offset;
982
983         struct client_data *client_info;
984
985         /*
986          * We create transmit buffers for every slave and one additional to send
987          * through tlb. In worst case every packet will be send on one port.
988          */
989         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS + 1][nb_pkts];
990         uint16_t slave_bufs_pkts[RTE_MAX_ETHPORTS + 1] = { 0 };
991
992         /*
993          * We create separate transmit buffers for update packets as they won't
994          * be counted in num_tx_total.
995          */
996         struct rte_mbuf *update_bufs[RTE_MAX_ETHPORTS][ALB_HASH_TABLE_SIZE];
997         uint16_t update_bufs_pkts[RTE_MAX_ETHPORTS] = { 0 };
998
999         struct rte_mbuf *upd_pkt;
1000         size_t pkt_size;
1001
1002         uint16_t num_send, num_not_send = 0;
1003         uint16_t num_tx_total = 0;
1004         uint16_t slave_idx;
1005
1006         int i, j;
1007
1008         /* Search tx buffer for ARP packets and forward them to alb */
1009         for (i = 0; i < nb_pkts; i++) {
1010                 eth_h = rte_pktmbuf_mtod(bufs[i], struct rte_ether_hdr *);
1011                 ether_type = eth_h->ether_type;
1012                 offset = get_vlan_offset(eth_h, &ether_type);
1013
1014                 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
1015                         slave_idx = bond_mode_alb_arp_xmit(eth_h, offset, internals);
1016
1017                         /* Change src mac in eth header */
1018                         rte_eth_macaddr_get(slave_idx, &eth_h->s_addr);
1019
1020                         /* Add packet to slave tx buffer */
1021                         slave_bufs[slave_idx][slave_bufs_pkts[slave_idx]] = bufs[i];
1022                         slave_bufs_pkts[slave_idx]++;
1023                 } else {
1024                         /* If packet is not ARP, send it with TLB policy */
1025                         slave_bufs[RTE_MAX_ETHPORTS][slave_bufs_pkts[RTE_MAX_ETHPORTS]] =
1026                                         bufs[i];
1027                         slave_bufs_pkts[RTE_MAX_ETHPORTS]++;
1028                 }
1029         }
1030
1031         /* Update connected client ARP tables */
1032         if (internals->mode6.ntt) {
1033                 for (i = 0; i < ALB_HASH_TABLE_SIZE; i++) {
1034                         client_info = &internals->mode6.client_table[i];
1035
1036                         if (client_info->in_use) {
1037                                 /* Allocate new packet to send ARP update on current slave */
1038                                 upd_pkt = rte_pktmbuf_alloc(internals->mode6.mempool);
1039                                 if (upd_pkt == NULL) {
1040                                         RTE_BOND_LOG(ERR,
1041                                                      "Failed to allocate ARP packet from pool");
1042                                         continue;
1043                                 }
1044                                 pkt_size = sizeof(struct rte_ether_hdr) +
1045                                         sizeof(struct rte_arp_hdr) +
1046                                         client_info->vlan_count *
1047                                         sizeof(struct rte_vlan_hdr);
1048                                 upd_pkt->data_len = pkt_size;
1049                                 upd_pkt->pkt_len = pkt_size;
1050
1051                                 slave_idx = bond_mode_alb_arp_upd(client_info, upd_pkt,
1052                                                 internals);
1053
1054                                 /* Add packet to update tx buffer */
1055                                 update_bufs[slave_idx][update_bufs_pkts[slave_idx]] = upd_pkt;
1056                                 update_bufs_pkts[slave_idx]++;
1057                         }
1058                 }
1059                 internals->mode6.ntt = 0;
1060         }
1061
1062         /* Send ARP packets on proper slaves */
1063         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
1064                 if (slave_bufs_pkts[i] > 0) {
1065                         num_send = rte_eth_tx_burst(i, bd_tx_q->queue_id,
1066                                         slave_bufs[i], slave_bufs_pkts[i]);
1067                         for (j = 0; j < slave_bufs_pkts[i] - num_send; j++) {
1068                                 bufs[nb_pkts - 1 - num_not_send - j] =
1069                                                 slave_bufs[i][nb_pkts - 1 - j];
1070                         }
1071
1072                         num_tx_total += num_send;
1073                         num_not_send += slave_bufs_pkts[i] - num_send;
1074
1075 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
1076         /* Print TX stats including update packets */
1077                         for (j = 0; j < slave_bufs_pkts[i]; j++) {
1078                                 eth_h = rte_pktmbuf_mtod(slave_bufs[i][j],
1079                                                         struct rte_ether_hdr *);
1080                                 mode6_debug("TX ARP:", eth_h, i, &burstnumberTX);
1081                         }
1082 #endif
1083                 }
1084         }
1085
1086         /* Send update packets on proper slaves */
1087         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
1088                 if (update_bufs_pkts[i] > 0) {
1089                         num_send = rte_eth_tx_burst(i, bd_tx_q->queue_id, update_bufs[i],
1090                                         update_bufs_pkts[i]);
1091                         for (j = num_send; j < update_bufs_pkts[i]; j++) {
1092                                 rte_pktmbuf_free(update_bufs[i][j]);
1093                         }
1094 #if defined(RTE_LIBRTE_BOND_DEBUG_ALB) || defined(RTE_LIBRTE_BOND_DEBUG_ALB_L1)
1095                         for (j = 0; j < update_bufs_pkts[i]; j++) {
1096                                 eth_h = rte_pktmbuf_mtod(update_bufs[i][j],
1097                                                         struct rte_ether_hdr *);
1098                                 mode6_debug("TX ARPupd:", eth_h, i, &burstnumberTX);
1099                         }
1100 #endif
1101                 }
1102         }
1103
1104         /* Send non-ARP packets using tlb policy */
1105         if (slave_bufs_pkts[RTE_MAX_ETHPORTS] > 0) {
1106                 num_send = bond_ethdev_tx_burst_tlb(queue,
1107                                 slave_bufs[RTE_MAX_ETHPORTS],
1108                                 slave_bufs_pkts[RTE_MAX_ETHPORTS]);
1109
1110                 for (j = 0; j < slave_bufs_pkts[RTE_MAX_ETHPORTS]; j++) {
1111                         bufs[nb_pkts - 1 - num_not_send - j] =
1112                                         slave_bufs[RTE_MAX_ETHPORTS][nb_pkts - 1 - j];
1113                 }
1114
1115                 num_tx_total += num_send;
1116         }
1117
1118         return num_tx_total;
1119 }
1120
1121 static inline uint16_t
1122 tx_burst_balance(void *queue, struct rte_mbuf **bufs, uint16_t nb_bufs,
1123                  uint16_t *slave_port_ids, uint16_t slave_count)
1124 {
1125         struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
1126         struct bond_dev_private *internals = bd_tx_q->dev_private;
1127
1128         /* Array to sort mbufs for transmission on each slave into */
1129         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_bufs];
1130         /* Number of mbufs for transmission on each slave */
1131         uint16_t slave_nb_bufs[RTE_MAX_ETHPORTS] = { 0 };
1132         /* Mapping array generated by hash function to map mbufs to slaves */
1133         uint16_t bufs_slave_port_idxs[nb_bufs];
1134
1135         uint16_t slave_tx_count;
1136         uint16_t total_tx_count = 0, total_tx_fail_count = 0;
1137
1138         uint16_t i;
1139
1140         /*
1141          * Populate slaves mbuf with the packets which are to be sent on it
1142          * selecting output slave using hash based on xmit policy
1143          */
1144         internals->burst_xmit_hash(bufs, nb_bufs, slave_count,
1145                         bufs_slave_port_idxs);
1146
1147         for (i = 0; i < nb_bufs; i++) {
1148                 /* Populate slave mbuf arrays with mbufs for that slave. */
1149                 uint16_t slave_idx = bufs_slave_port_idxs[i];
1150
1151                 slave_bufs[slave_idx][slave_nb_bufs[slave_idx]++] = bufs[i];
1152         }
1153
1154         /* Send packet burst on each slave device */
1155         for (i = 0; i < slave_count; i++) {
1156                 if (slave_nb_bufs[i] == 0)
1157                         continue;
1158
1159                 slave_tx_count = rte_eth_tx_burst(slave_port_ids[i],
1160                                 bd_tx_q->queue_id, slave_bufs[i],
1161                                 slave_nb_bufs[i]);
1162
1163                 total_tx_count += slave_tx_count;
1164
1165                 /* If tx burst fails move packets to end of bufs */
1166                 if (unlikely(slave_tx_count < slave_nb_bufs[i])) {
1167                         int slave_tx_fail_count = slave_nb_bufs[i] -
1168                                         slave_tx_count;
1169                         total_tx_fail_count += slave_tx_fail_count;
1170                         memcpy(&bufs[nb_bufs - total_tx_fail_count],
1171                                &slave_bufs[i][slave_tx_count],
1172                                slave_tx_fail_count * sizeof(bufs[0]));
1173                 }
1174         }
1175
1176         return total_tx_count;
1177 }
1178
1179 static uint16_t
1180 bond_ethdev_tx_burst_balance(void *queue, struct rte_mbuf **bufs,
1181                 uint16_t nb_bufs)
1182 {
1183         struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
1184         struct bond_dev_private *internals = bd_tx_q->dev_private;
1185
1186         uint16_t slave_port_ids[RTE_MAX_ETHPORTS];
1187         uint16_t slave_count;
1188
1189         if (unlikely(nb_bufs == 0))
1190                 return 0;
1191
1192         /* Copy slave list to protect against slave up/down changes during tx
1193          * bursting
1194          */
1195         slave_count = internals->active_slave_count;
1196         if (unlikely(slave_count < 1))
1197                 return 0;
1198
1199         memcpy(slave_port_ids, internals->active_slaves,
1200                         sizeof(slave_port_ids[0]) * slave_count);
1201         return tx_burst_balance(queue, bufs, nb_bufs, slave_port_ids,
1202                                 slave_count);
1203 }
1204
1205 static inline uint16_t
1206 tx_burst_8023ad(void *queue, struct rte_mbuf **bufs, uint16_t nb_bufs,
1207                 bool dedicated_txq)
1208 {
1209         struct bond_tx_queue *bd_tx_q = (struct bond_tx_queue *)queue;
1210         struct bond_dev_private *internals = bd_tx_q->dev_private;
1211
1212         uint16_t slave_port_ids[RTE_MAX_ETHPORTS];
1213         uint16_t slave_count;
1214
1215         uint16_t dist_slave_port_ids[RTE_MAX_ETHPORTS];
1216         uint16_t dist_slave_count;
1217
1218         uint16_t slave_tx_count;
1219
1220         uint16_t i;
1221
1222         /* Copy slave list to protect against slave up/down changes during tx
1223          * bursting */
1224         slave_count = internals->active_slave_count;
1225         if (unlikely(slave_count < 1))
1226                 return 0;
1227
1228         memcpy(slave_port_ids, internals->active_slaves,
1229                         sizeof(slave_port_ids[0]) * slave_count);
1230
1231         if (dedicated_txq)
1232                 goto skip_tx_ring;
1233
1234         /* Check for LACP control packets and send if available */
1235         for (i = 0; i < slave_count; i++) {
1236                 struct port *port = &bond_mode_8023ad_ports[slave_port_ids[i]];
1237                 struct rte_mbuf *ctrl_pkt = NULL;
1238
1239                 if (likely(rte_ring_empty(port->tx_ring)))
1240                         continue;
1241
1242                 if (rte_ring_dequeue(port->tx_ring,
1243                                      (void **)&ctrl_pkt) != -ENOENT) {
1244                         slave_tx_count = rte_eth_tx_burst(slave_port_ids[i],
1245                                         bd_tx_q->queue_id, &ctrl_pkt, 1);
1246                         /*
1247                          * re-enqueue LAG control plane packets to buffering
1248                          * ring if transmission fails so the packet isn't lost.
1249                          */
1250                         if (slave_tx_count != 1)
1251                                 rte_ring_enqueue(port->tx_ring, ctrl_pkt);
1252                 }
1253         }
1254
1255 skip_tx_ring:
1256         if (unlikely(nb_bufs == 0))
1257                 return 0;
1258
1259         dist_slave_count = 0;
1260         for (i = 0; i < slave_count; i++) {
1261                 struct port *port = &bond_mode_8023ad_ports[slave_port_ids[i]];
1262
1263                 if (ACTOR_STATE(port, DISTRIBUTING))
1264                         dist_slave_port_ids[dist_slave_count++] =
1265                                         slave_port_ids[i];
1266         }
1267
1268         if (unlikely(dist_slave_count < 1))
1269                 return 0;
1270
1271         return tx_burst_balance(queue, bufs, nb_bufs, dist_slave_port_ids,
1272                                 dist_slave_count);
1273 }
1274
1275 static uint16_t
1276 bond_ethdev_tx_burst_8023ad(void *queue, struct rte_mbuf **bufs,
1277                 uint16_t nb_bufs)
1278 {
1279         return tx_burst_8023ad(queue, bufs, nb_bufs, false);
1280 }
1281
1282 static uint16_t
1283 bond_ethdev_tx_burst_8023ad_fast_queue(void *queue, struct rte_mbuf **bufs,
1284                 uint16_t nb_bufs)
1285 {
1286         return tx_burst_8023ad(queue, bufs, nb_bufs, true);
1287 }
1288
1289 static uint16_t
1290 bond_ethdev_tx_burst_broadcast(void *queue, struct rte_mbuf **bufs,
1291                 uint16_t nb_pkts)
1292 {
1293         struct bond_dev_private *internals;
1294         struct bond_tx_queue *bd_tx_q;
1295
1296         uint16_t slaves[RTE_MAX_ETHPORTS];
1297         uint8_t tx_failed_flag = 0;
1298         uint16_t num_of_slaves;
1299
1300         uint16_t max_nb_of_tx_pkts = 0;
1301
1302         int slave_tx_total[RTE_MAX_ETHPORTS];
1303         int i, most_successful_tx_slave = -1;
1304
1305         bd_tx_q = (struct bond_tx_queue *)queue;
1306         internals = bd_tx_q->dev_private;
1307
1308         /* Copy slave list to protect against slave up/down changes during tx
1309          * bursting */
1310         num_of_slaves = internals->active_slave_count;
1311         memcpy(slaves, internals->active_slaves,
1312                         sizeof(internals->active_slaves[0]) * num_of_slaves);
1313
1314         if (num_of_slaves < 1)
1315                 return 0;
1316
1317         /* Increment reference count on mbufs */
1318         for (i = 0; i < nb_pkts; i++)
1319                 rte_mbuf_refcnt_update(bufs[i], num_of_slaves - 1);
1320
1321         /* Transmit burst on each active slave */
1322         for (i = 0; i < num_of_slaves; i++) {
1323                 slave_tx_total[i] = rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
1324                                         bufs, nb_pkts);
1325
1326                 if (unlikely(slave_tx_total[i] < nb_pkts))
1327                         tx_failed_flag = 1;
1328
1329                 /* record the value and slave index for the slave which transmits the
1330                  * maximum number of packets */
1331                 if (slave_tx_total[i] > max_nb_of_tx_pkts) {
1332                         max_nb_of_tx_pkts = slave_tx_total[i];
1333                         most_successful_tx_slave = i;
1334                 }
1335         }
1336
1337         /* if slaves fail to transmit packets from burst, the calling application
1338          * is not expected to know about multiple references to packets so we must
1339          * handle failures of all packets except those of the most successful slave
1340          */
1341         if (unlikely(tx_failed_flag))
1342                 for (i = 0; i < num_of_slaves; i++)
1343                         if (i != most_successful_tx_slave)
1344                                 while (slave_tx_total[i] < nb_pkts)
1345                                         rte_pktmbuf_free(bufs[slave_tx_total[i]++]);
1346
1347         return max_nb_of_tx_pkts;
1348 }
1349
1350 static void
1351 link_properties_set(struct rte_eth_dev *ethdev, struct rte_eth_link *slave_link)
1352 {
1353         struct bond_dev_private *bond_ctx = ethdev->data->dev_private;
1354
1355         if (bond_ctx->mode == BONDING_MODE_8023AD) {
1356                 /**
1357                  * If in mode 4 then save the link properties of the first
1358                  * slave, all subsequent slaves must match these properties
1359                  */
1360                 struct rte_eth_link *bond_link = &bond_ctx->mode4.slave_link;
1361
1362                 bond_link->link_autoneg = slave_link->link_autoneg;
1363                 bond_link->link_duplex = slave_link->link_duplex;
1364                 bond_link->link_speed = slave_link->link_speed;
1365         } else {
1366                 /**
1367                  * In any other mode the link properties are set to default
1368                  * values of AUTONEG/DUPLEX
1369                  */
1370                 ethdev->data->dev_link.link_autoneg = ETH_LINK_AUTONEG;
1371                 ethdev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
1372         }
1373 }
1374
1375 static int
1376 link_properties_valid(struct rte_eth_dev *ethdev,
1377                 struct rte_eth_link *slave_link)
1378 {
1379         struct bond_dev_private *bond_ctx = ethdev->data->dev_private;
1380
1381         if (bond_ctx->mode == BONDING_MODE_8023AD) {
1382                 struct rte_eth_link *bond_link = &bond_ctx->mode4.slave_link;
1383
1384                 if (bond_link->link_duplex != slave_link->link_duplex ||
1385                         bond_link->link_autoneg != slave_link->link_autoneg ||
1386                         bond_link->link_speed != slave_link->link_speed)
1387                         return -1;
1388         }
1389
1390         return 0;
1391 }
1392
1393 int
1394 mac_address_get(struct rte_eth_dev *eth_dev,
1395                 struct rte_ether_addr *dst_mac_addr)
1396 {
1397         struct rte_ether_addr *mac_addr;
1398
1399         if (eth_dev == NULL) {
1400                 RTE_BOND_LOG(ERR, "NULL pointer eth_dev specified");
1401                 return -1;
1402         }
1403
1404         if (dst_mac_addr == NULL) {
1405                 RTE_BOND_LOG(ERR, "NULL pointer MAC specified");
1406                 return -1;
1407         }
1408
1409         mac_addr = eth_dev->data->mac_addrs;
1410
1411         rte_ether_addr_copy(mac_addr, dst_mac_addr);
1412         return 0;
1413 }
1414
1415 int
1416 mac_address_set(struct rte_eth_dev *eth_dev,
1417                 struct rte_ether_addr *new_mac_addr)
1418 {
1419         struct rte_ether_addr *mac_addr;
1420
1421         if (eth_dev == NULL) {
1422                 RTE_BOND_LOG(ERR, "NULL pointer eth_dev specified");
1423                 return -1;
1424         }
1425
1426         if (new_mac_addr == NULL) {
1427                 RTE_BOND_LOG(ERR, "NULL pointer MAC specified");
1428                 return -1;
1429         }
1430
1431         mac_addr = eth_dev->data->mac_addrs;
1432
1433         /* If new MAC is different to current MAC then update */
1434         if (memcmp(mac_addr, new_mac_addr, sizeof(*mac_addr)) != 0)
1435                 memcpy(mac_addr, new_mac_addr, sizeof(*mac_addr));
1436
1437         return 0;
1438 }
1439
1440 static const struct rte_ether_addr null_mac_addr;
1441
1442 /*
1443  * Add additional MAC addresses to the slave
1444  */
1445 int
1446 slave_add_mac_addresses(struct rte_eth_dev *bonded_eth_dev,
1447                 uint16_t slave_port_id)
1448 {
1449         int i, ret;
1450         struct rte_ether_addr *mac_addr;
1451
1452         for (i = 1; i < BOND_MAX_MAC_ADDRS; i++) {
1453                 mac_addr = &bonded_eth_dev->data->mac_addrs[i];
1454                 if (rte_is_same_ether_addr(mac_addr, &null_mac_addr))
1455                         break;
1456
1457                 ret = rte_eth_dev_mac_addr_add(slave_port_id, mac_addr, 0);
1458                 if (ret < 0) {
1459                         /* rollback */
1460                         for (i--; i > 0; i--)
1461                                 rte_eth_dev_mac_addr_remove(slave_port_id,
1462                                         &bonded_eth_dev->data->mac_addrs[i]);
1463                         return ret;
1464                 }
1465         }
1466
1467         return 0;
1468 }
1469
1470 /*
1471  * Remove additional MAC addresses from the slave
1472  */
1473 int
1474 slave_remove_mac_addresses(struct rte_eth_dev *bonded_eth_dev,
1475                 uint16_t slave_port_id)
1476 {
1477         int i, rc, ret;
1478         struct rte_ether_addr *mac_addr;
1479
1480         rc = 0;
1481         for (i = 1; i < BOND_MAX_MAC_ADDRS; i++) {
1482                 mac_addr = &bonded_eth_dev->data->mac_addrs[i];
1483                 if (rte_is_same_ether_addr(mac_addr, &null_mac_addr))
1484                         break;
1485
1486                 ret = rte_eth_dev_mac_addr_remove(slave_port_id, mac_addr);
1487                 /* save only the first error */
1488                 if (ret < 0 && rc == 0)
1489                         rc = ret;
1490         }
1491
1492         return rc;
1493 }
1494
1495 int
1496 mac_address_slaves_update(struct rte_eth_dev *bonded_eth_dev)
1497 {
1498         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
1499         int i;
1500
1501         /* Update slave devices MAC addresses */
1502         if (internals->slave_count < 1)
1503                 return -1;
1504
1505         switch (internals->mode) {
1506         case BONDING_MODE_ROUND_ROBIN:
1507         case BONDING_MODE_BALANCE:
1508         case BONDING_MODE_BROADCAST:
1509                 for (i = 0; i < internals->slave_count; i++) {
1510                         if (rte_eth_dev_default_mac_addr_set(
1511                                         internals->slaves[i].port_id,
1512                                         bonded_eth_dev->data->mac_addrs)) {
1513                                 RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
1514                                                 internals->slaves[i].port_id);
1515                                 return -1;
1516                         }
1517                 }
1518                 break;
1519         case BONDING_MODE_8023AD:
1520                 bond_mode_8023ad_mac_address_update(bonded_eth_dev);
1521                 break;
1522         case BONDING_MODE_ACTIVE_BACKUP:
1523         case BONDING_MODE_TLB:
1524         case BONDING_MODE_ALB:
1525         default:
1526                 for (i = 0; i < internals->slave_count; i++) {
1527                         if (internals->slaves[i].port_id ==
1528                                         internals->current_primary_port) {
1529                                 if (rte_eth_dev_default_mac_addr_set(
1530                                                 internals->primary_port,
1531                                                 bonded_eth_dev->data->mac_addrs)) {
1532                                         RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
1533                                                         internals->current_primary_port);
1534                                         return -1;
1535                                 }
1536                         } else {
1537                                 if (rte_eth_dev_default_mac_addr_set(
1538                                                 internals->slaves[i].port_id,
1539                                                 &internals->slaves[i].persisted_mac_addr)) {
1540                                         RTE_BOND_LOG(ERR, "Failed to update port Id %d MAC address",
1541                                                         internals->slaves[i].port_id);
1542                                         return -1;
1543                                 }
1544                         }
1545                 }
1546         }
1547
1548         return 0;
1549 }
1550
1551 int
1552 bond_ethdev_mode_set(struct rte_eth_dev *eth_dev, int mode)
1553 {
1554         struct bond_dev_private *internals;
1555
1556         internals = eth_dev->data->dev_private;
1557
1558         switch (mode) {
1559         case BONDING_MODE_ROUND_ROBIN:
1560                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_round_robin;
1561                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
1562                 break;
1563         case BONDING_MODE_ACTIVE_BACKUP:
1564                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_active_backup;
1565                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_active_backup;
1566                 break;
1567         case BONDING_MODE_BALANCE:
1568                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_balance;
1569                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
1570                 break;
1571         case BONDING_MODE_BROADCAST:
1572                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_broadcast;
1573                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
1574                 break;
1575         case BONDING_MODE_8023AD:
1576                 if (bond_mode_8023ad_enable(eth_dev) != 0)
1577                         return -1;
1578
1579                 if (internals->mode4.dedicated_queues.enabled == 0) {
1580                         eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_8023ad;
1581                         eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_8023ad;
1582                         RTE_BOND_LOG(WARNING,
1583                                 "Using mode 4, it is necessary to do TX burst "
1584                                 "and RX burst at least every 100ms.");
1585                 } else {
1586                         /* Use flow director's optimization */
1587                         eth_dev->rx_pkt_burst =
1588                                         bond_ethdev_rx_burst_8023ad_fast_queue;
1589                         eth_dev->tx_pkt_burst =
1590                                         bond_ethdev_tx_burst_8023ad_fast_queue;
1591                 }
1592                 break;
1593         case BONDING_MODE_TLB:
1594                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_tlb;
1595                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_active_backup;
1596                 break;
1597         case BONDING_MODE_ALB:
1598                 if (bond_mode_alb_enable(eth_dev) != 0)
1599                         return -1;
1600
1601                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_alb;
1602                 eth_dev->rx_pkt_burst = bond_ethdev_rx_burst_alb;
1603                 break;
1604         default:
1605                 return -1;
1606         }
1607
1608         internals->mode = mode;
1609
1610         return 0;
1611 }
1612
1613
1614 static int
1615 slave_configure_slow_queue(struct rte_eth_dev *bonded_eth_dev,
1616                 struct rte_eth_dev *slave_eth_dev)
1617 {
1618         int errval = 0;
1619         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
1620         struct port *port = &bond_mode_8023ad_ports[slave_eth_dev->data->port_id];
1621
1622         if (port->slow_pool == NULL) {
1623                 char mem_name[256];
1624                 int slave_id = slave_eth_dev->data->port_id;
1625
1626                 snprintf(mem_name, RTE_DIM(mem_name), "slave_port%u_slow_pool",
1627                                 slave_id);
1628                 port->slow_pool = rte_pktmbuf_pool_create(mem_name, 8191,
1629                         250, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
1630                         slave_eth_dev->data->numa_node);
1631
1632                 /* Any memory allocation failure in initialization is critical because
1633                  * resources can't be free, so reinitialization is impossible. */
1634                 if (port->slow_pool == NULL) {
1635                         rte_panic("Slave %u: Failed to create memory pool '%s': %s\n",
1636                                 slave_id, mem_name, rte_strerror(rte_errno));
1637                 }
1638         }
1639
1640         if (internals->mode4.dedicated_queues.enabled == 1) {
1641                 /* Configure slow Rx queue */
1642
1643                 errval = rte_eth_rx_queue_setup(slave_eth_dev->data->port_id,
1644                                 internals->mode4.dedicated_queues.rx_qid, 128,
1645                                 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
1646                                 NULL, port->slow_pool);
1647                 if (errval != 0) {
1648                         RTE_BOND_LOG(ERR,
1649                                         "rte_eth_rx_queue_setup: port=%d queue_id %d, err (%d)",
1650                                         slave_eth_dev->data->port_id,
1651                                         internals->mode4.dedicated_queues.rx_qid,
1652                                         errval);
1653                         return errval;
1654                 }
1655
1656                 errval = rte_eth_tx_queue_setup(slave_eth_dev->data->port_id,
1657                                 internals->mode4.dedicated_queues.tx_qid, 512,
1658                                 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
1659                                 NULL);
1660                 if (errval != 0) {
1661                         RTE_BOND_LOG(ERR,
1662                                 "rte_eth_tx_queue_setup: port=%d queue_id %d, err (%d)",
1663                                 slave_eth_dev->data->port_id,
1664                                 internals->mode4.dedicated_queues.tx_qid,
1665                                 errval);
1666                         return errval;
1667                 }
1668         }
1669         return 0;
1670 }
1671
1672 int
1673 slave_configure(struct rte_eth_dev *bonded_eth_dev,
1674                 struct rte_eth_dev *slave_eth_dev)
1675 {
1676         struct bond_rx_queue *bd_rx_q;
1677         struct bond_tx_queue *bd_tx_q;
1678         uint16_t nb_rx_queues;
1679         uint16_t nb_tx_queues;
1680
1681         int errval;
1682         uint16_t q_id;
1683         struct rte_flow_error flow_error;
1684
1685         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
1686
1687         /* Stop slave */
1688         rte_eth_dev_stop(slave_eth_dev->data->port_id);
1689
1690         /* Enable interrupts on slave device if supported */
1691         if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1692                 slave_eth_dev->data->dev_conf.intr_conf.lsc = 1;
1693
1694         /* If RSS is enabled for bonding, try to enable it for slaves  */
1695         if (bonded_eth_dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
1696                 if (internals->rss_key_len != 0) {
1697                         slave_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len =
1698                                         internals->rss_key_len;
1699                         slave_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key =
1700                                         internals->rss_key;
1701                 } else {
1702                         slave_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
1703                 }
1704
1705                 slave_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf =
1706                                 bonded_eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
1707                 slave_eth_dev->data->dev_conf.rxmode.mq_mode =
1708                                 bonded_eth_dev->data->dev_conf.rxmode.mq_mode;
1709         }
1710
1711         if (bonded_eth_dev->data->dev_conf.rxmode.offloads &
1712                         DEV_RX_OFFLOAD_VLAN_FILTER)
1713                 slave_eth_dev->data->dev_conf.rxmode.offloads |=
1714                                 DEV_RX_OFFLOAD_VLAN_FILTER;
1715         else
1716                 slave_eth_dev->data->dev_conf.rxmode.offloads &=
1717                                 ~DEV_RX_OFFLOAD_VLAN_FILTER;
1718
1719         nb_rx_queues = bonded_eth_dev->data->nb_rx_queues;
1720         nb_tx_queues = bonded_eth_dev->data->nb_tx_queues;
1721
1722         if (internals->mode == BONDING_MODE_8023AD) {
1723                 if (internals->mode4.dedicated_queues.enabled == 1) {
1724                         nb_rx_queues++;
1725                         nb_tx_queues++;
1726                 }
1727         }
1728
1729         errval = rte_eth_dev_set_mtu(slave_eth_dev->data->port_id,
1730                                      bonded_eth_dev->data->mtu);
1731         if (errval != 0 && errval != -ENOTSUP) {
1732                 RTE_BOND_LOG(ERR, "rte_eth_dev_set_mtu: port %u, err (%d)",
1733                                 slave_eth_dev->data->port_id, errval);
1734                 return errval;
1735         }
1736
1737         /* Configure device */
1738         errval = rte_eth_dev_configure(slave_eth_dev->data->port_id,
1739                         nb_rx_queues, nb_tx_queues,
1740                         &(slave_eth_dev->data->dev_conf));
1741         if (errval != 0) {
1742                 RTE_BOND_LOG(ERR, "Cannot configure slave device: port %u, err (%d)",
1743                                 slave_eth_dev->data->port_id, errval);
1744                 return errval;
1745         }
1746
1747         /* Setup Rx Queues */
1748         for (q_id = 0; q_id < bonded_eth_dev->data->nb_rx_queues; q_id++) {
1749                 bd_rx_q = (struct bond_rx_queue *)bonded_eth_dev->data->rx_queues[q_id];
1750
1751                 errval = rte_eth_rx_queue_setup(slave_eth_dev->data->port_id, q_id,
1752                                 bd_rx_q->nb_rx_desc,
1753                                 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
1754                                 &(bd_rx_q->rx_conf), bd_rx_q->mb_pool);
1755                 if (errval != 0) {
1756                         RTE_BOND_LOG(ERR,
1757                                         "rte_eth_rx_queue_setup: port=%d queue_id %d, err (%d)",
1758                                         slave_eth_dev->data->port_id, q_id, errval);
1759                         return errval;
1760                 }
1761         }
1762
1763         /* Setup Tx Queues */
1764         for (q_id = 0; q_id < bonded_eth_dev->data->nb_tx_queues; q_id++) {
1765                 bd_tx_q = (struct bond_tx_queue *)bonded_eth_dev->data->tx_queues[q_id];
1766
1767                 errval = rte_eth_tx_queue_setup(slave_eth_dev->data->port_id, q_id,
1768                                 bd_tx_q->nb_tx_desc,
1769                                 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
1770                                 &bd_tx_q->tx_conf);
1771                 if (errval != 0) {
1772                         RTE_BOND_LOG(ERR,
1773                                 "rte_eth_tx_queue_setup: port=%d queue_id %d, err (%d)",
1774                                 slave_eth_dev->data->port_id, q_id, errval);
1775                         return errval;
1776                 }
1777         }
1778
1779         if (internals->mode == BONDING_MODE_8023AD &&
1780                         internals->mode4.dedicated_queues.enabled == 1) {
1781                 if (slave_configure_slow_queue(bonded_eth_dev, slave_eth_dev)
1782                                 != 0)
1783                         return errval;
1784
1785                 if (bond_ethdev_8023ad_flow_verify(bonded_eth_dev,
1786                                 slave_eth_dev->data->port_id) != 0) {
1787                         RTE_BOND_LOG(ERR,
1788                                 "rte_eth_tx_queue_setup: port=%d queue_id %d, err (%d)",
1789                                 slave_eth_dev->data->port_id, q_id, errval);
1790                         return -1;
1791                 }
1792
1793                 if (internals->mode4.dedicated_queues.flow[slave_eth_dev->data->port_id] != NULL)
1794                         rte_flow_destroy(slave_eth_dev->data->port_id,
1795                                         internals->mode4.dedicated_queues.flow[slave_eth_dev->data->port_id],
1796                                         &flow_error);
1797
1798                 bond_ethdev_8023ad_flow_set(bonded_eth_dev,
1799                                 slave_eth_dev->data->port_id);
1800         }
1801
1802         /* Start device */
1803         errval = rte_eth_dev_start(slave_eth_dev->data->port_id);
1804         if (errval != 0) {
1805                 RTE_BOND_LOG(ERR, "rte_eth_dev_start: port=%u, err (%d)",
1806                                 slave_eth_dev->data->port_id, errval);
1807                 return -1;
1808         }
1809
1810         /* If RSS is enabled for bonding, synchronize RETA */
1811         if (bonded_eth_dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS) {
1812                 int i;
1813                 struct bond_dev_private *internals;
1814
1815                 internals = bonded_eth_dev->data->dev_private;
1816
1817                 for (i = 0; i < internals->slave_count; i++) {
1818                         if (internals->slaves[i].port_id == slave_eth_dev->data->port_id) {
1819                                 errval = rte_eth_dev_rss_reta_update(
1820                                                 slave_eth_dev->data->port_id,
1821                                                 &internals->reta_conf[0],
1822                                                 internals->slaves[i].reta_size);
1823                                 if (errval != 0) {
1824                                         RTE_BOND_LOG(WARNING,
1825                                                      "rte_eth_dev_rss_reta_update on slave port %d fails (err %d)."
1826                                                      " RSS Configuration for bonding may be inconsistent.",
1827                                                      slave_eth_dev->data->port_id, errval);
1828                                 }
1829                                 break;
1830                         }
1831                 }
1832         }
1833
1834         /* If lsc interrupt is set, check initial slave's link status */
1835         if (slave_eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
1836                 slave_eth_dev->dev_ops->link_update(slave_eth_dev, 0);
1837                 bond_ethdev_lsc_event_callback(slave_eth_dev->data->port_id,
1838                         RTE_ETH_EVENT_INTR_LSC, &bonded_eth_dev->data->port_id,
1839                         NULL);
1840         }
1841
1842         return 0;
1843 }
1844
1845 void
1846 slave_remove(struct bond_dev_private *internals,
1847                 struct rte_eth_dev *slave_eth_dev)
1848 {
1849         uint16_t i;
1850
1851         for (i = 0; i < internals->slave_count; i++)
1852                 if (internals->slaves[i].port_id ==
1853                                 slave_eth_dev->data->port_id)
1854                         break;
1855
1856         if (i < (internals->slave_count - 1)) {
1857                 struct rte_flow *flow;
1858
1859                 memmove(&internals->slaves[i], &internals->slaves[i + 1],
1860                                 sizeof(internals->slaves[0]) *
1861                                 (internals->slave_count - i - 1));
1862                 TAILQ_FOREACH(flow, &internals->flow_list, next) {
1863                         memmove(&flow->flows[i], &flow->flows[i + 1],
1864                                 sizeof(flow->flows[0]) *
1865                                 (internals->slave_count - i - 1));
1866                         flow->flows[internals->slave_count - 1] = NULL;
1867                 }
1868         }
1869
1870         internals->slave_count--;
1871
1872         /* force reconfiguration of slave interfaces */
1873         _rte_eth_dev_reset(slave_eth_dev);
1874 }
1875
1876 static void
1877 bond_ethdev_slave_link_status_change_monitor(void *cb_arg);
1878
1879 void
1880 slave_add(struct bond_dev_private *internals,
1881                 struct rte_eth_dev *slave_eth_dev)
1882 {
1883         struct bond_slave_details *slave_details =
1884                         &internals->slaves[internals->slave_count];
1885
1886         slave_details->port_id = slave_eth_dev->data->port_id;
1887         slave_details->last_link_status = 0;
1888
1889         /* Mark slave devices that don't support interrupts so we can
1890          * compensate when we start the bond
1891          */
1892         if (!(slave_eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
1893                 slave_details->link_status_poll_enabled = 1;
1894         }
1895
1896         slave_details->link_status_wait_to_complete = 0;
1897         /* clean tlb_last_obytes when adding port for bonding device */
1898         memcpy(&(slave_details->persisted_mac_addr), slave_eth_dev->data->mac_addrs,
1899                         sizeof(struct rte_ether_addr));
1900 }
1901
1902 void
1903 bond_ethdev_primary_set(struct bond_dev_private *internals,
1904                 uint16_t slave_port_id)
1905 {
1906         int i;
1907
1908         if (internals->active_slave_count < 1)
1909                 internals->current_primary_port = slave_port_id;
1910         else
1911                 /* Search bonded device slave ports for new proposed primary port */
1912                 for (i = 0; i < internals->active_slave_count; i++) {
1913                         if (internals->active_slaves[i] == slave_port_id)
1914                                 internals->current_primary_port = slave_port_id;
1915                 }
1916 }
1917
1918 static int
1919 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev);
1920
1921 static int
1922 bond_ethdev_start(struct rte_eth_dev *eth_dev)
1923 {
1924         struct bond_dev_private *internals;
1925         int i;
1926
1927         /* slave eth dev will be started by bonded device */
1928         if (check_for_bonded_ethdev(eth_dev)) {
1929                 RTE_BOND_LOG(ERR, "User tried to explicitly start a slave eth_dev (%d)",
1930                                 eth_dev->data->port_id);
1931                 return -1;
1932         }
1933
1934         eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
1935         eth_dev->data->dev_started = 1;
1936
1937         internals = eth_dev->data->dev_private;
1938
1939         if (internals->slave_count == 0) {
1940                 RTE_BOND_LOG(ERR, "Cannot start port since there are no slave devices");
1941                 goto out_err;
1942         }
1943
1944         if (internals->user_defined_mac == 0) {
1945                 struct rte_ether_addr *new_mac_addr = NULL;
1946
1947                 for (i = 0; i < internals->slave_count; i++)
1948                         if (internals->slaves[i].port_id == internals->primary_port)
1949                                 new_mac_addr = &internals->slaves[i].persisted_mac_addr;
1950
1951                 if (new_mac_addr == NULL)
1952                         goto out_err;
1953
1954                 if (mac_address_set(eth_dev, new_mac_addr) != 0) {
1955                         RTE_BOND_LOG(ERR, "bonded port (%d) failed to update MAC address",
1956                                         eth_dev->data->port_id);
1957                         goto out_err;
1958                 }
1959         }
1960
1961         if (internals->mode == BONDING_MODE_8023AD) {
1962                 if (internals->mode4.dedicated_queues.enabled == 1) {
1963                         internals->mode4.dedicated_queues.rx_qid =
1964                                         eth_dev->data->nb_rx_queues;
1965                         internals->mode4.dedicated_queues.tx_qid =
1966                                         eth_dev->data->nb_tx_queues;
1967                 }
1968         }
1969
1970
1971         /* Reconfigure each slave device if starting bonded device */
1972         for (i = 0; i < internals->slave_count; i++) {
1973                 struct rte_eth_dev *slave_ethdev =
1974                                 &(rte_eth_devices[internals->slaves[i].port_id]);
1975                 if (slave_configure(eth_dev, slave_ethdev) != 0) {
1976                         RTE_BOND_LOG(ERR,
1977                                 "bonded port (%d) failed to reconfigure slave device (%d)",
1978                                 eth_dev->data->port_id,
1979                                 internals->slaves[i].port_id);
1980                         goto out_err;
1981                 }
1982                 /* We will need to poll for link status if any slave doesn't
1983                  * support interrupts
1984                  */
1985                 if (internals->slaves[i].link_status_poll_enabled)
1986                         internals->link_status_polling_enabled = 1;
1987         }
1988
1989         /* start polling if needed */
1990         if (internals->link_status_polling_enabled) {
1991                 rte_eal_alarm_set(
1992                         internals->link_status_polling_interval_ms * 1000,
1993                         bond_ethdev_slave_link_status_change_monitor,
1994                         (void *)&rte_eth_devices[internals->port_id]);
1995         }
1996
1997         /* Update all slave devices MACs*/
1998         if (mac_address_slaves_update(eth_dev) != 0)
1999                 goto out_err;
2000
2001         if (internals->user_defined_primary_port)
2002                 bond_ethdev_primary_set(internals, internals->primary_port);
2003
2004         if (internals->mode == BONDING_MODE_8023AD)
2005                 bond_mode_8023ad_start(eth_dev);
2006
2007         if (internals->mode == BONDING_MODE_TLB ||
2008                         internals->mode == BONDING_MODE_ALB)
2009                 bond_tlb_enable(internals);
2010
2011         return 0;
2012
2013 out_err:
2014         eth_dev->data->dev_started = 0;
2015         return -1;
2016 }
2017
2018 static void
2019 bond_ethdev_free_queues(struct rte_eth_dev *dev)
2020 {
2021         uint16_t i;
2022
2023         if (dev->data->rx_queues != NULL) {
2024                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
2025                         rte_free(dev->data->rx_queues[i]);
2026                         dev->data->rx_queues[i] = NULL;
2027                 }
2028                 dev->data->nb_rx_queues = 0;
2029         }
2030
2031         if (dev->data->tx_queues != NULL) {
2032                 for (i = 0; i < dev->data->nb_tx_queues; i++) {
2033                         rte_free(dev->data->tx_queues[i]);
2034                         dev->data->tx_queues[i] = NULL;
2035                 }
2036                 dev->data->nb_tx_queues = 0;
2037         }
2038 }
2039
2040 void
2041 bond_ethdev_stop(struct rte_eth_dev *eth_dev)
2042 {
2043         struct bond_dev_private *internals = eth_dev->data->dev_private;
2044         uint16_t i;
2045
2046         if (internals->mode == BONDING_MODE_8023AD) {
2047                 struct port *port;
2048                 void *pkt = NULL;
2049
2050                 bond_mode_8023ad_stop(eth_dev);
2051
2052                 /* Discard all messages to/from mode 4 state machines */
2053                 for (i = 0; i < internals->active_slave_count; i++) {
2054                         port = &bond_mode_8023ad_ports[internals->active_slaves[i]];
2055
2056                         RTE_ASSERT(port->rx_ring != NULL);
2057                         while (rte_ring_dequeue(port->rx_ring, &pkt) != -ENOENT)
2058                                 rte_pktmbuf_free(pkt);
2059
2060                         RTE_ASSERT(port->tx_ring != NULL);
2061                         while (rte_ring_dequeue(port->tx_ring, &pkt) != -ENOENT)
2062                                 rte_pktmbuf_free(pkt);
2063                 }
2064         }
2065
2066         if (internals->mode == BONDING_MODE_TLB ||
2067                         internals->mode == BONDING_MODE_ALB) {
2068                 bond_tlb_disable(internals);
2069                 for (i = 0; i < internals->active_slave_count; i++)
2070                         tlb_last_obytets[internals->active_slaves[i]] = 0;
2071         }
2072
2073         eth_dev->data->dev_link.link_status = ETH_LINK_DOWN;
2074         eth_dev->data->dev_started = 0;
2075
2076         internals->link_status_polling_enabled = 0;
2077         for (i = 0; i < internals->slave_count; i++) {
2078                 uint16_t slave_id = internals->slaves[i].port_id;
2079                 if (find_slave_by_id(internals->active_slaves,
2080                                 internals->active_slave_count, slave_id) !=
2081                                                 internals->active_slave_count) {
2082                         internals->slaves[i].last_link_status = 0;
2083                         rte_eth_dev_stop(slave_id);
2084                         deactivate_slave(eth_dev, slave_id);
2085                 }
2086         }
2087 }
2088
2089 void
2090 bond_ethdev_close(struct rte_eth_dev *dev)
2091 {
2092         struct bond_dev_private *internals = dev->data->dev_private;
2093         uint16_t bond_port_id = internals->port_id;
2094         int skipped = 0;
2095         struct rte_flow_error ferror;
2096
2097         RTE_BOND_LOG(INFO, "Closing bonded device %s", dev->device->name);
2098         while (internals->slave_count != skipped) {
2099                 uint16_t port_id = internals->slaves[skipped].port_id;
2100
2101                 rte_eth_dev_stop(port_id);
2102
2103                 if (rte_eth_bond_slave_remove(bond_port_id, port_id) != 0) {
2104                         RTE_BOND_LOG(ERR,
2105                                      "Failed to remove port %d from bonded device %s",
2106                                      port_id, dev->device->name);
2107                         skipped++;
2108                 }
2109         }
2110         bond_flow_ops.flush(dev, &ferror);
2111         bond_ethdev_free_queues(dev);
2112         rte_bitmap_reset(internals->vlan_filter_bmp);
2113 }
2114
2115 /* forward declaration */
2116 static int bond_ethdev_configure(struct rte_eth_dev *dev);
2117
2118 static int
2119 bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2120 {
2121         struct bond_dev_private *internals = dev->data->dev_private;
2122         struct bond_slave_details slave;
2123         int ret;
2124
2125         uint16_t max_nb_rx_queues = UINT16_MAX;
2126         uint16_t max_nb_tx_queues = UINT16_MAX;
2127         uint16_t max_rx_desc_lim = UINT16_MAX;
2128         uint16_t max_tx_desc_lim = UINT16_MAX;
2129
2130         dev_info->max_mac_addrs = BOND_MAX_MAC_ADDRS;
2131
2132         dev_info->max_rx_pktlen = internals->candidate_max_rx_pktlen ?
2133                         internals->candidate_max_rx_pktlen :
2134                         RTE_ETHER_MAX_JUMBO_FRAME_LEN;
2135
2136         /* Max number of tx/rx queues that the bonded device can support is the
2137          * minimum values of the bonded slaves, as all slaves must be capable
2138          * of supporting the same number of tx/rx queues.
2139          */
2140         if (internals->slave_count > 0) {
2141                 struct rte_eth_dev_info slave_info;
2142                 uint16_t idx;
2143
2144                 for (idx = 0; idx < internals->slave_count; idx++) {
2145                         slave = internals->slaves[idx];
2146                         ret = rte_eth_dev_info_get(slave.port_id, &slave_info);
2147                         if (ret != 0) {
2148                                 RTE_BOND_LOG(ERR,
2149                                         "%s: Error during getting device (port %u) info: %s\n",
2150                                         __func__,
2151                                         slave.port_id,
2152                                         strerror(-ret));
2153
2154                                 return ret;
2155                         }
2156
2157                         if (slave_info.max_rx_queues < max_nb_rx_queues)
2158                                 max_nb_rx_queues = slave_info.max_rx_queues;
2159
2160                         if (slave_info.max_tx_queues < max_nb_tx_queues)
2161                                 max_nb_tx_queues = slave_info.max_tx_queues;
2162
2163                         if (slave_info.rx_desc_lim.nb_max < max_rx_desc_lim)
2164                                 max_rx_desc_lim = slave_info.rx_desc_lim.nb_max;
2165
2166                         if (slave_info.tx_desc_lim.nb_max < max_tx_desc_lim)
2167                                 max_tx_desc_lim = slave_info.tx_desc_lim.nb_max;
2168                 }
2169         }
2170
2171         dev_info->max_rx_queues = max_nb_rx_queues;
2172         dev_info->max_tx_queues = max_nb_tx_queues;
2173
2174         memcpy(&dev_info->default_rxconf, &internals->default_rxconf,
2175                sizeof(dev_info->default_rxconf));
2176         memcpy(&dev_info->default_txconf, &internals->default_txconf,
2177                sizeof(dev_info->default_txconf));
2178
2179         dev_info->rx_desc_lim.nb_max = max_rx_desc_lim;
2180         dev_info->tx_desc_lim.nb_max = max_tx_desc_lim;
2181
2182         /**
2183          * If dedicated hw queues enabled for link bonding device in LACP mode
2184          * then we need to reduce the maximum number of data path queues by 1.
2185          */
2186         if (internals->mode == BONDING_MODE_8023AD &&
2187                 internals->mode4.dedicated_queues.enabled == 1) {
2188                 dev_info->max_rx_queues--;
2189                 dev_info->max_tx_queues--;
2190         }
2191
2192         dev_info->min_rx_bufsize = 0;
2193
2194         dev_info->rx_offload_capa = internals->rx_offload_capa;
2195         dev_info->tx_offload_capa = internals->tx_offload_capa;
2196         dev_info->rx_queue_offload_capa = internals->rx_queue_offload_capa;
2197         dev_info->tx_queue_offload_capa = internals->tx_queue_offload_capa;
2198         dev_info->flow_type_rss_offloads = internals->flow_type_rss_offloads;
2199
2200         dev_info->reta_size = internals->reta_size;
2201
2202         return 0;
2203 }
2204
2205 static int
2206 bond_ethdev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
2207 {
2208         int res;
2209         uint16_t i;
2210         struct bond_dev_private *internals = dev->data->dev_private;
2211
2212         /* don't do this while a slave is being added */
2213         rte_spinlock_lock(&internals->lock);
2214
2215         if (on)
2216                 rte_bitmap_set(internals->vlan_filter_bmp, vlan_id);
2217         else
2218                 rte_bitmap_clear(internals->vlan_filter_bmp, vlan_id);
2219
2220         for (i = 0; i < internals->slave_count; i++) {
2221                 uint16_t port_id = internals->slaves[i].port_id;
2222
2223                 res = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
2224                 if (res == ENOTSUP)
2225                         RTE_BOND_LOG(WARNING,
2226                                      "Setting VLAN filter on slave port %u not supported.",
2227                                      port_id);
2228         }
2229
2230         rte_spinlock_unlock(&internals->lock);
2231         return 0;
2232 }
2233
2234 static int
2235 bond_ethdev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
2236                 uint16_t nb_rx_desc, unsigned int socket_id __rte_unused,
2237                 const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool)
2238 {
2239         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)
2240                         rte_zmalloc_socket(NULL, sizeof(struct bond_rx_queue),
2241                                         0, dev->data->numa_node);
2242         if (bd_rx_q == NULL)
2243                 return -1;
2244
2245         bd_rx_q->queue_id = rx_queue_id;
2246         bd_rx_q->dev_private = dev->data->dev_private;
2247
2248         bd_rx_q->nb_rx_desc = nb_rx_desc;
2249
2250         memcpy(&(bd_rx_q->rx_conf), rx_conf, sizeof(struct rte_eth_rxconf));
2251         bd_rx_q->mb_pool = mb_pool;
2252
2253         dev->data->rx_queues[rx_queue_id] = bd_rx_q;
2254
2255         return 0;
2256 }
2257
2258 static int
2259 bond_ethdev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
2260                 uint16_t nb_tx_desc, unsigned int socket_id __rte_unused,
2261                 const struct rte_eth_txconf *tx_conf)
2262 {
2263         struct bond_tx_queue *bd_tx_q  = (struct bond_tx_queue *)
2264                         rte_zmalloc_socket(NULL, sizeof(struct bond_tx_queue),
2265                                         0, dev->data->numa_node);
2266
2267         if (bd_tx_q == NULL)
2268                 return -1;
2269
2270         bd_tx_q->queue_id = tx_queue_id;
2271         bd_tx_q->dev_private = dev->data->dev_private;
2272
2273         bd_tx_q->nb_tx_desc = nb_tx_desc;
2274         memcpy(&(bd_tx_q->tx_conf), tx_conf, sizeof(bd_tx_q->tx_conf));
2275
2276         dev->data->tx_queues[tx_queue_id] = bd_tx_q;
2277
2278         return 0;
2279 }
2280
2281 static void
2282 bond_ethdev_rx_queue_release(void *queue)
2283 {
2284         if (queue == NULL)
2285                 return;
2286
2287         rte_free(queue);
2288 }
2289
2290 static void
2291 bond_ethdev_tx_queue_release(void *queue)
2292 {
2293         if (queue == NULL)
2294                 return;
2295
2296         rte_free(queue);
2297 }
2298
2299 static void
2300 bond_ethdev_slave_link_status_change_monitor(void *cb_arg)
2301 {
2302         struct rte_eth_dev *bonded_ethdev, *slave_ethdev;
2303         struct bond_dev_private *internals;
2304
2305         /* Default value for polling slave found is true as we don't want to
2306          * disable the polling thread if we cannot get the lock */
2307         int i, polling_slave_found = 1;
2308
2309         if (cb_arg == NULL)
2310                 return;
2311
2312         bonded_ethdev = cb_arg;
2313         internals = bonded_ethdev->data->dev_private;
2314
2315         if (!bonded_ethdev->data->dev_started ||
2316                 !internals->link_status_polling_enabled)
2317                 return;
2318
2319         /* If device is currently being configured then don't check slaves link
2320          * status, wait until next period */
2321         if (rte_spinlock_trylock(&internals->lock)) {
2322                 if (internals->slave_count > 0)
2323                         polling_slave_found = 0;
2324
2325                 for (i = 0; i < internals->slave_count; i++) {
2326                         if (!internals->slaves[i].link_status_poll_enabled)
2327                                 continue;
2328
2329                         slave_ethdev = &rte_eth_devices[internals->slaves[i].port_id];
2330                         polling_slave_found = 1;
2331
2332                         /* Update slave link status */
2333                         (*slave_ethdev->dev_ops->link_update)(slave_ethdev,
2334                                         internals->slaves[i].link_status_wait_to_complete);
2335
2336                         /* if link status has changed since last checked then call lsc
2337                          * event callback */
2338                         if (slave_ethdev->data->dev_link.link_status !=
2339                                         internals->slaves[i].last_link_status) {
2340                                 internals->slaves[i].last_link_status =
2341                                                 slave_ethdev->data->dev_link.link_status;
2342
2343                                 bond_ethdev_lsc_event_callback(internals->slaves[i].port_id,
2344                                                 RTE_ETH_EVENT_INTR_LSC,
2345                                                 &bonded_ethdev->data->port_id,
2346                                                 NULL);
2347                         }
2348                 }
2349                 rte_spinlock_unlock(&internals->lock);
2350         }
2351
2352         if (polling_slave_found)
2353                 /* Set alarm to continue monitoring link status of slave ethdev's */
2354                 rte_eal_alarm_set(internals->link_status_polling_interval_ms * 1000,
2355                                 bond_ethdev_slave_link_status_change_monitor, cb_arg);
2356 }
2357
2358 static int
2359 bond_ethdev_link_update(struct rte_eth_dev *ethdev, int wait_to_complete)
2360 {
2361         void (*link_update)(uint16_t port_id, struct rte_eth_link *eth_link);
2362
2363         struct bond_dev_private *bond_ctx;
2364         struct rte_eth_link slave_link;
2365
2366         uint32_t idx;
2367
2368         bond_ctx = ethdev->data->dev_private;
2369
2370         ethdev->data->dev_link.link_speed = ETH_SPEED_NUM_NONE;
2371
2372         if (ethdev->data->dev_started == 0 ||
2373                         bond_ctx->active_slave_count == 0) {
2374                 ethdev->data->dev_link.link_status = ETH_LINK_DOWN;
2375                 return 0;
2376         }
2377
2378         ethdev->data->dev_link.link_status = ETH_LINK_UP;
2379
2380         if (wait_to_complete)
2381                 link_update = rte_eth_link_get;
2382         else
2383                 link_update = rte_eth_link_get_nowait;
2384
2385         switch (bond_ctx->mode) {
2386         case BONDING_MODE_BROADCAST:
2387                 /**
2388                  * Setting link speed to UINT32_MAX to ensure we pick up the
2389                  * value of the first active slave
2390                  */
2391                 ethdev->data->dev_link.link_speed = UINT32_MAX;
2392
2393                 /**
2394                  * link speed is minimum value of all the slaves link speed as
2395                  * packet loss will occur on this slave if transmission at rates
2396                  * greater than this are attempted
2397                  */
2398                 for (idx = 1; idx < bond_ctx->active_slave_count; idx++) {
2399                         link_update(bond_ctx->active_slaves[0], &slave_link);
2400
2401                         if (slave_link.link_speed <
2402                                         ethdev->data->dev_link.link_speed)
2403                                 ethdev->data->dev_link.link_speed =
2404                                                 slave_link.link_speed;
2405                 }
2406                 break;
2407         case BONDING_MODE_ACTIVE_BACKUP:
2408                 /* Current primary slave */
2409                 link_update(bond_ctx->current_primary_port, &slave_link);
2410
2411                 ethdev->data->dev_link.link_speed = slave_link.link_speed;
2412                 break;
2413         case BONDING_MODE_8023AD:
2414                 ethdev->data->dev_link.link_autoneg =
2415                                 bond_ctx->mode4.slave_link.link_autoneg;
2416                 ethdev->data->dev_link.link_duplex =
2417                                 bond_ctx->mode4.slave_link.link_duplex;
2418                 /* fall through to update link speed */
2419         case BONDING_MODE_ROUND_ROBIN:
2420         case BONDING_MODE_BALANCE:
2421         case BONDING_MODE_TLB:
2422         case BONDING_MODE_ALB:
2423         default:
2424                 /**
2425                  * In theses mode the maximum theoretical link speed is the sum
2426                  * of all the slaves
2427                  */
2428                 ethdev->data->dev_link.link_speed = ETH_SPEED_NUM_NONE;
2429
2430                 for (idx = 0; idx < bond_ctx->active_slave_count; idx++) {
2431                         link_update(bond_ctx->active_slaves[idx], &slave_link);
2432
2433                         ethdev->data->dev_link.link_speed +=
2434                                         slave_link.link_speed;
2435                 }
2436         }
2437
2438
2439         return 0;
2440 }
2441
2442
2443 static int
2444 bond_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2445 {
2446         struct bond_dev_private *internals = dev->data->dev_private;
2447         struct rte_eth_stats slave_stats;
2448         int i, j;
2449
2450         for (i = 0; i < internals->slave_count; i++) {
2451                 rte_eth_stats_get(internals->slaves[i].port_id, &slave_stats);
2452
2453                 stats->ipackets += slave_stats.ipackets;
2454                 stats->opackets += slave_stats.opackets;
2455                 stats->ibytes += slave_stats.ibytes;
2456                 stats->obytes += slave_stats.obytes;
2457                 stats->imissed += slave_stats.imissed;
2458                 stats->ierrors += slave_stats.ierrors;
2459                 stats->oerrors += slave_stats.oerrors;
2460                 stats->rx_nombuf += slave_stats.rx_nombuf;
2461
2462                 for (j = 0; j < RTE_ETHDEV_QUEUE_STAT_CNTRS; j++) {
2463                         stats->q_ipackets[j] += slave_stats.q_ipackets[j];
2464                         stats->q_opackets[j] += slave_stats.q_opackets[j];
2465                         stats->q_ibytes[j] += slave_stats.q_ibytes[j];
2466                         stats->q_obytes[j] += slave_stats.q_obytes[j];
2467                         stats->q_errors[j] += slave_stats.q_errors[j];
2468                 }
2469
2470         }
2471
2472         return 0;
2473 }
2474
2475 static void
2476 bond_ethdev_stats_reset(struct rte_eth_dev *dev)
2477 {
2478         struct bond_dev_private *internals = dev->data->dev_private;
2479         int i;
2480
2481         for (i = 0; i < internals->slave_count; i++)
2482                 rte_eth_stats_reset(internals->slaves[i].port_id);
2483 }
2484
2485 static int
2486 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev)
2487 {
2488         struct bond_dev_private *internals = eth_dev->data->dev_private;
2489         int i;
2490         int ret = 0;
2491         uint16_t port_id;
2492
2493         switch (internals->mode) {
2494         /* Promiscuous mode is propagated to all slaves */
2495         case BONDING_MODE_ROUND_ROBIN:
2496         case BONDING_MODE_BALANCE:
2497         case BONDING_MODE_BROADCAST:
2498         case BONDING_MODE_8023AD: {
2499                 unsigned int slave_ok = 0;
2500
2501                 for (i = 0; i < internals->slave_count; i++) {
2502                         port_id = internals->slaves[i].port_id;
2503
2504                         ret = rte_eth_promiscuous_enable(port_id);
2505                         if (ret != 0)
2506                                 RTE_BOND_LOG(ERR,
2507                                         "Failed to enable promiscuous mode for port %u: %s",
2508                                         port_id, rte_strerror(-ret));
2509                         else
2510                                 slave_ok++;
2511                 }
2512                 /*
2513                  * Report success if operation is successful on at least
2514                  * on one slave. Otherwise return last error code.
2515                  */
2516                 if (slave_ok > 0)
2517                         ret = 0;
2518                 break;
2519         }
2520         /* Promiscuous mode is propagated only to primary slave */
2521         case BONDING_MODE_ACTIVE_BACKUP:
2522         case BONDING_MODE_TLB:
2523         case BONDING_MODE_ALB:
2524         default:
2525                 /* Do not touch promisc when there cannot be primary ports */
2526                 if (internals->slave_count == 0)
2527                         break;
2528                 port_id = internals->current_primary_port;
2529                 ret = rte_eth_promiscuous_enable(port_id);
2530                 if (ret != 0)
2531                         RTE_BOND_LOG(ERR,
2532                                 "Failed to enable promiscuous mode for port %u: %s",
2533                                 port_id, rte_strerror(-ret));
2534         }
2535
2536         return ret;
2537 }
2538
2539 static int
2540 bond_ethdev_promiscuous_disable(struct rte_eth_dev *dev)
2541 {
2542         struct bond_dev_private *internals = dev->data->dev_private;
2543         int i;
2544         int ret = 0;
2545         uint16_t port_id;
2546
2547         switch (internals->mode) {
2548         /* Promiscuous mode is propagated to all slaves */
2549         case BONDING_MODE_ROUND_ROBIN:
2550         case BONDING_MODE_BALANCE:
2551         case BONDING_MODE_BROADCAST:
2552         case BONDING_MODE_8023AD: {
2553                 unsigned int slave_ok = 0;
2554
2555                 for (i = 0; i < internals->slave_count; i++) {
2556                         port_id = internals->slaves[i].port_id;
2557
2558                         if (internals->mode == BONDING_MODE_8023AD &&
2559                             bond_mode_8023ad_ports[port_id].forced_rx_flags ==
2560                                         BOND_8023AD_FORCED_PROMISC) {
2561                                 slave_ok++;
2562                                 continue;
2563                         }
2564                         ret = rte_eth_promiscuous_disable(port_id);
2565                         if (ret != 0)
2566                                 RTE_BOND_LOG(ERR,
2567                                         "Failed to disable promiscuous mode for port %u: %s",
2568                                         port_id, rte_strerror(-ret));
2569                         else
2570                                 slave_ok++;
2571                 }
2572                 /*
2573                  * Report success if operation is successful on at least
2574                  * on one slave. Otherwise return last error code.
2575                  */
2576                 if (slave_ok > 0)
2577                         ret = 0;
2578                 break;
2579         }
2580         /* Promiscuous mode is propagated only to primary slave */
2581         case BONDING_MODE_ACTIVE_BACKUP:
2582         case BONDING_MODE_TLB:
2583         case BONDING_MODE_ALB:
2584         default:
2585                 /* Do not touch promisc when there cannot be primary ports */
2586                 if (internals->slave_count == 0)
2587                         break;
2588                 port_id = internals->current_primary_port;
2589                 ret = rte_eth_promiscuous_disable(port_id);
2590                 if (ret != 0)
2591                         RTE_BOND_LOG(ERR,
2592                                 "Failed to disable promiscuous mode for port %u: %s",
2593                                 port_id, rte_strerror(-ret));
2594         }
2595
2596         return ret;
2597 }
2598
2599 static void
2600 bond_ethdev_allmulticast_enable(struct rte_eth_dev *eth_dev)
2601 {
2602         struct bond_dev_private *internals = eth_dev->data->dev_private;
2603         int i;
2604
2605         switch (internals->mode) {
2606         /* allmulti mode is propagated to all slaves */
2607         case BONDING_MODE_ROUND_ROBIN:
2608         case BONDING_MODE_BALANCE:
2609         case BONDING_MODE_BROADCAST:
2610         case BONDING_MODE_8023AD:
2611                 for (i = 0; i < internals->slave_count; i++) {
2612                         uint16_t port_id = internals->slaves[i].port_id;
2613
2614                         rte_eth_allmulticast_enable(port_id);
2615                 }
2616                 break;
2617         /* allmulti mode is propagated only to primary slave */
2618         case BONDING_MODE_ACTIVE_BACKUP:
2619         case BONDING_MODE_TLB:
2620         case BONDING_MODE_ALB:
2621         default:
2622                 /* Do not touch allmulti when there cannot be primary ports */
2623                 if (internals->slave_count == 0)
2624                         break;
2625                 rte_eth_allmulticast_enable(internals->current_primary_port);
2626         }
2627 }
2628
2629 static void
2630 bond_ethdev_allmulticast_disable(struct rte_eth_dev *eth_dev)
2631 {
2632         struct bond_dev_private *internals = eth_dev->data->dev_private;
2633         int i;
2634
2635         switch (internals->mode) {
2636         /* allmulti mode is propagated to all slaves */
2637         case BONDING_MODE_ROUND_ROBIN:
2638         case BONDING_MODE_BALANCE:
2639         case BONDING_MODE_BROADCAST:
2640         case BONDING_MODE_8023AD:
2641                 for (i = 0; i < internals->slave_count; i++) {
2642                         uint16_t port_id = internals->slaves[i].port_id;
2643
2644                         if (internals->mode == BONDING_MODE_8023AD &&
2645                             bond_mode_8023ad_ports[port_id].forced_rx_flags ==
2646                                         BOND_8023AD_FORCED_ALLMULTI)
2647                                 continue;
2648                         rte_eth_allmulticast_disable(port_id);
2649                 }
2650                 break;
2651         /* allmulti mode is propagated only to primary slave */
2652         case BONDING_MODE_ACTIVE_BACKUP:
2653         case BONDING_MODE_TLB:
2654         case BONDING_MODE_ALB:
2655         default:
2656                 /* Do not touch allmulti when there cannot be primary ports */
2657                 if (internals->slave_count == 0)
2658                         break;
2659                 rte_eth_allmulticast_disable(internals->current_primary_port);
2660         }
2661 }
2662
2663 static void
2664 bond_ethdev_delayed_lsc_propagation(void *arg)
2665 {
2666         if (arg == NULL)
2667                 return;
2668
2669         _rte_eth_dev_callback_process((struct rte_eth_dev *)arg,
2670                         RTE_ETH_EVENT_INTR_LSC, NULL);
2671 }
2672
2673 int
2674 bond_ethdev_lsc_event_callback(uint16_t port_id, enum rte_eth_event_type type,
2675                 void *param, void *ret_param __rte_unused)
2676 {
2677         struct rte_eth_dev *bonded_eth_dev;
2678         struct bond_dev_private *internals;
2679         struct rte_eth_link link;
2680         int rc = -1;
2681
2682         uint8_t lsc_flag = 0;
2683         int valid_slave = 0;
2684         uint16_t active_pos;
2685         uint16_t i;
2686
2687         if (type != RTE_ETH_EVENT_INTR_LSC || param == NULL)
2688                 return rc;
2689
2690         bonded_eth_dev = &rte_eth_devices[*(uint16_t *)param];
2691
2692         if (check_for_bonded_ethdev(bonded_eth_dev))
2693                 return rc;
2694
2695         internals = bonded_eth_dev->data->dev_private;
2696
2697         /* If the device isn't started don't handle interrupts */
2698         if (!bonded_eth_dev->data->dev_started)
2699                 return rc;
2700
2701         /* verify that port_id is a valid slave of bonded port */
2702         for (i = 0; i < internals->slave_count; i++) {
2703                 if (internals->slaves[i].port_id == port_id) {
2704                         valid_slave = 1;
2705                         break;
2706                 }
2707         }
2708
2709         if (!valid_slave)
2710                 return rc;
2711
2712         /* Synchronize lsc callback parallel calls either by real link event
2713          * from the slaves PMDs or by the bonding PMD itself.
2714          */
2715         rte_spinlock_lock(&internals->lsc_lock);
2716
2717         /* Search for port in active port list */
2718         active_pos = find_slave_by_id(internals->active_slaves,
2719                         internals->active_slave_count, port_id);
2720
2721         rte_eth_link_get_nowait(port_id, &link);
2722         if (link.link_status) {
2723                 if (active_pos < internals->active_slave_count)
2724                         goto link_update;
2725
2726                 /* check link state properties if bonded link is up*/
2727                 if (bonded_eth_dev->data->dev_link.link_status == ETH_LINK_UP) {
2728                         if (link_properties_valid(bonded_eth_dev, &link) != 0)
2729                                 RTE_BOND_LOG(ERR, "Invalid link properties "
2730                                              "for slave %d in bonding mode %d",
2731                                              port_id, internals->mode);
2732                 } else {
2733                         /* inherit slave link properties */
2734                         link_properties_set(bonded_eth_dev, &link);
2735                 }
2736
2737                 /* If no active slave ports then set this port to be
2738                  * the primary port.
2739                  */
2740                 if (internals->active_slave_count < 1) {
2741                         /* If first active slave, then change link status */
2742                         bonded_eth_dev->data->dev_link.link_status =
2743                                                                 ETH_LINK_UP;
2744                         internals->current_primary_port = port_id;
2745                         lsc_flag = 1;
2746
2747                         mac_address_slaves_update(bonded_eth_dev);
2748                 }
2749
2750                 activate_slave(bonded_eth_dev, port_id);
2751
2752                 /* If the user has defined the primary port then default to
2753                  * using it.
2754                  */
2755                 if (internals->user_defined_primary_port &&
2756                                 internals->primary_port == port_id)
2757                         bond_ethdev_primary_set(internals, port_id);
2758         } else {
2759                 if (active_pos == internals->active_slave_count)
2760                         goto link_update;
2761
2762                 /* Remove from active slave list */
2763                 deactivate_slave(bonded_eth_dev, port_id);
2764
2765                 if (internals->active_slave_count < 1)
2766                         lsc_flag = 1;
2767
2768                 /* Update primary id, take first active slave from list or if none
2769                  * available set to -1 */
2770                 if (port_id == internals->current_primary_port) {
2771                         if (internals->active_slave_count > 0)
2772                                 bond_ethdev_primary_set(internals,
2773                                                 internals->active_slaves[0]);
2774                         else
2775                                 internals->current_primary_port = internals->primary_port;
2776                 }
2777         }
2778
2779 link_update:
2780         /**
2781          * Update bonded device link properties after any change to active
2782          * slaves
2783          */
2784         bond_ethdev_link_update(bonded_eth_dev, 0);
2785
2786         if (lsc_flag) {
2787                 /* Cancel any possible outstanding interrupts if delays are enabled */
2788                 if (internals->link_up_delay_ms > 0 ||
2789                         internals->link_down_delay_ms > 0)
2790                         rte_eal_alarm_cancel(bond_ethdev_delayed_lsc_propagation,
2791                                         bonded_eth_dev);
2792
2793                 if (bonded_eth_dev->data->dev_link.link_status) {
2794                         if (internals->link_up_delay_ms > 0)
2795                                 rte_eal_alarm_set(internals->link_up_delay_ms * 1000,
2796                                                 bond_ethdev_delayed_lsc_propagation,
2797                                                 (void *)bonded_eth_dev);
2798                         else
2799                                 _rte_eth_dev_callback_process(bonded_eth_dev,
2800                                                 RTE_ETH_EVENT_INTR_LSC,
2801                                                 NULL);
2802
2803                 } else {
2804                         if (internals->link_down_delay_ms > 0)
2805                                 rte_eal_alarm_set(internals->link_down_delay_ms * 1000,
2806                                                 bond_ethdev_delayed_lsc_propagation,
2807                                                 (void *)bonded_eth_dev);
2808                         else
2809                                 _rte_eth_dev_callback_process(bonded_eth_dev,
2810                                                 RTE_ETH_EVENT_INTR_LSC,
2811                                                 NULL);
2812                 }
2813         }
2814
2815         rte_spinlock_unlock(&internals->lsc_lock);
2816
2817         return rc;
2818 }
2819
2820 static int
2821 bond_ethdev_rss_reta_update(struct rte_eth_dev *dev,
2822                 struct rte_eth_rss_reta_entry64 *reta_conf, uint16_t reta_size)
2823 {
2824         unsigned i, j;
2825         int result = 0;
2826         int slave_reta_size;
2827         unsigned reta_count;
2828         struct bond_dev_private *internals = dev->data->dev_private;
2829
2830         if (reta_size != internals->reta_size)
2831                 return -EINVAL;
2832
2833          /* Copy RETA table */
2834         reta_count = reta_size / RTE_RETA_GROUP_SIZE;
2835
2836         for (i = 0; i < reta_count; i++) {
2837                 internals->reta_conf[i].mask = reta_conf[i].mask;
2838                 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++)
2839                         if ((reta_conf[i].mask >> j) & 0x01)
2840                                 internals->reta_conf[i].reta[j] = reta_conf[i].reta[j];
2841         }
2842
2843         /* Fill rest of array */
2844         for (; i < RTE_DIM(internals->reta_conf); i += reta_count)
2845                 memcpy(&internals->reta_conf[i], &internals->reta_conf[0],
2846                                 sizeof(internals->reta_conf[0]) * reta_count);
2847
2848         /* Propagate RETA over slaves */
2849         for (i = 0; i < internals->slave_count; i++) {
2850                 slave_reta_size = internals->slaves[i].reta_size;
2851                 result = rte_eth_dev_rss_reta_update(internals->slaves[i].port_id,
2852                                 &internals->reta_conf[0], slave_reta_size);
2853                 if (result < 0)
2854                         return result;
2855         }
2856
2857         return 0;
2858 }
2859
2860 static int
2861 bond_ethdev_rss_reta_query(struct rte_eth_dev *dev,
2862                 struct rte_eth_rss_reta_entry64 *reta_conf, uint16_t reta_size)
2863 {
2864         int i, j;
2865         struct bond_dev_private *internals = dev->data->dev_private;
2866
2867         if (reta_size != internals->reta_size)
2868                 return -EINVAL;
2869
2870          /* Copy RETA table */
2871         for (i = 0; i < reta_size / RTE_RETA_GROUP_SIZE; i++)
2872                 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++)
2873                         if ((reta_conf[i].mask >> j) & 0x01)
2874                                 reta_conf[i].reta[j] = internals->reta_conf[i].reta[j];
2875
2876         return 0;
2877 }
2878
2879 static int
2880 bond_ethdev_rss_hash_update(struct rte_eth_dev *dev,
2881                 struct rte_eth_rss_conf *rss_conf)
2882 {
2883         int i, result = 0;
2884         struct bond_dev_private *internals = dev->data->dev_private;
2885         struct rte_eth_rss_conf bond_rss_conf;
2886
2887         memcpy(&bond_rss_conf, rss_conf, sizeof(struct rte_eth_rss_conf));
2888
2889         bond_rss_conf.rss_hf &= internals->flow_type_rss_offloads;
2890
2891         if (bond_rss_conf.rss_hf != 0)
2892                 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf = bond_rss_conf.rss_hf;
2893
2894         if (bond_rss_conf.rss_key && bond_rss_conf.rss_key_len <
2895                         sizeof(internals->rss_key)) {
2896                 if (bond_rss_conf.rss_key_len == 0)
2897                         bond_rss_conf.rss_key_len = 40;
2898                 internals->rss_key_len = bond_rss_conf.rss_key_len;
2899                 memcpy(internals->rss_key, bond_rss_conf.rss_key,
2900                                 internals->rss_key_len);
2901         }
2902
2903         for (i = 0; i < internals->slave_count; i++) {
2904                 result = rte_eth_dev_rss_hash_update(internals->slaves[i].port_id,
2905                                 &bond_rss_conf);
2906                 if (result < 0)
2907                         return result;
2908         }
2909
2910         return 0;
2911 }
2912
2913 static int
2914 bond_ethdev_rss_hash_conf_get(struct rte_eth_dev *dev,
2915                 struct rte_eth_rss_conf *rss_conf)
2916 {
2917         struct bond_dev_private *internals = dev->data->dev_private;
2918
2919         rss_conf->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
2920         rss_conf->rss_key_len = internals->rss_key_len;
2921         if (rss_conf->rss_key)
2922                 memcpy(rss_conf->rss_key, internals->rss_key, internals->rss_key_len);
2923
2924         return 0;
2925 }
2926
2927 static int
2928 bond_ethdev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2929 {
2930         struct rte_eth_dev *slave_eth_dev;
2931         struct bond_dev_private *internals = dev->data->dev_private;
2932         int ret, i;
2933
2934         rte_spinlock_lock(&internals->lock);
2935
2936         for (i = 0; i < internals->slave_count; i++) {
2937                 slave_eth_dev = &rte_eth_devices[internals->slaves[i].port_id];
2938                 if (*slave_eth_dev->dev_ops->mtu_set == NULL) {
2939                         rte_spinlock_unlock(&internals->lock);
2940                         return -ENOTSUP;
2941                 }
2942         }
2943         for (i = 0; i < internals->slave_count; i++) {
2944                 ret = rte_eth_dev_set_mtu(internals->slaves[i].port_id, mtu);
2945                 if (ret < 0) {
2946                         rte_spinlock_unlock(&internals->lock);
2947                         return ret;
2948                 }
2949         }
2950
2951         rte_spinlock_unlock(&internals->lock);
2952         return 0;
2953 }
2954
2955 static int
2956 bond_ethdev_mac_address_set(struct rte_eth_dev *dev,
2957                         struct rte_ether_addr *addr)
2958 {
2959         if (mac_address_set(dev, addr)) {
2960                 RTE_BOND_LOG(ERR, "Failed to update MAC address");
2961                 return -EINVAL;
2962         }
2963
2964         return 0;
2965 }
2966
2967 static int
2968 bond_filter_ctrl(struct rte_eth_dev *dev __rte_unused,
2969                  enum rte_filter_type type, enum rte_filter_op op, void *arg)
2970 {
2971         if (type == RTE_ETH_FILTER_GENERIC && op == RTE_ETH_FILTER_GET) {
2972                 *(const void **)arg = &bond_flow_ops;
2973                 return 0;
2974         }
2975         return -ENOTSUP;
2976 }
2977
2978 static int
2979 bond_ethdev_mac_addr_add(struct rte_eth_dev *dev,
2980                         struct rte_ether_addr *mac_addr,
2981                         __rte_unused uint32_t index, uint32_t vmdq)
2982 {
2983         struct rte_eth_dev *slave_eth_dev;
2984         struct bond_dev_private *internals = dev->data->dev_private;
2985         int ret, i;
2986
2987         rte_spinlock_lock(&internals->lock);
2988
2989         for (i = 0; i < internals->slave_count; i++) {
2990                 slave_eth_dev = &rte_eth_devices[internals->slaves[i].port_id];
2991                 if (*slave_eth_dev->dev_ops->mac_addr_add == NULL ||
2992                          *slave_eth_dev->dev_ops->mac_addr_remove == NULL) {
2993                         ret = -ENOTSUP;
2994                         goto end;
2995                 }
2996         }
2997
2998         for (i = 0; i < internals->slave_count; i++) {
2999                 ret = rte_eth_dev_mac_addr_add(internals->slaves[i].port_id,
3000                                 mac_addr, vmdq);
3001                 if (ret < 0) {
3002                         /* rollback */
3003                         for (i--; i >= 0; i--)
3004                                 rte_eth_dev_mac_addr_remove(
3005                                         internals->slaves[i].port_id, mac_addr);
3006                         goto end;
3007                 }
3008         }
3009
3010         ret = 0;
3011 end:
3012         rte_spinlock_unlock(&internals->lock);
3013         return ret;
3014 }
3015
3016 static void
3017 bond_ethdev_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3018 {
3019         struct rte_eth_dev *slave_eth_dev;
3020         struct bond_dev_private *internals = dev->data->dev_private;
3021         int i;
3022
3023         rte_spinlock_lock(&internals->lock);
3024
3025         for (i = 0; i < internals->slave_count; i++) {
3026                 slave_eth_dev = &rte_eth_devices[internals->slaves[i].port_id];
3027                 if (*slave_eth_dev->dev_ops->mac_addr_remove == NULL)
3028                         goto end;
3029         }
3030
3031         struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[index];
3032
3033         for (i = 0; i < internals->slave_count; i++)
3034                 rte_eth_dev_mac_addr_remove(internals->slaves[i].port_id,
3035                                 mac_addr);
3036
3037 end:
3038         rte_spinlock_unlock(&internals->lock);
3039 }
3040
3041 const struct eth_dev_ops default_dev_ops = {
3042         .dev_start            = bond_ethdev_start,
3043         .dev_stop             = bond_ethdev_stop,
3044         .dev_close            = bond_ethdev_close,
3045         .dev_configure        = bond_ethdev_configure,
3046         .dev_infos_get        = bond_ethdev_info,
3047         .vlan_filter_set      = bond_ethdev_vlan_filter_set,
3048         .rx_queue_setup       = bond_ethdev_rx_queue_setup,
3049         .tx_queue_setup       = bond_ethdev_tx_queue_setup,
3050         .rx_queue_release     = bond_ethdev_rx_queue_release,
3051         .tx_queue_release     = bond_ethdev_tx_queue_release,
3052         .link_update          = bond_ethdev_link_update,
3053         .stats_get            = bond_ethdev_stats_get,
3054         .stats_reset          = bond_ethdev_stats_reset,
3055         .promiscuous_enable   = bond_ethdev_promiscuous_enable,
3056         .promiscuous_disable  = bond_ethdev_promiscuous_disable,
3057         .allmulticast_enable  = bond_ethdev_allmulticast_enable,
3058         .allmulticast_disable = bond_ethdev_allmulticast_disable,
3059         .reta_update          = bond_ethdev_rss_reta_update,
3060         .reta_query           = bond_ethdev_rss_reta_query,
3061         .rss_hash_update      = bond_ethdev_rss_hash_update,
3062         .rss_hash_conf_get    = bond_ethdev_rss_hash_conf_get,
3063         .mtu_set              = bond_ethdev_mtu_set,
3064         .mac_addr_set         = bond_ethdev_mac_address_set,
3065         .mac_addr_add         = bond_ethdev_mac_addr_add,
3066         .mac_addr_remove      = bond_ethdev_mac_addr_remove,
3067         .filter_ctrl          = bond_filter_ctrl
3068 };
3069
3070 static int
3071 bond_alloc(struct rte_vdev_device *dev, uint8_t mode)
3072 {
3073         const char *name = rte_vdev_device_name(dev);
3074         uint8_t socket_id = dev->device.numa_node;
3075         struct bond_dev_private *internals = NULL;
3076         struct rte_eth_dev *eth_dev = NULL;
3077         uint32_t vlan_filter_bmp_size;
3078
3079         /* now do all data allocation - for eth_dev structure, dummy pci driver
3080          * and internal (private) data
3081          */
3082
3083         /* reserve an ethdev entry */
3084         eth_dev = rte_eth_vdev_allocate(dev, sizeof(*internals));
3085         if (eth_dev == NULL) {
3086                 RTE_BOND_LOG(ERR, "Unable to allocate rte_eth_dev");
3087                 goto err;
3088         }
3089
3090         internals = eth_dev->data->dev_private;
3091         eth_dev->data->nb_rx_queues = (uint16_t)1;
3092         eth_dev->data->nb_tx_queues = (uint16_t)1;
3093
3094         /* Allocate memory for storing MAC addresses */
3095         eth_dev->data->mac_addrs = rte_zmalloc_socket(name, RTE_ETHER_ADDR_LEN *
3096                         BOND_MAX_MAC_ADDRS, 0, socket_id);
3097         if (eth_dev->data->mac_addrs == NULL) {
3098                 RTE_BOND_LOG(ERR,
3099                              "Failed to allocate %u bytes needed to store MAC addresses",
3100                              RTE_ETHER_ADDR_LEN * BOND_MAX_MAC_ADDRS);
3101                 goto err;
3102         }
3103
3104         eth_dev->dev_ops = &default_dev_ops;
3105         eth_dev->data->dev_flags = RTE_ETH_DEV_INTR_LSC;
3106
3107         rte_spinlock_init(&internals->lock);
3108         rte_spinlock_init(&internals->lsc_lock);
3109
3110         internals->port_id = eth_dev->data->port_id;
3111         internals->mode = BONDING_MODE_INVALID;
3112         internals->current_primary_port = RTE_MAX_ETHPORTS + 1;
3113         internals->balance_xmit_policy = BALANCE_XMIT_POLICY_LAYER2;
3114         internals->burst_xmit_hash = burst_xmit_l2_hash;
3115         internals->user_defined_mac = 0;
3116
3117         internals->link_status_polling_enabled = 0;
3118
3119         internals->link_status_polling_interval_ms =
3120                 DEFAULT_POLLING_INTERVAL_10_MS;
3121         internals->link_down_delay_ms = 0;
3122         internals->link_up_delay_ms = 0;
3123
3124         internals->slave_count = 0;
3125         internals->active_slave_count = 0;
3126         internals->rx_offload_capa = 0;
3127         internals->tx_offload_capa = 0;
3128         internals->rx_queue_offload_capa = 0;
3129         internals->tx_queue_offload_capa = 0;
3130         internals->candidate_max_rx_pktlen = 0;
3131         internals->max_rx_pktlen = 0;
3132
3133         /* Initially allow to choose any offload type */
3134         internals->flow_type_rss_offloads = ETH_RSS_PROTO_MASK;
3135
3136         memset(&internals->default_rxconf, 0,
3137                sizeof(internals->default_rxconf));
3138         memset(&internals->default_txconf, 0,
3139                sizeof(internals->default_txconf));
3140
3141         memset(&internals->rx_desc_lim, 0, sizeof(internals->rx_desc_lim));
3142         memset(&internals->tx_desc_lim, 0, sizeof(internals->tx_desc_lim));
3143
3144         memset(internals->active_slaves, 0, sizeof(internals->active_slaves));
3145         memset(internals->slaves, 0, sizeof(internals->slaves));
3146
3147         TAILQ_INIT(&internals->flow_list);
3148         internals->flow_isolated_valid = 0;
3149
3150         /* Set mode 4 default configuration */
3151         bond_mode_8023ad_setup(eth_dev, NULL);
3152         if (bond_ethdev_mode_set(eth_dev, mode)) {
3153                 RTE_BOND_LOG(ERR, "Failed to set bonded device %d mode to %d",
3154                                  eth_dev->data->port_id, mode);
3155                 goto err;
3156         }
3157
3158         vlan_filter_bmp_size =
3159                 rte_bitmap_get_memory_footprint(RTE_ETHER_MAX_VLAN_ID + 1);
3160         internals->vlan_filter_bmpmem = rte_malloc(name, vlan_filter_bmp_size,
3161                                                    RTE_CACHE_LINE_SIZE);
3162         if (internals->vlan_filter_bmpmem == NULL) {
3163                 RTE_BOND_LOG(ERR,
3164                              "Failed to allocate vlan bitmap for bonded device %u",
3165                              eth_dev->data->port_id);
3166                 goto err;
3167         }
3168
3169         internals->vlan_filter_bmp = rte_bitmap_init(RTE_ETHER_MAX_VLAN_ID + 1,
3170                         internals->vlan_filter_bmpmem, vlan_filter_bmp_size);
3171         if (internals->vlan_filter_bmp == NULL) {
3172                 RTE_BOND_LOG(ERR,
3173                              "Failed to init vlan bitmap for bonded device %u",
3174                              eth_dev->data->port_id);
3175                 rte_free(internals->vlan_filter_bmpmem);
3176                 goto err;
3177         }
3178
3179         return eth_dev->data->port_id;
3180
3181 err:
3182         rte_free(internals);
3183         if (eth_dev != NULL)
3184                 eth_dev->data->dev_private = NULL;
3185         rte_eth_dev_release_port(eth_dev);
3186         return -1;
3187 }
3188
3189 static int
3190 bond_probe(struct rte_vdev_device *dev)
3191 {
3192         const char *name;
3193         struct bond_dev_private *internals;
3194         struct rte_kvargs *kvlist;
3195         uint8_t bonding_mode, socket_id/*, agg_mode*/;
3196         int  arg_count, port_id;
3197         uint8_t agg_mode;
3198         struct rte_eth_dev *eth_dev;
3199
3200         if (!dev)
3201                 return -EINVAL;
3202
3203         name = rte_vdev_device_name(dev);
3204         RTE_BOND_LOG(INFO, "Initializing pmd_bond for %s", name);
3205
3206         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
3207                 eth_dev = rte_eth_dev_attach_secondary(name);
3208                 if (!eth_dev) {
3209                         RTE_BOND_LOG(ERR, "Failed to probe %s", name);
3210                         return -1;
3211                 }
3212                 /* TODO: request info from primary to set up Rx and Tx */
3213                 eth_dev->dev_ops = &default_dev_ops;
3214                 eth_dev->device = &dev->device;
3215                 rte_eth_dev_probing_finish(eth_dev);
3216                 return 0;
3217         }
3218
3219         kvlist = rte_kvargs_parse(rte_vdev_device_args(dev),
3220                 pmd_bond_init_valid_arguments);
3221         if (kvlist == NULL)
3222                 return -1;
3223
3224         /* Parse link bonding mode */
3225         if (rte_kvargs_count(kvlist, PMD_BOND_MODE_KVARG) == 1) {
3226                 if (rte_kvargs_process(kvlist, PMD_BOND_MODE_KVARG,
3227                                 &bond_ethdev_parse_slave_mode_kvarg,
3228                                 &bonding_mode) != 0) {
3229                         RTE_BOND_LOG(ERR, "Invalid mode for bonded device %s",
3230                                         name);
3231                         goto parse_error;
3232                 }
3233         } else {
3234                 RTE_BOND_LOG(ERR, "Mode must be specified only once for bonded "
3235                                 "device %s", name);
3236                 goto parse_error;
3237         }
3238
3239         /* Parse socket id to create bonding device on */
3240         arg_count = rte_kvargs_count(kvlist, PMD_BOND_SOCKET_ID_KVARG);
3241         if (arg_count == 1) {
3242                 if (rte_kvargs_process(kvlist, PMD_BOND_SOCKET_ID_KVARG,
3243                                 &bond_ethdev_parse_socket_id_kvarg, &socket_id)
3244                                 != 0) {
3245                         RTE_BOND_LOG(ERR, "Invalid socket Id specified for "
3246                                         "bonded device %s", name);
3247                         goto parse_error;
3248                 }
3249         } else if (arg_count > 1) {
3250                 RTE_BOND_LOG(ERR, "Socket Id can be specified only once for "
3251                                 "bonded device %s", name);
3252                 goto parse_error;
3253         } else {
3254                 socket_id = rte_socket_id();
3255         }
3256
3257         dev->device.numa_node = socket_id;
3258
3259         /* Create link bonding eth device */
3260         port_id = bond_alloc(dev, bonding_mode);
3261         if (port_id < 0) {
3262                 RTE_BOND_LOG(ERR, "Failed to create socket %s in mode %u on "
3263                                 "socket %u.",   name, bonding_mode, socket_id);
3264                 goto parse_error;
3265         }
3266         internals = rte_eth_devices[port_id].data->dev_private;
3267         internals->kvlist = kvlist;
3268
3269         if (rte_kvargs_count(kvlist, PMD_BOND_AGG_MODE_KVARG) == 1) {
3270                 if (rte_kvargs_process(kvlist,
3271                                 PMD_BOND_AGG_MODE_KVARG,
3272                                 &bond_ethdev_parse_slave_agg_mode_kvarg,
3273                                 &agg_mode) != 0) {
3274                         RTE_BOND_LOG(ERR,
3275                                         "Failed to parse agg selection mode for bonded device %s",
3276                                         name);
3277                         goto parse_error;
3278                 }
3279
3280                 if (internals->mode == BONDING_MODE_8023AD)
3281                         internals->mode4.agg_selection = agg_mode;
3282         } else {
3283                 internals->mode4.agg_selection = AGG_STABLE;
3284         }
3285
3286         rte_eth_dev_probing_finish(&rte_eth_devices[port_id]);
3287         RTE_BOND_LOG(INFO, "Create bonded device %s on port %d in mode %u on "
3288                         "socket %u.",   name, port_id, bonding_mode, socket_id);
3289         return 0;
3290
3291 parse_error:
3292         rte_kvargs_free(kvlist);
3293
3294         return -1;
3295 }
3296
3297 static int
3298 bond_remove(struct rte_vdev_device *dev)
3299 {
3300         struct rte_eth_dev *eth_dev;
3301         struct bond_dev_private *internals;
3302         const char *name;
3303
3304         if (!dev)
3305                 return -EINVAL;
3306
3307         name = rte_vdev_device_name(dev);
3308         RTE_BOND_LOG(INFO, "Uninitializing pmd_bond for %s", name);
3309
3310         /* now free all data allocation - for eth_dev structure,
3311          * dummy pci driver and internal (private) data
3312          */
3313
3314         /* find an ethdev entry */
3315         eth_dev = rte_eth_dev_allocated(name);
3316         if (eth_dev == NULL)
3317                 return -ENODEV;
3318
3319         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
3320                 return rte_eth_dev_release_port(eth_dev);
3321
3322         RTE_ASSERT(eth_dev->device == &dev->device);
3323
3324         internals = eth_dev->data->dev_private;
3325         if (internals->slave_count != 0)
3326                 return -EBUSY;
3327
3328         if (eth_dev->data->dev_started == 1) {
3329                 bond_ethdev_stop(eth_dev);
3330                 bond_ethdev_close(eth_dev);
3331         }
3332
3333         eth_dev->dev_ops = NULL;
3334         eth_dev->rx_pkt_burst = NULL;
3335         eth_dev->tx_pkt_burst = NULL;
3336
3337         internals = eth_dev->data->dev_private;
3338         /* Try to release mempool used in mode6. If the bond
3339          * device is not mode6, free the NULL is not problem.
3340          */
3341         rte_mempool_free(internals->mode6.mempool);
3342         rte_bitmap_free(internals->vlan_filter_bmp);
3343         rte_free(internals->vlan_filter_bmpmem);
3344
3345         rte_eth_dev_release_port(eth_dev);
3346
3347         return 0;
3348 }
3349
3350 /* this part will resolve the slave portids after all the other pdev and vdev
3351  * have been allocated */
3352 static int
3353 bond_ethdev_configure(struct rte_eth_dev *dev)
3354 {
3355         const char *name = dev->device->name;
3356         struct bond_dev_private *internals = dev->data->dev_private;
3357         struct rte_kvargs *kvlist = internals->kvlist;
3358         int arg_count;
3359         uint16_t port_id = dev - rte_eth_devices;
3360         uint8_t agg_mode;
3361
3362         static const uint8_t default_rss_key[40] = {
3363                 0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2, 0x41, 0x67, 0x25, 0x3D,
3364                 0x43, 0xA3, 0x8F, 0xB0, 0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
3365                 0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C, 0x6A, 0x42, 0xB7, 0x3B,
3366                 0xBE, 0xAC, 0x01, 0xFA
3367         };
3368
3369         unsigned i, j;
3370
3371         /*
3372          * If RSS is enabled, fill table with default values and
3373          * set key to the the value specified in port RSS configuration.
3374          * Fall back to default RSS key if the key is not specified
3375          */
3376         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS) {
3377                 if (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key != NULL) {
3378                         internals->rss_key_len =
3379                                 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len;
3380                         memcpy(internals->rss_key,
3381                                dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key,
3382                                internals->rss_key_len);
3383                 } else {
3384                         internals->rss_key_len = sizeof(default_rss_key);
3385                         memcpy(internals->rss_key, default_rss_key,
3386                                internals->rss_key_len);
3387                 }
3388
3389                 for (i = 0; i < RTE_DIM(internals->reta_conf); i++) {
3390                         internals->reta_conf[i].mask = ~0LL;
3391                         for (j = 0; j < RTE_RETA_GROUP_SIZE; j++)
3392                                 internals->reta_conf[i].reta[j] =
3393                                                 (i * RTE_RETA_GROUP_SIZE + j) %
3394                                                 dev->data->nb_rx_queues;
3395                 }
3396         }
3397
3398         /* set the max_rx_pktlen */
3399         internals->max_rx_pktlen = internals->candidate_max_rx_pktlen;
3400
3401         /*
3402          * if no kvlist, it means that this bonded device has been created
3403          * through the bonding api.
3404          */
3405         if (!kvlist)
3406                 return 0;
3407
3408         /* Parse MAC address for bonded device */
3409         arg_count = rte_kvargs_count(kvlist, PMD_BOND_MAC_ADDR_KVARG);
3410         if (arg_count == 1) {
3411                 struct rte_ether_addr bond_mac;
3412
3413                 if (rte_kvargs_process(kvlist, PMD_BOND_MAC_ADDR_KVARG,
3414                                        &bond_ethdev_parse_bond_mac_addr_kvarg, &bond_mac) < 0) {
3415                         RTE_BOND_LOG(INFO, "Invalid mac address for bonded device %s",
3416                                      name);
3417                         return -1;
3418                 }
3419
3420                 /* Set MAC address */
3421                 if (rte_eth_bond_mac_address_set(port_id, &bond_mac) != 0) {
3422                         RTE_BOND_LOG(ERR,
3423                                      "Failed to set mac address on bonded device %s",
3424                                      name);
3425                         return -1;
3426                 }
3427         } else if (arg_count > 1) {
3428                 RTE_BOND_LOG(ERR,
3429                              "MAC address can be specified only once for bonded device %s",
3430                              name);
3431                 return -1;
3432         }
3433
3434         /* Parse/set balance mode transmit policy */
3435         arg_count = rte_kvargs_count(kvlist, PMD_BOND_XMIT_POLICY_KVARG);
3436         if (arg_count == 1) {
3437                 uint8_t xmit_policy;
3438
3439                 if (rte_kvargs_process(kvlist, PMD_BOND_XMIT_POLICY_KVARG,
3440                                        &bond_ethdev_parse_balance_xmit_policy_kvarg, &xmit_policy) !=
3441                     0) {
3442                         RTE_BOND_LOG(INFO,
3443                                      "Invalid xmit policy specified for bonded device %s",
3444                                      name);
3445                         return -1;
3446                 }
3447
3448                 /* Set balance mode transmit policy*/
3449                 if (rte_eth_bond_xmit_policy_set(port_id, xmit_policy) != 0) {
3450                         RTE_BOND_LOG(ERR,
3451                                      "Failed to set balance xmit policy on bonded device %s",
3452                                      name);
3453                         return -1;
3454                 }
3455         } else if (arg_count > 1) {
3456                 RTE_BOND_LOG(ERR,
3457                              "Transmit policy can be specified only once for bonded device %s",
3458                              name);
3459                 return -1;
3460         }
3461
3462         if (rte_kvargs_count(kvlist, PMD_BOND_AGG_MODE_KVARG) == 1) {
3463                 if (rte_kvargs_process(kvlist,
3464                                        PMD_BOND_AGG_MODE_KVARG,
3465                                        &bond_ethdev_parse_slave_agg_mode_kvarg,
3466                                        &agg_mode) != 0) {
3467                         RTE_BOND_LOG(ERR,
3468                                      "Failed to parse agg selection mode for bonded device %s",
3469                                      name);
3470                 }
3471                 if (internals->mode == BONDING_MODE_8023AD) {
3472                         int ret = rte_eth_bond_8023ad_agg_selection_set(port_id,
3473                                         agg_mode);
3474                         if (ret < 0) {
3475                                 RTE_BOND_LOG(ERR,
3476                                         "Invalid args for agg selection set for bonded device %s",
3477                                         name);
3478                                 return -1;
3479                         }
3480                 }
3481         }
3482
3483         /* Parse/add slave ports to bonded device */
3484         if (rte_kvargs_count(kvlist, PMD_BOND_SLAVE_PORT_KVARG) > 0) {
3485                 struct bond_ethdev_slave_ports slave_ports;
3486                 unsigned i;
3487
3488                 memset(&slave_ports, 0, sizeof(slave_ports));
3489
3490                 if (rte_kvargs_process(kvlist, PMD_BOND_SLAVE_PORT_KVARG,
3491                                        &bond_ethdev_parse_slave_port_kvarg, &slave_ports) != 0) {
3492                         RTE_BOND_LOG(ERR,
3493                                      "Failed to parse slave ports for bonded device %s",
3494                                      name);
3495                         return -1;
3496                 }
3497
3498                 for (i = 0; i < slave_ports.slave_count; i++) {
3499                         if (rte_eth_bond_slave_add(port_id, slave_ports.slaves[i]) != 0) {
3500                                 RTE_BOND_LOG(ERR,
3501                                              "Failed to add port %d as slave to bonded device %s",
3502                                              slave_ports.slaves[i], name);
3503                         }
3504                 }
3505
3506         } else {
3507                 RTE_BOND_LOG(INFO, "No slaves specified for bonded device %s", name);
3508                 return -1;
3509         }
3510
3511         /* Parse/set primary slave port id*/
3512         arg_count = rte_kvargs_count(kvlist, PMD_BOND_PRIMARY_SLAVE_KVARG);
3513         if (arg_count == 1) {
3514                 uint16_t primary_slave_port_id;
3515
3516                 if (rte_kvargs_process(kvlist,
3517                                        PMD_BOND_PRIMARY_SLAVE_KVARG,
3518                                        &bond_ethdev_parse_primary_slave_port_id_kvarg,
3519                                        &primary_slave_port_id) < 0) {
3520                         RTE_BOND_LOG(INFO,
3521                                      "Invalid primary slave port id specified for bonded device %s",
3522                                      name);
3523                         return -1;
3524                 }
3525
3526                 /* Set balance mode transmit policy*/
3527                 if (rte_eth_bond_primary_set(port_id, primary_slave_port_id)
3528                     != 0) {
3529                         RTE_BOND_LOG(ERR,
3530                                      "Failed to set primary slave port %d on bonded device %s",
3531                                      primary_slave_port_id, name);
3532                         return -1;
3533                 }
3534         } else if (arg_count > 1) {
3535                 RTE_BOND_LOG(INFO,
3536                              "Primary slave can be specified only once for bonded device %s",
3537                              name);
3538                 return -1;
3539         }
3540
3541         /* Parse link status monitor polling interval */
3542         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LSC_POLL_PERIOD_KVARG);
3543         if (arg_count == 1) {
3544                 uint32_t lsc_poll_interval_ms;
3545
3546                 if (rte_kvargs_process(kvlist,
3547                                        PMD_BOND_LSC_POLL_PERIOD_KVARG,
3548                                        &bond_ethdev_parse_time_ms_kvarg,
3549                                        &lsc_poll_interval_ms) < 0) {
3550                         RTE_BOND_LOG(INFO,
3551                                      "Invalid lsc polling interval value specified for bonded"
3552                                      " device %s", name);
3553                         return -1;
3554                 }
3555
3556                 if (rte_eth_bond_link_monitoring_set(port_id, lsc_poll_interval_ms)
3557                     != 0) {
3558                         RTE_BOND_LOG(ERR,
3559                                      "Failed to set lsc monitor polling interval (%u ms) on bonded device %s",
3560                                      lsc_poll_interval_ms, name);
3561                         return -1;
3562                 }
3563         } else if (arg_count > 1) {
3564                 RTE_BOND_LOG(INFO,
3565                              "LSC polling interval can be specified only once for bonded"
3566                              " device %s", name);
3567                 return -1;
3568         }
3569
3570         /* Parse link up interrupt propagation delay */
3571         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LINK_UP_PROP_DELAY_KVARG);
3572         if (arg_count == 1) {
3573                 uint32_t link_up_delay_ms;
3574
3575                 if (rte_kvargs_process(kvlist,
3576                                        PMD_BOND_LINK_UP_PROP_DELAY_KVARG,
3577                                        &bond_ethdev_parse_time_ms_kvarg,
3578                                        &link_up_delay_ms) < 0) {
3579                         RTE_BOND_LOG(INFO,
3580                                      "Invalid link up propagation delay value specified for"
3581                                      " bonded device %s", name);
3582                         return -1;
3583                 }
3584
3585                 /* Set balance mode transmit policy*/
3586                 if (rte_eth_bond_link_up_prop_delay_set(port_id, link_up_delay_ms)
3587                     != 0) {
3588                         RTE_BOND_LOG(ERR,
3589                                      "Failed to set link up propagation delay (%u ms) on bonded"
3590                                      " device %s", link_up_delay_ms, name);
3591                         return -1;
3592                 }
3593         } else if (arg_count > 1) {
3594                 RTE_BOND_LOG(INFO,
3595                              "Link up propagation delay can be specified only once for"
3596                              " bonded device %s", name);
3597                 return -1;
3598         }
3599
3600         /* Parse link down interrupt propagation delay */
3601         arg_count = rte_kvargs_count(kvlist, PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG);
3602         if (arg_count == 1) {
3603                 uint32_t link_down_delay_ms;
3604
3605                 if (rte_kvargs_process(kvlist,
3606                                        PMD_BOND_LINK_DOWN_PROP_DELAY_KVARG,
3607                                        &bond_ethdev_parse_time_ms_kvarg,
3608                                        &link_down_delay_ms) < 0) {
3609                         RTE_BOND_LOG(INFO,
3610                                      "Invalid link down propagation delay value specified for"
3611                                      " bonded device %s", name);
3612                         return -1;
3613                 }
3614
3615                 /* Set balance mode transmit policy*/
3616                 if (rte_eth_bond_link_down_prop_delay_set(port_id, link_down_delay_ms)
3617                     != 0) {
3618                         RTE_BOND_LOG(ERR,
3619                                      "Failed to set link down propagation delay (%u ms) on bonded device %s",
3620                                      link_down_delay_ms, name);
3621                         return -1;
3622                 }
3623         } else if (arg_count > 1) {
3624                 RTE_BOND_LOG(INFO,
3625                              "Link down propagation delay can be specified only once for  bonded device %s",
3626                              name);
3627                 return -1;
3628         }
3629
3630         return 0;
3631 }
3632
3633 struct rte_vdev_driver pmd_bond_drv = {
3634         .probe = bond_probe,
3635         .remove = bond_remove,
3636 };
3637
3638 RTE_PMD_REGISTER_VDEV(net_bonding, pmd_bond_drv);
3639 RTE_PMD_REGISTER_ALIAS(net_bonding, eth_bond);
3640
3641 RTE_PMD_REGISTER_PARAM_STRING(net_bonding,
3642         "slave=<ifc> "
3643         "primary=<ifc> "
3644         "mode=[0-6] "
3645         "xmit_policy=[l2 | l23 | l34] "
3646         "agg_mode=[count | stable | bandwidth] "
3647         "socket_id=<int> "
3648         "mac=<mac addr> "
3649         "lsc_poll_period_ms=<int> "
3650         "up_delay=<int> "
3651         "down_delay=<int>");
3652
3653 int bond_logtype;
3654
3655 RTE_INIT(bond_init_log)
3656 {
3657         bond_logtype = rte_log_register("pmd.net.bond");
3658         if (bond_logtype >= 0)
3659                 rte_log_set_level(bond_logtype, RTE_LOG_NOTICE);
3660 }