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