5979ce5b1e0f8ca69edb0cd4d6c7094b73bf996e
[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 = (struct ether_hdr *)buf->data;
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 = (struct ether_hdr *)buf->data;
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 static int
711 bond_ethdev_configure(struct rte_eth_dev *dev __rte_unused)
712 {
713         return 0;
714 }
715
716 static void
717 bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
718 {
719         dev_info->driver_name = driver_name;
720         dev_info->max_mac_addrs = 1;
721
722         dev_info->max_rx_pktlen = (uint32_t)2048;
723
724         dev_info->max_rx_queues = (uint16_t)128;
725         dev_info->max_tx_queues = (uint16_t)512;
726
727         dev_info->min_rx_bufsize = 0;
728         dev_info->pci_dev = dev->pci_dev;
729 }
730
731 static int
732 bond_ethdev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
733                 uint16_t nb_rx_desc, unsigned int socket_id __rte_unused,
734                 const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool)
735 {
736         struct bond_rx_queue *bd_rx_q = (struct bond_rx_queue *)
737                         rte_zmalloc_socket(NULL, sizeof(struct bond_rx_queue),
738                                         0, dev->pci_dev->numa_node);
739         if (bd_rx_q == NULL)
740                 return -1;
741
742         bd_rx_q->queue_id = rx_queue_id;
743         bd_rx_q->dev_private = dev->data->dev_private;
744
745         bd_rx_q->nb_rx_desc = nb_rx_desc;
746
747         memcpy(&(bd_rx_q->rx_conf), rx_conf, sizeof(struct rte_eth_rxconf));
748         bd_rx_q->mb_pool = mb_pool;
749
750         dev->data->rx_queues[rx_queue_id] = bd_rx_q;
751
752         return 0;
753 }
754
755 static int
756 bond_ethdev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
757                 uint16_t nb_tx_desc, unsigned int socket_id __rte_unused,
758                 const struct rte_eth_txconf *tx_conf)
759 {
760         struct bond_tx_queue *bd_tx_q  = (struct bond_tx_queue *)
761                         rte_zmalloc_socket(NULL, sizeof(struct bond_tx_queue),
762                                         0, dev->pci_dev->numa_node);
763
764         if (bd_tx_q == NULL)
765                         return -1;
766
767         bd_tx_q->queue_id = tx_queue_id;
768         bd_tx_q->dev_private = dev->data->dev_private;
769
770         bd_tx_q->nb_tx_desc = nb_tx_desc;
771         memcpy(&(bd_tx_q->tx_conf), tx_conf, sizeof(bd_tx_q->tx_conf));
772
773         dev->data->tx_queues[tx_queue_id] = bd_tx_q;
774
775         return 0;
776 }
777
778 static void
779 bond_ethdev_rx_queue_release(void *queue)
780 {
781         if (queue == NULL)
782                 return;
783
784         rte_free(queue);
785 }
786
787 static void
788 bond_ethdev_tx_queue_release(void *queue)
789 {
790         if (queue == NULL)
791                 return;
792
793         rte_free(queue);
794 }
795
796 static int
797 bond_ethdev_link_update(struct rte_eth_dev *bonded_eth_dev,
798                 int wait_to_complete)
799 {
800         struct bond_dev_private *internals = bonded_eth_dev->data->dev_private;
801
802         if (!bonded_eth_dev->data->dev_started ||
803                 internals->active_slave_count == 0) {
804                 bonded_eth_dev->data->dev_link.link_status = 0;
805                 return 0;
806         } else {
807                 struct rte_eth_dev *slave_eth_dev;
808                 int i, link_up = 0;
809
810                 for (i = 0; i < internals->active_slave_count; i++) {
811                         slave_eth_dev = &rte_eth_devices[internals->active_slaves[i]];
812
813                         (*slave_eth_dev->dev_ops->link_update)(slave_eth_dev,
814                                         wait_to_complete);
815                         if (slave_eth_dev->data->dev_link.link_status == 1) {
816                                 link_up = 1;
817                                 break;
818                         }
819                 }
820
821                 bonded_eth_dev->data->dev_link.link_status = link_up;
822         }
823
824         return 0;
825 }
826
827 static void
828 bond_ethdev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
829 {
830         struct bond_dev_private *internals = dev->data->dev_private;
831         struct rte_eth_stats slave_stats;
832
833         int i;
834
835         /* clear bonded stats before populating from slaves */
836         memset(stats, 0, sizeof(*stats));
837
838         for (i = 0; i < internals->slave_count; i++) {
839                 rte_eth_stats_get(internals->slaves[i], &slave_stats);
840
841                 stats->ipackets += slave_stats.ipackets;
842                 stats->opackets += slave_stats.opackets;
843                 stats->ibytes += slave_stats.ibytes;
844                 stats->obytes += slave_stats.obytes;
845                 stats->ierrors += slave_stats.ierrors;
846                 stats->oerrors += slave_stats.oerrors;
847                 stats->imcasts += slave_stats.imcasts;
848                 stats->rx_nombuf += slave_stats.rx_nombuf;
849                 stats->fdirmatch += slave_stats.fdirmatch;
850                 stats->fdirmiss += slave_stats.fdirmiss;
851                 stats->tx_pause_xon += slave_stats.tx_pause_xon;
852                 stats->rx_pause_xon += slave_stats.rx_pause_xon;
853                 stats->tx_pause_xoff += slave_stats.tx_pause_xoff;
854                 stats->rx_pause_xoff += slave_stats.rx_pause_xoff;
855         }
856 }
857
858 static void
859 bond_ethdev_stats_reset(struct rte_eth_dev *dev)
860 {
861         struct bond_dev_private *internals = dev->data->dev_private;
862         int i;
863
864         for (i = 0; i < internals->slave_count; i++)
865                 rte_eth_stats_reset(internals->slaves[i]);
866 }
867
868 static void
869 bond_ethdev_promiscuous_enable(struct rte_eth_dev *eth_dev)
870 {
871         struct bond_dev_private *internals = eth_dev->data->dev_private;
872         int i;
873
874         internals->promiscuous_en = 1;
875
876         switch (internals->mode) {
877         /* Promiscuous mode is propagated to all slaves */
878         case BONDING_MODE_ROUND_ROBIN:
879         case BONDING_MODE_BALANCE:
880         case BONDING_MODE_BROADCAST:
881                 for (i = 0; i < internals->slave_count; i++)
882                         rte_eth_promiscuous_enable(internals->slaves[i]);
883                 break;
884         /* Promiscuous mode is propagated only to primary slave */
885         case BONDING_MODE_ACTIVE_BACKUP:
886         default:
887                 rte_eth_promiscuous_enable(internals->current_primary_port);
888
889         }
890 }
891
892 static void
893 bond_ethdev_promiscuous_disable(struct rte_eth_dev *dev)
894 {
895         struct bond_dev_private *internals = dev->data->dev_private;
896         int i;
897
898         internals->promiscuous_en = 0;
899
900         switch (internals->mode) {
901         /* Promiscuous mode is propagated to all slaves */
902         case BONDING_MODE_ROUND_ROBIN:
903         case BONDING_MODE_BALANCE:
904         case BONDING_MODE_BROADCAST:
905                 for (i = 0; i < internals->slave_count; i++)
906                         rte_eth_promiscuous_disable(internals->slaves[i]);
907                 break;
908         /* Promiscuous mode is propagated only to primary slave */
909         case BONDING_MODE_ACTIVE_BACKUP:
910         default:
911                 rte_eth_promiscuous_disable(internals->current_primary_port);
912         }
913 }
914
915 void
916 bond_ethdev_lsc_event_callback(uint8_t port_id, enum rte_eth_event_type type,
917                 void *param)
918 {
919         struct rte_eth_dev *bonded_eth_dev, *slave_eth_dev;
920         struct bond_dev_private *internals;
921         struct rte_eth_link link;
922
923         int i, valid_slave = 0, active_pos = -1;
924
925         if (type != RTE_ETH_EVENT_INTR_LSC || param == NULL)
926                 return;
927
928         bonded_eth_dev = &rte_eth_devices[*(uint8_t *)param];
929         slave_eth_dev = &rte_eth_devices[port_id];
930
931         if (valid_bonded_ethdev(bonded_eth_dev))
932                 return;
933
934         internals = bonded_eth_dev->data->dev_private;
935
936         /* If the device isn't started don't handle interrupts */
937         if (!bonded_eth_dev->data->dev_started)
938                 return;
939
940         /* verify that port_id is a valid slave of bonded port */
941         for (i = 0; i < internals->slave_count; i++) {
942                 if (internals->slaves[i] == port_id) {
943                         valid_slave = 1;
944                         break;
945                 }
946         }
947
948         if (!valid_slave)
949                 return;
950
951         /* Search for port in active port list */
952         for (i = 0; i < internals->active_slave_count; i++) {
953                 if (port_id == internals->active_slaves[i]) {
954                         active_pos = i;
955                         break;
956                 }
957         }
958
959         rte_eth_link_get_nowait(port_id, &link);
960         if (link.link_status) {
961                 if (active_pos >= 0)
962                         return;
963
964                 /* if no active slave ports then set this port to be primary port */
965                 if (internals->active_slave_count < 1) {
966                         /* If first active slave, then change link status */
967                         bonded_eth_dev->data->dev_link.link_status = 1;
968                         internals->current_primary_port = port_id;
969
970                         /* Inherit eth dev link properties from first active slave */
971                         link_properties_set(bonded_eth_dev,
972                                         &(slave_eth_dev->data->dev_link));
973                 }
974                 internals->active_slaves[internals->active_slave_count++] = port_id;
975
976                 /* If user has defined the primary port then default to using it */
977                 if (internals->user_defined_primary_port &&
978                                 internals->primary_port == port_id)
979                         bond_ethdev_primary_set(internals, port_id);
980         } else {
981                 if (active_pos < 0)
982                         return;
983
984                 /* Remove from active slave list */
985                 for (i = active_pos; i < (internals->active_slave_count - 1); i++)
986                         internals->active_slaves[i] = internals->active_slaves[i+1];
987
988                 internals->active_slave_count--;
989
990                 /* No active slaves, change link status to down and reset other
991                  * link properties */
992                 if (internals->active_slave_count < 1) {
993                         bonded_eth_dev->data->dev_link.link_status = 0;
994
995                         link_properties_reset(bonded_eth_dev);
996                 }
997
998                 /* Update primary id, take first active slave from list or if none
999                  * available set to -1 */
1000                 if (port_id == internals->current_primary_port) {
1001                         if (internals->active_slave_count > 0)
1002                                 bond_ethdev_primary_set(internals,
1003                                                 internals->active_slaves[0]);
1004                         else
1005                                 internals->current_primary_port = internals->primary_port;
1006                 }
1007         }
1008 }
1009
1010 struct eth_dev_ops default_dev_ops = {
1011                 .dev_start = bond_ethdev_start,
1012                 .dev_stop = bond_ethdev_stop,
1013                 .dev_close = bond_ethdev_close,
1014                 .dev_configure = bond_ethdev_configure,
1015                 .dev_infos_get = bond_ethdev_info,
1016                 .rx_queue_setup = bond_ethdev_rx_queue_setup,
1017                 .tx_queue_setup = bond_ethdev_tx_queue_setup,
1018                 .rx_queue_release = bond_ethdev_rx_queue_release,
1019                 .tx_queue_release = bond_ethdev_tx_queue_release,
1020                 .link_update = bond_ethdev_link_update,
1021                 .stats_get = bond_ethdev_stats_get,
1022                 .stats_reset = bond_ethdev_stats_reset,
1023                 .promiscuous_enable = bond_ethdev_promiscuous_enable,
1024                 .promiscuous_disable = bond_ethdev_promiscuous_disable
1025 };
1026
1027 static int
1028 bond_init(const char *name, const char *params)
1029 {
1030         struct rte_kvargs *kvlist;
1031         uint8_t bonding_mode, socket_id;
1032         int  arg_count, port_id;
1033
1034         RTE_LOG(INFO, EAL, "Initializing pmd_bond for %s\n", name);
1035
1036         kvlist = rte_kvargs_parse(params, pmd_bond_init_valid_arguments);
1037         if (kvlist == NULL)
1038                 return -1;
1039
1040         /* Parse link bonding mode */
1041         if (rte_kvargs_count(kvlist, PMD_BOND_MODE_KVARG) == 1) {
1042                 if (rte_kvargs_process(kvlist, PMD_BOND_MODE_KVARG,
1043                                 &bond_ethdev_parse_slave_mode_kvarg, &bonding_mode) != 0) {
1044                         RTE_LOG(ERR, EAL, "Invalid mode for bonded device %s\n", name);
1045                         return -1;
1046                 }
1047         } else {
1048                 RTE_LOG(ERR, EAL,
1049                                 "Mode must be specified only once for bonded device %s\n",
1050                                 name);
1051                 return -1;
1052         }
1053
1054         /* Parse socket id to create bonding device on */
1055         arg_count = rte_kvargs_count(kvlist, PMD_BOND_SOCKET_ID_KVARG);
1056         if (arg_count == 1) {
1057                 if (rte_kvargs_process(kvlist, PMD_BOND_SOCKET_ID_KVARG,
1058                                 &bond_ethdev_parse_socket_id_kvarg, &socket_id) != 0) {
1059                         RTE_LOG(ERR, EAL,
1060                                         "Invalid socket Id specified for bonded device %s\n",
1061                                         name);
1062                         return -1;
1063                 }
1064         } else if (arg_count > 1) {
1065                 RTE_LOG(ERR, EAL,
1066                                 "Socket Id can be specified only once for bonded device %s\n",
1067                                 name);
1068                 return -1;
1069         } else {
1070                 socket_id = rte_socket_id();
1071         }
1072
1073         /* Create link bonding eth device */
1074         port_id = rte_eth_bond_create(name, bonding_mode, socket_id);
1075         if (port_id < 0) {
1076                 RTE_LOG(ERR, EAL,
1077                                 "Failed to create socket %s in mode %u on socket %u.\n",
1078                                 name, bonding_mode, socket_id);
1079                 return -1;
1080         }
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
1086         /* Parse MAC address for bonded device */
1087         arg_count = rte_kvargs_count(kvlist, PMD_BOND_MAC_ADDR_KVARG);
1088         if (arg_count == 1) {
1089                 struct ether_addr bond_mac;
1090
1091                 if (rte_kvargs_process(kvlist, PMD_BOND_MAC_ADDR_KVARG,
1092                                 &bond_ethdev_parse_bond_mac_addr_kvarg, &bond_mac) < 0) {
1093                         RTE_LOG(INFO, EAL, "Invalid mac address for bonded device %s\n",
1094                                         name);
1095                         return -1;
1096                 }
1097
1098                 /* Set MAC address */
1099                 if (rte_eth_bond_mac_address_set(port_id, &bond_mac) != 0) {
1100                         RTE_LOG(ERR, EAL,
1101                                         "Failed to set mac address on bonded device %s\n",
1102                                         name);
1103                         return -1;
1104                 }
1105         } else if (arg_count > 1) {
1106                 RTE_LOG(ERR, EAL,
1107                                 "MAC address can be specified only once for bonded device %s\n",
1108                                 name);
1109                 return -1;
1110         }
1111
1112         /* Parse/set balance mode transmit policy */
1113         arg_count = rte_kvargs_count(kvlist, PMD_BOND_XMIT_POLICY_KVARG);
1114         if (arg_count == 1) {
1115                 uint8_t xmit_policy;
1116
1117                 if (rte_kvargs_process(kvlist, PMD_BOND_XMIT_POLICY_KVARG,
1118                                 &bond_ethdev_parse_balance_xmit_policy_kvarg, &xmit_policy) !=
1119                                                 0) {
1120                         RTE_LOG(INFO, EAL,
1121                                         "Invalid xmit policy specified for bonded device %s\n",
1122                                         name);
1123                         return -1;
1124                 }
1125
1126                 /* Set balance mode transmit policy*/
1127                 if (rte_eth_bond_xmit_policy_set(port_id, xmit_policy) != 0) {
1128                         RTE_LOG(ERR, EAL,
1129                                         "Failed to set balance xmit policy on bonded device %s\n",
1130                                         name);
1131                         return -1;
1132                 }
1133         } else if (arg_count > 1) {
1134                 RTE_LOG(ERR, EAL,
1135                                 "Transmit policy can be specified only once for bonded device %s\n",
1136                                 name);
1137                 return -1;
1138         }
1139
1140         /* Parse/add slave ports to bonded device */
1141         if (rte_kvargs_count(kvlist, PMD_BOND_SLAVE_PORT_KVARG) > 0) {
1142                 struct bond_ethdev_slave_ports slave_ports;
1143                 unsigned i;
1144
1145                 memset(&slave_ports, 0, sizeof(slave_ports));
1146
1147                 if (rte_kvargs_process(kvlist, PMD_BOND_SLAVE_PORT_KVARG,
1148                                 &bond_ethdev_parse_slave_port_kvarg, &slave_ports) != 0) {
1149                         RTE_LOG(ERR, EAL,
1150                                         "Failed to parse slave ports for bonded device %s\n",
1151                                         name);
1152                         return -1;
1153                 }
1154
1155                 for (i = 0; i < slave_ports.slave_count; i++) {
1156                         if (rte_eth_bond_slave_add(port_id, slave_ports.slaves[i]) != 0) {
1157                                 RTE_LOG(ERR, EAL,
1158                                                 "Failed to add port %d as slave to bonded device %s\n",
1159                                                 slave_ports.slaves[i], name);
1160                         }
1161                 }
1162
1163         } else {
1164                 RTE_LOG(INFO, EAL, "No slaves specified for bonded device %s\n", name);
1165                 return -1;
1166         }
1167
1168         /* Parse/set primary slave port id*/
1169         arg_count = rte_kvargs_count(kvlist, PMD_BOND_PRIMARY_SLAVE_KVARG);
1170         if (arg_count == 1) {
1171                 uint8_t primary_slave_port_id;
1172
1173                 if (rte_kvargs_process(kvlist,
1174                                 PMD_BOND_PRIMARY_SLAVE_KVARG,
1175                                 &bond_ethdev_parse_primary_slave_port_id_kvarg,
1176                                 &primary_slave_port_id) < 0) {
1177                         RTE_LOG(INFO, EAL,
1178                                         "Invalid primary slave port id specified for bonded device %s\n",
1179                                         name);
1180                         return -1;
1181                 }
1182
1183                 /* Set balance mode transmit policy*/
1184                 if (rte_eth_bond_primary_set(port_id, (uint8_t)primary_slave_port_id)
1185                                 != 0) {
1186                         RTE_LOG(ERR, EAL,
1187                                         "Failed to set primary slave port %d on bonded device %s\n",
1188                                         primary_slave_port_id, name);
1189                         return -1;
1190                 }
1191         } else if (arg_count > 1) {
1192                 RTE_LOG(INFO, EAL,
1193                                 "Primary slave can be specified only once for bonded device %s\n",
1194                                 name);
1195                 return -1;
1196         }
1197
1198         return 0;
1199 }
1200
1201 static struct rte_driver bond_drv = {
1202         .name = PMD_BOND_NAME,
1203         .type = PMD_BDEV,
1204         .init = bond_init,
1205 };
1206
1207 PMD_REGISTER_DRIVER(bond_drv);