bond: support link status interrupt
[dpdk.git] / lib / librte_pmd_bond / rte_eth_bond_pmd.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_mbuf.h>
35 #include <rte_malloc.h>
36 #include <rte_ethdev.h>
37 #include <rte_tcp.h>
38 #include <rte_udp.h>
39 #include <rte_ip.h>
40 #include <rte_devargs.h>
41 #include <rte_kvargs.h>
42 #include <rte_dev.h>
43
44 #include "rte_eth_bond.h"
45 #include "rte_eth_bond_private.h"
46
47 static uint16_t
48 bond_ethdev_rx_burst(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
49 {
50         struct bond_dev_private *internals;
51
52         uint16_t num_rx_slave = 0;
53         uint16_t num_rx_total = 0;
54
55         int i;
56
57         /* Cast to structure, containing bonded device's port id and queue id */
58         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)queue;
59
60         internals = bd_rx_q->dev_private;
61
62         switch (internals->mode) {
63         case BONDING_MODE_ROUND_ROBIN:
64 #ifdef RTE_MBUF_REFCNT
65         case BONDING_MODE_BROADCAST:
66 #endif
67         case BONDING_MODE_BALANCE:
68                 for (i = 0; i < internals->active_slave_count && nb_pkts; i++) {
69                         /* Offset of pointer to *bufs increases as packets are received
70                          * from other slaves */
71                         num_rx_slave = rte_eth_rx_burst(internals->active_slaves[i],
72                                         bd_rx_q->queue_id, bufs + num_rx_total, nb_pkts);
73                         if (num_rx_slave) {
74                                 num_rx_total += num_rx_slave;
75                                 nb_pkts -= num_rx_slave;
76                         }
77                 }
78                 break;
79         case BONDING_MODE_ACTIVE_BACKUP:
80                 num_rx_slave = rte_eth_rx_burst(internals->current_primary_port,
81                                 bd_rx_q->queue_id, bufs, nb_pkts);
82                 if (num_rx_slave)
83                         num_rx_total = num_rx_slave;
84                 break;
85         }
86         return num_rx_total;
87 }
88
89 static uint16_t
90 bond_ethdev_tx_round_robin(void *queue, struct rte_mbuf **bufs,
91                 uint16_t nb_pkts)
92 {
93         struct bond_dev_private *dev_private;
94         struct bond_tx_queue *bd_tx_q;
95
96         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
97         uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
98
99         uint8_t num_of_slaves;
100         uint8_t slaves[RTE_MAX_ETHPORTS];
101
102         uint16_t num_tx_total = 0;
103
104         static int slave_idx = 0;
105         int i, cs_idx = 0;
106
107         bd_tx_q = (struct bond_tx_queue *)queue;
108         dev_private = bd_tx_q->dev_private;
109
110         /* Copy slave list to protect against slave up/down changes during tx
111          * bursting */
112         num_of_slaves = dev_private->active_slave_count;
113         memcpy(slaves, dev_private->active_slaves,
114                         sizeof(dev_private->active_slaves[0]) * num_of_slaves);
115
116         if (num_of_slaves < 1)
117                 return num_tx_total;
118
119         /* Populate slaves mbuf with which packets are to be sent on it  */
120         for (i = 0; i < nb_pkts; i++) {
121                 cs_idx = (slave_idx + i) % num_of_slaves;
122                 slave_bufs[cs_idx][(slave_nb_pkts[cs_idx])++] = bufs[i];
123         }
124
125         /* increment current slave index so the next call to tx burst starts on the
126          * next slave */
127         slave_idx = ++cs_idx;
128
129         /* Send packet burst on each slave device */
130         for (i = 0; i < num_of_slaves; i++)
131                 if (slave_nb_pkts[i] > 0)
132                         num_tx_total += rte_eth_tx_burst(slaves[i],
133                                         bd_tx_q->queue_id, slave_bufs[i], slave_nb_pkts[i]);
134
135         return num_tx_total;
136 }
137
138 static uint16_t
139 bond_ethdev_tx_active_backup(void *queue,
140                 struct rte_mbuf **bufs, uint16_t nb_pkts)
141 {
142         struct bond_dev_private *internals;
143         struct bond_tx_queue *bd_tx_q;
144
145         bd_tx_q = (struct bond_tx_queue *)queue;
146         internals = bd_tx_q->dev_private;
147
148         if (internals->active_slave_count < 1)
149                 return 0;
150
151         return rte_eth_tx_burst(internals->current_primary_port, bd_tx_q->queue_id,
152                         bufs, nb_pkts);
153 }
154
155 static inline uint16_t
156 ether_hash(struct ether_hdr *eth_hdr)
157 {
158         uint16_t *word_src_addr = (uint16_t *)eth_hdr->s_addr.addr_bytes;
159         uint16_t *word_dst_addr = (uint16_t *)eth_hdr->d_addr.addr_bytes;
160
161         return (word_src_addr[0] ^ word_dst_addr[0]) ^
162                         (word_src_addr[1] ^ word_dst_addr[1]) ^
163                         (word_src_addr[2] ^ word_dst_addr[2]);
164 }
165
166 static inline uint32_t
167 ipv4_hash(struct ipv4_hdr *ipv4_hdr)
168 {
169         return (ipv4_hdr->src_addr ^ ipv4_hdr->dst_addr);
170 }
171
172 static inline uint32_t
173 ipv6_hash(struct ipv6_hdr *ipv6_hdr)
174 {
175         uint32_t *word_src_addr = (uint32_t *)&(ipv6_hdr->src_addr[0]);
176         uint32_t *word_dst_addr = (uint32_t *)&(ipv6_hdr->dst_addr[0]);
177
178         return (word_src_addr[0] ^ word_dst_addr[0]) ^
179                         (word_src_addr[1] ^ word_dst_addr[1]) ^
180                         (word_src_addr[2] ^ word_dst_addr[2]) ^
181                         (word_src_addr[3] ^ word_dst_addr[3]);
182 }
183
184 static uint32_t
185 udp_hash(struct udp_hdr *hdr)
186 {
187         return hdr->src_port ^ hdr->dst_port;
188 }
189
190 static inline uint16_t
191 xmit_slave_hash(const struct rte_mbuf *buf, uint8_t slave_count, uint8_t policy)
192 {
193         struct ether_hdr *eth_hdr;
194         struct udp_hdr *udp_hdr;
195         size_t eth_offset = 0;
196         uint32_t hash = 0;
197
198         if (slave_count == 1)
199                 return 0;
200
201         switch (policy) {
202         case BALANCE_XMIT_POLICY_LAYER2:
203                 eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
204
205                 hash = ether_hash(eth_hdr);
206                 hash ^= hash >> 8;
207                 return hash % slave_count;
208
209         case BALANCE_XMIT_POLICY_LAYER23:
210                 eth_hdr = rte_pktmbuf_mtod(buf, struct ether_hdr *);
211
212                 if (buf->ol_flags & PKT_RX_VLAN_PKT)
213                         eth_offset = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
214                 else
215                         eth_offset = sizeof(struct ether_hdr);
216
217                 if (buf->ol_flags & PKT_RX_IPV4_HDR) {
218                         struct ipv4_hdr *ipv4_hdr;
219                         ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(buf,
220                                         unsigned char *) + eth_offset);
221
222                         hash = ether_hash(eth_hdr) ^ ipv4_hash(ipv4_hdr);
223
224                 } else {
225                         struct ipv6_hdr *ipv6_hdr;
226
227                         ipv6_hdr = (struct ipv6_hdr *)(rte_pktmbuf_mtod(buf,
228                                         unsigned char *) + eth_offset);
229
230                         hash = ether_hash(eth_hdr) ^ ipv6_hash(ipv6_hdr);
231                 }
232                 break;
233
234         case BALANCE_XMIT_POLICY_LAYER34:
235                 if (buf->ol_flags & PKT_RX_VLAN_PKT)
236                         eth_offset = sizeof(struct ether_hdr) + sizeof(struct vlan_hdr);
237                 else
238                         eth_offset = sizeof(struct ether_hdr);
239
240                 if (buf->ol_flags & PKT_RX_IPV4_HDR) {
241                         struct ipv4_hdr *ipv4_hdr = (struct ipv4_hdr *)
242                                         (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset);
243
244                         if (ipv4_hdr->next_proto_id == IPPROTO_UDP) {
245                                 udp_hdr = (struct udp_hdr *)
246                                                 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset +
247                                                                 sizeof(struct ipv4_hdr));
248                                 hash = ipv4_hash(ipv4_hdr) ^ udp_hash(udp_hdr);
249                         } else {
250                                 hash = ipv4_hash(ipv4_hdr);
251                         }
252                 } else {
253                         struct ipv6_hdr *ipv6_hdr = (struct ipv6_hdr *)
254                                         (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset);
255
256                         if (ipv6_hdr->proto == IPPROTO_UDP) {
257                                 udp_hdr = (struct udp_hdr *)
258                                                 (rte_pktmbuf_mtod(buf, unsigned char *) + eth_offset +
259                                                                 sizeof(struct ipv6_hdr));
260                                 hash = ipv6_hash(ipv6_hdr) ^ udp_hash(udp_hdr);
261                         } else {
262                                 hash = ipv6_hash(ipv6_hdr);
263                         }
264                 }
265                 break;
266         }
267
268         hash ^= hash >> 16;
269         hash ^= hash >> 8;
270
271         return hash % slave_count;
272 }
273
274 static uint16_t
275 bond_ethdev_tx_balance(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts)
276 {
277         struct bond_dev_private *internals;
278         struct bond_tx_queue *bd_tx_q;
279
280         uint8_t num_of_slaves;
281         uint8_t slaves[RTE_MAX_ETHPORTS];
282
283         uint16_t num_tx_total = 0;
284
285         int i, op_slave_id;
286
287         struct rte_mbuf *slave_bufs[RTE_MAX_ETHPORTS][nb_pkts];
288         uint16_t slave_nb_pkts[RTE_MAX_ETHPORTS] = { 0 };
289
290         bd_tx_q = (struct bond_tx_queue *)queue;
291         internals = bd_tx_q->dev_private;
292
293         /* Copy slave list to protect against slave up/down changes during tx
294          * bursting */
295         num_of_slaves = internals->active_slave_count;
296         memcpy(slaves, internals->active_slaves,
297                         sizeof(internals->active_slaves[0]) * num_of_slaves);
298
299         if (num_of_slaves < 1)
300                 return num_tx_total;
301
302         /* Populate slaves mbuf with the packets which are to be sent on it  */
303         for (i = 0; i < nb_pkts; i++) {
304                 /* Select output slave using hash based on xmit policy */
305                 op_slave_id = xmit_slave_hash(bufs[i], num_of_slaves,
306                                 internals->balance_xmit_policy);
307
308                 /* Populate slave mbuf arrays with mbufs for that slave */
309                 slave_bufs[op_slave_id][slave_nb_pkts[op_slave_id]++] = bufs[i];
310         }
311
312         /* Send packet burst on each slave device */
313         for (i = 0; i < num_of_slaves; i++) {
314                 if (slave_nb_pkts[i] > 0) {
315                         num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
316                                         slave_bufs[i], slave_nb_pkts[i]);
317                 }
318         }
319
320         return num_tx_total;
321 }
322
323 #ifdef RTE_MBUF_REFCNT
324 static uint16_t
325 bond_ethdev_tx_burst_broadcast(void *queue, struct rte_mbuf **bufs,
326                 uint16_t nb_pkts)
327 {
328         struct bond_dev_private *internals;
329         struct bond_tx_queue *bd_tx_q;
330
331         uint8_t num_of_slaves;
332         uint8_t slaves[RTE_MAX_ETHPORTS];
333
334         uint16_t num_tx_total = 0;
335
336         int i;
337
338         bd_tx_q = (struct bond_tx_queue *)queue;
339         internals = bd_tx_q->dev_private;
340
341         /* Copy slave list to protect against slave up/down changes during tx
342          * bursting */
343         num_of_slaves = internals->active_slave_count;
344         memcpy(slaves, internals->active_slaves,
345                         sizeof(internals->active_slaves[0]) * num_of_slaves);
346
347         if (num_of_slaves < 1)
348                 return 0;
349
350         /* Increment reference count on mbufs */
351         for (i = 0; i < nb_pkts; i++)
352                 rte_mbuf_refcnt_update(bufs[i], num_of_slaves - 1);
353
354         /* Transmit burst on each active slave */
355         for (i = 0; i < num_of_slaves; i++)
356                 num_tx_total += rte_eth_tx_burst(slaves[i], bd_tx_q->queue_id,
357                                 bufs, nb_pkts);
358
359         return num_tx_total;
360 }
361 #endif
362
363 void
364 link_properties_set(struct rte_eth_dev *bonded_eth_dev,
365                 struct rte_eth_link *slave_dev_link)
366 {
367         struct rte_eth_link *bonded_dev_link = &bonded_eth_dev->data->dev_link;
368         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
369
370         if (slave_dev_link->link_status &&
371                 bonded_eth_dev->data->dev_started) {
372                 bonded_dev_link->link_duplex = slave_dev_link->link_duplex;
373                 bonded_dev_link->link_speed = slave_dev_link->link_speed;
374
375                 internals->link_props_set = 1;
376         }
377 }
378
379 void
380 link_properties_reset(struct rte_eth_dev *bonded_eth_dev)
381 {
382         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
383
384         memset(&(bonded_eth_dev->data->dev_link), 0,
385                         sizeof(bonded_eth_dev->data->dev_link));
386
387         internals->link_props_set = 0;
388 }
389
390 int
391 link_properties_valid(struct rte_eth_link *bonded_dev_link,
392                 struct rte_eth_link *slave_dev_link)
393 {
394         if (bonded_dev_link->link_duplex != slave_dev_link->link_duplex ||
395                 bonded_dev_link->link_speed !=  slave_dev_link->link_speed)
396                 return -1;
397
398         return 0;
399 }
400
401 int
402 mac_address_set(struct rte_eth_dev *eth_dev, struct ether_addr *new_mac_addr)
403 {
404         struct ether_addr *mac_addr;
405
406         mac_addr = eth_dev->data->mac_addrs;
407
408         if (eth_dev == NULL) {
409                 RTE_LOG(ERR, PMD, "%s: NULL pointer eth_dev specified\n", __func__);
410                 return -1;
411         }
412
413         if (new_mac_addr == NULL) {
414                 RTE_LOG(ERR, PMD, "%s: NULL pointer MAC specified\n", __func__);
415                 return -1;
416         }
417
418         /* if new MAC is different to current MAC then update */
419         if (memcmp(mac_addr, new_mac_addr, sizeof(*mac_addr)) != 0)
420                 memcpy(mac_addr, new_mac_addr, sizeof(*mac_addr));
421
422         return 0;
423 }
424
425 int
426 mac_address_slaves_update(struct rte_eth_dev *bonded_eth_dev)
427 {
428         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
429         int i;
430
431         /* Update slave devices MAC addresses */
432         if (internals->slave_count < 1)
433                 return -1;
434
435         switch (internals->mode) {
436         case BONDING_MODE_ROUND_ROBIN:
437         case BONDING_MODE_BALANCE:
438 #ifdef RTE_MBUF_REFCNT
439         case BONDING_MODE_BROADCAST:
440 #endif
441                 for (i = 0; i < internals->slave_count; i++) {
442                         if (mac_address_set(&rte_eth_devices[internals->slaves[i]],
443                                         bonded_eth_dev->data->mac_addrs)) {
444                                 RTE_LOG(ERR, PMD,
445                                                 "%s: Failed to update port Id %d MAC address\n",
446                                                 __func__, internals->slaves[i]);
447                                 return -1;
448                         }
449                 }
450                 break;
451         case BONDING_MODE_ACTIVE_BACKUP:
452         default:
453                 for (i = 0; i < internals->slave_count; i++) {
454                         if (internals->slaves[i] == internals->current_primary_port) {
455                                 if (mac_address_set(&rte_eth_devices[internals->primary_port],
456                                                 bonded_eth_dev->data->mac_addrs)) {
457                                         RTE_LOG(ERR, PMD,
458                                                         "%s: Failed to update port Id %d MAC address\n",
459                                                         __func__, internals->current_primary_port);
460                                 }
461                         } else {
462                                 struct slave_conf *conf =
463                                                 slave_config_get(internals, internals->slaves[i]);
464
465                                 if (mac_address_set(&rte_eth_devices[internals->slaves[i]],
466                                                 &conf->mac_addr)) {
467                                         RTE_LOG(ERR, PMD,
468                                                         "%s: Failed to update port Id %d MAC address\n",
469                                                         __func__, internals->slaves[i]);
470
471                                         return -1;
472                                 }
473                         }
474                 }
475         }
476
477         return 0;
478 }
479
480 int
481 bond_ethdev_mode_set(struct rte_eth_dev *eth_dev, int mode)
482 {
483         struct bond_dev_private *internals;
484
485         internals = eth_dev->data->dev_private;
486
487         switch (mode) {
488         case BONDING_MODE_ROUND_ROBIN:
489                 eth_dev->tx_pkt_burst = bond_ethdev_tx_round_robin;
490                 break;
491         case BONDING_MODE_ACTIVE_BACKUP:
492                 eth_dev->tx_pkt_burst = bond_ethdev_tx_active_backup;
493                 break;
494         case BONDING_MODE_BALANCE:
495                 eth_dev->tx_pkt_burst = bond_ethdev_tx_balance;
496                 break;
497 #ifdef RTE_MBUF_REFCNT
498         case BONDING_MODE_BROADCAST:
499                 eth_dev->tx_pkt_burst = bond_ethdev_tx_burst_broadcast;
500                 break;
501 #endif
502         default:
503                 return -1;
504         }
505
506         eth_dev->rx_pkt_burst = bond_ethdev_rx_burst;
507         internals->mode = mode;
508
509         return 0;
510 }
511
512 int
513 slave_configure(struct rte_eth_dev *bonded_eth_dev,
514                 struct rte_eth_dev *slave_eth_dev)
515 {
516         struct bond_rx_queue *bd_rx_q;
517         struct bond_tx_queue *bd_tx_q;
518
519         int q_id;
520
521         /* Stop slave */
522         rte_eth_dev_stop(slave_eth_dev->data->port_id);
523
524         /* Enable interrupts on slave device */
525         slave_eth_dev->data->dev_conf.intr_conf.lsc = 1;
526
527         if (rte_eth_dev_configure(slave_eth_dev->data->port_id,
528                         bonded_eth_dev->data->nb_rx_queues,
529                         bonded_eth_dev->data->nb_tx_queues,
530                         &(slave_eth_dev->data->dev_conf)) != 0) {
531                 RTE_LOG(ERR, PMD, "Cannot configure slave device: port=%u\n",
532                                 slave_eth_dev->data->port_id);
533                 return -1;
534         }
535
536         /* Setup Rx Queues */
537         for (q_id = 0; q_id < bonded_eth_dev->data->nb_rx_queues; q_id++) {
538                 bd_rx_q = (struct bond_rx_queue *)bonded_eth_dev->data->rx_queues[q_id];
539
540                 if (rte_eth_rx_queue_setup(slave_eth_dev->data->port_id, q_id,
541                                 bd_rx_q->nb_rx_desc,
542                                 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
543                                 &(bd_rx_q->rx_conf), bd_rx_q->mb_pool) != 0) {
544                         RTE_LOG(ERR, PMD, "rte_eth_rx_queue_setup: port=%d queue_id %d\n",
545                                         slave_eth_dev->data->port_id, q_id);
546                         return -1;
547                 }
548         }
549
550         /* Setup Tx Queues */
551         for (q_id = 0; q_id < bonded_eth_dev->data->nb_tx_queues; q_id++) {
552                 bd_tx_q = (struct bond_tx_queue *)bonded_eth_dev->data->tx_queues[q_id];
553
554                 if (rte_eth_tx_queue_setup(slave_eth_dev->data->port_id, q_id,
555                                 bd_tx_q->nb_tx_desc,
556                                 rte_eth_dev_socket_id(slave_eth_dev->data->port_id),
557                                 &bd_tx_q->tx_conf) != 0) {
558                         RTE_LOG(ERR, PMD, "rte_eth_tx_queue_setup: port=%d queue_id %d\n",
559                                         slave_eth_dev->data->port_id, q_id);
560                         return -1;
561                 }
562         }
563
564         /* Start device */
565         if (rte_eth_dev_start(slave_eth_dev->data->port_id) != 0) {
566                 RTE_LOG(ERR, PMD, "rte_eth_dev_start: port=%u\n",
567                                 slave_eth_dev->data->port_id);
568                 return -1;
569         }
570
571         return 0;
572 }
573
574 struct slave_conf *
575 slave_config_get(struct bond_dev_private *internals, uint8_t slave_port_id)
576 {
577         int i;
578
579         for (i = 0; i < internals->slave_count; i++) {
580                 if (internals->presisted_slaves_conf[i].port_id == slave_port_id)
581                         return &internals->presisted_slaves_conf[i];
582         }
583         return NULL;
584 }
585
586 void
587 slave_config_clear(struct bond_dev_private *internals,
588                 struct rte_eth_dev *slave_eth_dev)
589 {
590         int i, found = 0;
591
592         for (i = 0; i < internals->slave_count; i++) {
593                 if (internals->presisted_slaves_conf[i].port_id ==
594                                 slave_eth_dev->data->port_id) {
595                         found = 1;
596                         memset(&internals->presisted_slaves_conf[i], 0,
597                                         sizeof(internals->presisted_slaves_conf[i]));
598                 }
599                 if (found && i < (internals->slave_count - 1)) {
600                         memcpy(&internals->presisted_slaves_conf[i],
601                                         &internals->presisted_slaves_conf[i+1],
602                                         sizeof(internals->presisted_slaves_conf[i]));
603                 }
604         }
605 }
606
607 void
608 slave_config_store(struct bond_dev_private *internals,
609                 struct rte_eth_dev *slave_eth_dev)
610 {
611         struct slave_conf *presisted_slave_conf =
612                         &internals->presisted_slaves_conf[internals->slave_count];
613
614         presisted_slave_conf->port_id = slave_eth_dev->data->port_id;
615
616         memcpy(&(presisted_slave_conf->mac_addr), slave_eth_dev->data->mac_addrs,
617                         sizeof(struct ether_addr));
618 }
619
620 void
621 bond_ethdev_primary_set(struct bond_dev_private *internals,
622                 uint8_t slave_port_id)
623 {
624         int i;
625
626         if (internals->active_slave_count < 1)
627                 internals->current_primary_port = slave_port_id;
628         else
629                 /* Search bonded device slave ports for new proposed primary port */
630                 for (i = 0; i < internals->active_slave_count; i++) {
631                         if (internals->active_slaves[i] == slave_port_id)
632                                 internals->current_primary_port = slave_port_id;
633                 }
634 }
635
636 static void
637 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev);
638
639 static int
640 bond_ethdev_start(struct rte_eth_dev *eth_dev)
641 {
642         struct bond_dev_private *internals;
643         int i;
644
645         /* slave eth dev will be started by bonded device */
646         if (valid_bonded_ethdev(eth_dev)) {
647                 RTE_LOG(ERR, PMD,
648                                 "%s: user tried to explicitly start a slave eth_dev (%d) of the bonded eth_dev\n",
649                                 __func__, eth_dev->data->port_id);
650                 return -1;
651         }
652
653         eth_dev->data->dev_link.link_status = 1;
654         eth_dev->data->dev_started = 1;
655
656         internals = eth_dev->data->dev_private;
657
658         if (internals->slave_count == 0) {
659                 RTE_LOG(ERR, PMD,
660                                 "%s: Cannot start port since there are no slave devices\n",
661                                 __func__);
662                 return -1;
663         }
664
665         if (internals->user_defined_mac == 0) {
666                 struct slave_conf *conf = slave_config_get(internals,
667                                 internals->primary_port);
668
669                 if (mac_address_set(eth_dev, &(conf->mac_addr)) != 0) {
670                         RTE_LOG(ERR, PMD,
671                                         "bonded port (%d) failed to update mac address",
672                                         eth_dev->data->port_id);
673                         return -1;
674                 }
675         }
676
677         /* Update all slave devices MACs*/
678         if (mac_address_slaves_update(eth_dev) != 0)
679                 return -1;
680
681         /* If bonded device is configure in promiscuous mode then re-apply config */
682         if (internals->promiscuous_en)
683                 bond_ethdev_promiscuous_enable(eth_dev);
684
685         /* Reconfigure each slave device if starting bonded device */
686         for (i = 0; i < internals->slave_count; i++) {
687                 if (slave_configure(eth_dev, &(rte_eth_devices[internals->slaves[i]]))
688                                 != 0) {
689                         RTE_LOG(ERR, PMD, "bonded port "
690                                         "(%d) failed to reconfigure slave device (%d)\n)",
691                                         eth_dev->data->port_id, internals->slaves[i]);
692                         return -1;
693                 }
694         }
695
696         if (internals->user_defined_primary_port)
697                 bond_ethdev_primary_set(internals, internals->primary_port);
698
699         return 0;
700 }
701
702 static void
703 bond_ethdev_stop(struct rte_eth_dev *eth_dev)
704 {
705         struct bond_dev_private *internals = eth_dev->data->dev_private;
706
707         internals->active_slave_count = 0;
708
709         eth_dev->data->dev_link.link_status = 0;
710         eth_dev->data->dev_started = 0;
711 }
712
713 static void
714 bond_ethdev_close(struct rte_eth_dev *dev __rte_unused)
715 {
716 }
717
718 /* forward declaration */
719 static int bond_ethdev_configure(struct rte_eth_dev *dev);
720
721 static void
722 bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
723 {
724         dev_info->driver_name = driver_name;
725         dev_info->max_mac_addrs = 1;
726
727         dev_info->max_rx_pktlen = (uint32_t)2048;
728
729         dev_info->max_rx_queues = (uint16_t)128;
730         dev_info->max_tx_queues = (uint16_t)512;
731
732         dev_info->min_rx_bufsize = 0;
733         dev_info->pci_dev = dev->pci_dev;
734 }
735
736 static int
737 bond_ethdev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
738                 uint16_t nb_rx_desc, unsigned int socket_id __rte_unused,
739                 const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool)
740 {
741         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)
742                         rte_zmalloc_socket(NULL, sizeof(struct bond_rx_queue),
743                                         0, dev->pci_dev->numa_node);
744         if (bd_rx_q == NULL)
745                 return -1;
746
747         bd_rx_q->queue_id = rx_queue_id;
748         bd_rx_q->dev_private = dev->data->dev_private;
749
750         bd_rx_q->nb_rx_desc = nb_rx_desc;
751
752         memcpy(&(bd_rx_q->rx_conf), rx_conf, sizeof(struct rte_eth_rxconf));
753         bd_rx_q->mb_pool = mb_pool;
754
755         dev->data->rx_queues[rx_queue_id] = bd_rx_q;
756
757         return 0;
758 }
759
760 static int
761 bond_ethdev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
762                 uint16_t nb_tx_desc, unsigned int socket_id __rte_unused,
763                 const struct rte_eth_txconf *tx_conf)
764 {
765         struct bond_tx_queue *bd_tx_q  = (struct bond_tx_queue *)
766                         rte_zmalloc_socket(NULL, sizeof(struct bond_tx_queue),
767                                         0, dev->pci_dev->numa_node);
768
769         if (bd_tx_q == NULL)
770                         return -1;
771
772         bd_tx_q->queue_id = tx_queue_id;
773         bd_tx_q->dev_private = dev->data->dev_private;
774
775         bd_tx_q->nb_tx_desc = nb_tx_desc;
776         memcpy(&(bd_tx_q->tx_conf), tx_conf, sizeof(bd_tx_q->tx_conf));
777
778         dev->data->tx_queues[tx_queue_id] = bd_tx_q;
779
780         return 0;
781 }
782
783 static void
784 bond_ethdev_rx_queue_release(void *queue)
785 {
786         if (queue == NULL)
787                 return;
788
789         rte_free(queue);
790 }
791
792 static void
793 bond_ethdev_tx_queue_release(void *queue)
794 {
795         if (queue == NULL)
796                 return;
797
798         rte_free(queue);
799 }
800
801 static int
802 bond_ethdev_link_update(struct rte_eth_dev *bonded_eth_dev,
803                 int wait_to_complete)
804 {
805         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
806
807         if (!bonded_eth_dev->data->dev_started ||
808                 internals->active_slave_count == 0) {
809                 bonded_eth_dev->data->dev_link.link_status = 0;
810                 return 0;
811         } else {
812                 struct rte_eth_dev *slave_eth_dev;
813                 int i, link_up = 0;
814
815                 for (i = 0; i < internals->active_slave_count; i++) {
816                         slave_eth_dev = &rte_eth_devices[internals->active_slaves[i]];
817
818                         (*slave_eth_dev->dev_ops->link_update)(slave_eth_dev,
819                                         wait_to_complete);
820                         if (slave_eth_dev->data->dev_link.link_status == 1) {
821                                 link_up = 1;
822                                 break;
823                         }
824                 }
825
826                 bonded_eth_dev->data->dev_link.link_status = link_up;
827         }
828
829         return 0;
830 }
831
832 static void
833 bond_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
834 {
835         struct bond_dev_private *internals = dev->data->dev_private;
836         struct rte_eth_stats slave_stats;
837
838         int i;
839
840         /* clear bonded stats before populating from slaves */
841         memset(stats, 0, sizeof(*stats));
842
843         for (i = 0; i < internals->slave_count; i++) {
844                 rte_eth_stats_get(internals->slaves[i], &slave_stats);
845
846                 stats->ipackets += slave_stats.ipackets;
847                 stats->opackets += slave_stats.opackets;
848                 stats->ibytes += slave_stats.ibytes;
849                 stats->obytes += slave_stats.obytes;
850                 stats->ierrors += slave_stats.ierrors;
851                 stats->oerrors += slave_stats.oerrors;
852                 stats->imcasts += slave_stats.imcasts;
853                 stats->rx_nombuf += slave_stats.rx_nombuf;
854                 stats->fdirmatch += slave_stats.fdirmatch;
855                 stats->fdirmiss += slave_stats.fdirmiss;
856                 stats->tx_pause_xon += slave_stats.tx_pause_xon;
857                 stats->rx_pause_xon += slave_stats.rx_pause_xon;
858                 stats->tx_pause_xoff += slave_stats.tx_pause_xoff;
859                 stats->rx_pause_xoff += slave_stats.rx_pause_xoff;
860         }
861 }
862
863 static void
864 bond_ethdev_stats_reset(struct rte_eth_dev *dev)
865 {
866         struct bond_dev_private *internals = dev->data->dev_private;
867         int i;
868
869         for (i = 0; i < internals->slave_count; i++)
870                 rte_eth_stats_reset(internals->slaves[i]);
871 }
872
873 static void
874 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev)
875 {
876         struct bond_dev_private *internals = eth_dev->data->dev_private;
877         int i;
878
879         internals->promiscuous_en = 1;
880
881         switch (internals->mode) {
882         /* Promiscuous mode is propagated to all slaves */
883         case BONDING_MODE_ROUND_ROBIN:
884         case BONDING_MODE_BALANCE:
885 #ifdef RTE_MBUF_REFCNT
886         case BONDING_MODE_BROADCAST:
887 #endif
888                 for (i = 0; i < internals->slave_count; i++)
889                         rte_eth_promiscuous_enable(internals->slaves[i]);
890                 break;
891         /* Promiscuous mode is propagated only to primary slave */
892         case BONDING_MODE_ACTIVE_BACKUP:
893         default:
894                 rte_eth_promiscuous_enable(internals->current_primary_port);
895
896         }
897 }
898
899 static void
900 bond_ethdev_promiscuous_disable(struct rte_eth_dev *dev)
901 {
902         struct bond_dev_private *internals = dev->data->dev_private;
903         int i;
904
905         internals->promiscuous_en = 0;
906
907         switch (internals->mode) {
908         /* Promiscuous mode is propagated to all slaves */
909         case BONDING_MODE_ROUND_ROBIN:
910         case BONDING_MODE_BALANCE:
911 #ifdef RTE_MBUF_REFCNT
912         case BONDING_MODE_BROADCAST:
913 #endif
914                 for (i = 0; i < internals->slave_count; i++)
915                         rte_eth_promiscuous_disable(internals->slaves[i]);
916                 break;
917         /* Promiscuous mode is propagated only to primary slave */
918         case BONDING_MODE_ACTIVE_BACKUP:
919         default:
920                 rte_eth_promiscuous_disable(internals->current_primary_port);
921         }
922 }
923
924 void
925 bond_ethdev_lsc_event_callback(uint8_t port_id, enum rte_eth_event_type type,
926                 void *param)
927 {
928         struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
929         struct bond_dev_private *internals;
930         struct rte_eth_link link;
931
932         int i, valid_slave = 0, active_pos = -1;
933         uint8_t lsc_flag = 0;
934
935         if (type != RTE_ETH_EVENT_INTR_LSC || param == NULL)
936                 return;
937
938         bonded_eth_dev = &rte_eth_devices[*(uint8_t *)param];
939         slave_eth_dev = &rte_eth_devices[port_id];
940
941         if (valid_bonded_ethdev(bonded_eth_dev))
942                 return;
943
944         internals = bonded_eth_dev->data->dev_private;
945
946         /* If the device isn't started don't handle interrupts */
947         if (!bonded_eth_dev->data->dev_started)
948                 return;
949
950         /* verify that port_id is a valid slave of bonded port */
951         for (i = 0; i < internals->slave_count; i++) {
952                 if (internals->slaves[i] == port_id) {
953                         valid_slave = 1;
954                         break;
955                 }
956         }
957
958         if (!valid_slave)
959                 return;
960
961         /* Search for port in active port list */
962         for (i = 0; i < internals->active_slave_count; i++) {
963                 if (port_id == internals->active_slaves[i]) {
964                         active_pos = i;
965                         break;
966                 }
967         }
968
969         rte_eth_link_get_nowait(port_id, &link);
970         if (link.link_status) {
971                 if (active_pos >= 0)
972                         return;
973
974                 /* if no active slave ports then set this port to be primary port */
975                 if (internals->active_slave_count < 1) {
976                         /* If first active slave, then change link status */
977                         bonded_eth_dev->data->dev_link.link_status = 1;
978                         internals->current_primary_port = port_id;
979                         lsc_flag = 1;
980
981                         /* Inherit eth dev link properties from first active slave */
982                         link_properties_set(bonded_eth_dev,
983                                         &(slave_eth_dev->data->dev_link));
984                 }
985                 internals->active_slaves[internals->active_slave_count++] = port_id;
986
987                 /* If user has defined the primary port then default to using it */
988                 if (internals->user_defined_primary_port &&
989                                 internals->primary_port == port_id)
990                         bond_ethdev_primary_set(internals, port_id);
991         } else {
992                 if (active_pos < 0)
993                         return;
994
995                 /* Remove from active slave list */
996                 for (i = active_pos; i < (internals->active_slave_count - 1); i++)
997                         internals->active_slaves[i] = internals->active_slaves[i+1];
998
999                 internals->active_slave_count--;
1000
1001                 /* No active slaves, change link status to down and reset other
1002                  * link properties */
1003                 if (internals->active_slave_count < 1) {
1004                         lsc_flag = 1;
1005                         bonded_eth_dev->data->dev_link.link_status = 0;
1006
1007                         link_properties_reset(bonded_eth_dev);
1008                 }
1009
1010                 /* Update primary id, take first active slave from list or if none
1011                  * available set to -1 */
1012                 if (port_id == internals->current_primary_port) {
1013                         if (internals->active_slave_count > 0)
1014                                 bond_ethdev_primary_set(internals,
1015                                                 internals->active_slaves[0]);
1016                         else
1017                                 internals->current_primary_port = internals->primary_port;
1018                 }
1019         }
1020
1021         if (lsc_flag)
1022                 _rte_eth_dev_callback_process(bonded_eth_dev, RTE_ETH_EVENT_INTR_LSC);
1023 }
1024
1025 struct eth_dev_ops default_dev_ops = {
1026                 .dev_start = bond_ethdev_start,
1027                 .dev_stop = bond_ethdev_stop,
1028                 .dev_close = bond_ethdev_close,
1029                 .dev_configure = bond_ethdev_configure,
1030                 .dev_infos_get = bond_ethdev_info,
1031                 .rx_queue_setup = bond_ethdev_rx_queue_setup,
1032                 .tx_queue_setup = bond_ethdev_tx_queue_setup,
1033                 .rx_queue_release = bond_ethdev_rx_queue_release,
1034                 .tx_queue_release = bond_ethdev_tx_queue_release,
1035                 .link_update = bond_ethdev_link_update,
1036                 .stats_get = bond_ethdev_stats_get,
1037                 .stats_reset = bond_ethdev_stats_reset,
1038                 .promiscuous_enable = bond_ethdev_promiscuous_enable,
1039                 .promiscuous_disable = bond_ethdev_promiscuous_disable
1040 };
1041
1042 static int
1043 bond_init(const char *name, const char *params)
1044 {
1045         struct bond_dev_private *internals;
1046         struct rte_kvargs *kvlist;
1047         uint8_t bonding_mode, socket_id;
1048         int  arg_count, port_id;
1049
1050         RTE_LOG(INFO, EAL, "Initializing pmd_bond for %s\n", name);
1051
1052         kvlist = rte_kvargs_parse(params, pmd_bond_init_valid_arguments);
1053         if (kvlist == NULL)
1054                 return -1;
1055
1056         /* Parse link bonding mode */
1057         if (rte_kvargs_count(kvlist, PMD_BOND_MODE_KVARG) == 1) {
1058                 if (rte_kvargs_process(kvlist, PMD_BOND_MODE_KVARG,
1059                                 &bond_ethdev_parse_slave_mode_kvarg, &bonding_mode) != 0) {
1060                         RTE_LOG(ERR, EAL, "Invalid mode for bonded device %s\n", name);
1061                         return -1;
1062                 }
1063         } else {
1064                 RTE_LOG(ERR, EAL,
1065                                 "Mode must be specified only once for bonded device %s\n",
1066                                 name);
1067                 return -1;
1068         }
1069
1070         /* Parse socket id to create bonding device on */
1071         arg_count = rte_kvargs_count(kvlist, PMD_BOND_SOCKET_ID_KVARG);
1072         if (arg_count == 1) {
1073                 if (rte_kvargs_process(kvlist, PMD_BOND_SOCKET_ID_KVARG,
1074                                 &bond_ethdev_parse_socket_id_kvarg, &socket_id) != 0) {
1075                         RTE_LOG(ERR, EAL,
1076                                         "Invalid socket Id specified for bonded device %s\n",
1077                                         name);
1078                         return -1;
1079                 }
1080         } else if (arg_count > 1) {
1081                 RTE_LOG(ERR, EAL,
1082                                 "Socket Id can be specified only once for bonded device %s\n",
1083                                 name);
1084                 return -1;
1085         } else {
1086                 socket_id = rte_socket_id();
1087         }
1088
1089         /* Create link bonding eth device */
1090         port_id = rte_eth_bond_create(name, bonding_mode, socket_id);
1091         if (port_id < 0) {
1092                 RTE_LOG(ERR, EAL,
1093                                 "Failed to create socket %s in mode %u on socket %u.\n",
1094                                 name, bonding_mode, socket_id);
1095                 return -1;
1096         }
1097         internals = rte_eth_devices[port_id].data->dev_private;
1098         internals->kvlist = kvlist;
1099
1100         RTE_LOG(INFO, EAL,
1101                         "Create bonded device %s on port %d in mode %u on socket %u.\n",
1102                         name, port_id, bonding_mode, socket_id);
1103         return 0;
1104 }
1105
1106 /* this part will resolve the slave portids after all the other pdev and vdev
1107  * have been allocated */
1108 static int
1109 bond_ethdev_configure(struct rte_eth_dev *dev)
1110 {
1111         char *name = dev->data->name;
1112         struct bond_dev_private *internals = dev->data->dev_private;
1113         struct rte_kvargs *kvlist = internals->kvlist;
1114         int arg_count, port_id = dev - rte_eth_devices;
1115
1116         /*
1117          * if no kvlist, it means that this bonded device has been created
1118          * through the bonding api.
1119          */
1120         if (!kvlist)
1121                 return 0;
1122
1123         /* Parse MAC address for bonded device */
1124         arg_count = rte_kvargs_count(kvlist, PMD_BOND_MAC_ADDR_KVARG);
1125         if (arg_count == 1) {
1126                 struct ether_addr bond_mac;
1127
1128                 if (rte_kvargs_process(kvlist, PMD_BOND_MAC_ADDR_KVARG,
1129                                 &bond_ethdev_parse_bond_mac_addr_kvarg, &bond_mac) < 0) {
1130                         RTE_LOG(INFO, EAL, "Invalid mac address for bonded device %s\n",
1131                                         name);
1132                         return -1;
1133                 }
1134
1135                 /* Set MAC address */
1136                 if (rte_eth_bond_mac_address_set(port_id, &bond_mac) != 0) {
1137                         RTE_LOG(ERR, EAL,
1138                                         "Failed to set mac address on bonded device %s\n",
1139                                         name);
1140                         return -1;
1141                 }
1142         } else if (arg_count > 1) {
1143                 RTE_LOG(ERR, EAL,
1144                                 "MAC address can be specified only once for bonded device %s\n",
1145                                 name);
1146                 return -1;
1147         }
1148
1149         /* Parse/set balance mode transmit policy */
1150         arg_count = rte_kvargs_count(kvlist, PMD_BOND_XMIT_POLICY_KVARG);
1151         if (arg_count == 1) {
1152                 uint8_t xmit_policy;
1153
1154                 if (rte_kvargs_process(kvlist, PMD_BOND_XMIT_POLICY_KVARG,
1155                                 &bond_ethdev_parse_balance_xmit_policy_kvarg, &xmit_policy) !=
1156                                                 0) {
1157                         RTE_LOG(INFO, EAL,
1158                                         "Invalid xmit policy specified for bonded device %s\n",
1159                                         name);
1160                         return -1;
1161                 }
1162
1163                 /* Set balance mode transmit policy*/
1164                 if (rte_eth_bond_xmit_policy_set(port_id, xmit_policy) != 0) {
1165                         RTE_LOG(ERR, EAL,
1166                                         "Failed to set balance xmit policy on bonded device %s\n",
1167                                         name);
1168                         return -1;
1169                 }
1170         } else if (arg_count > 1) {
1171                 RTE_LOG(ERR, EAL,
1172                                 "Transmit policy can be specified only once for bonded device %s\n",
1173                                 name);
1174                 return -1;
1175         }
1176
1177         /* Parse/add slave ports to bonded device */
1178         if (rte_kvargs_count(kvlist, PMD_BOND_SLAVE_PORT_KVARG) > 0) {
1179                 struct bond_ethdev_slave_ports slave_ports;
1180                 unsigned i;
1181
1182                 memset(&slave_ports, 0, sizeof(slave_ports));
1183
1184                 if (rte_kvargs_process(kvlist, PMD_BOND_SLAVE_PORT_KVARG,
1185                                 &bond_ethdev_parse_slave_port_kvarg, &slave_ports) != 0) {
1186                         RTE_LOG(ERR, EAL,
1187                                         "Failed to parse slave ports for bonded device %s\n",
1188                                         name);
1189                         return -1;
1190                 }
1191
1192                 for (i = 0; i < slave_ports.slave_count; i++) {
1193                         if (rte_eth_bond_slave_add(port_id, slave_ports.slaves[i]) != 0) {
1194                                 RTE_LOG(ERR, EAL,
1195                                                 "Failed to add port %d as slave to bonded device %s\n",
1196                                                 slave_ports.slaves[i], name);
1197                         }
1198                 }
1199
1200         } else {
1201                 RTE_LOG(INFO, EAL, "No slaves specified for bonded device %s\n", name);
1202                 return -1;
1203         }
1204
1205         /* Parse/set primary slave port id*/
1206         arg_count = rte_kvargs_count(kvlist, PMD_BOND_PRIMARY_SLAVE_KVARG);
1207         if (arg_count == 1) {
1208                 uint8_t primary_slave_port_id;
1209
1210                 if (rte_kvargs_process(kvlist,
1211                                 PMD_BOND_PRIMARY_SLAVE_KVARG,
1212                                 &bond_ethdev_parse_primary_slave_port_id_kvarg,
1213                                 &primary_slave_port_id) < 0) {
1214                         RTE_LOG(INFO, EAL,
1215                                         "Invalid primary slave port id specified for bonded device %s\n",
1216                                         name);
1217                         return -1;
1218                 }
1219
1220                 /* Set balance mode transmit policy*/
1221                 if (rte_eth_bond_primary_set(port_id, (uint8_t)primary_slave_port_id)
1222                                 != 0) {
1223                         RTE_LOG(ERR, EAL,
1224                                         "Failed to set primary slave port %d on bonded device %s\n",
1225                                         primary_slave_port_id, name);
1226                         return -1;
1227                 }
1228         } else if (arg_count > 1) {
1229                 RTE_LOG(INFO, EAL,
1230                                 "Primary slave can be specified only once for bonded device %s\n",
1231                                 name);
1232                 return -1;
1233         }
1234
1235         return 0;
1236 }
1237
1238 static struct rte_driver bond_drv = {
1239         .name = "eth_bond",
1240         .type = PMD_VDEV,
1241         .init = bond_init,
1242 };
1243
1244 PMD_REGISTER_DRIVER(bond_drv);