kni: keep ethernet MAC address when interface goes up
[dpdk.git] / lib / librte_eal / linuxapp / kni / kni_net.c
1 /*-
2  * GPL LICENSE SUMMARY
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of version 2 of the GNU General Public License as
8  *   published by the Free Software Foundation.
9  *
10  *   This program is distributed in the hope that it will be useful, but
11  *   WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *   General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  *   The full GNU General Public License is included in this distribution
19  *   in the file called LICENSE.GPL.
20  *
21  *   Contact Information:
22  *   Intel Corporation
23  */
24
25 /*
26  * This code is inspired from the book "Linux Device Drivers" by
27  * Alessandro Rubini and Jonathan Corbet, published by O'Reilly & Associates
28  */
29
30 #include <linux/device.h>
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h> /* eth_type_trans */
35 #include <linux/skbuff.h>
36 #include <linux/kthread.h>
37 #include <linux/delay.h>
38
39 #include <exec-env/rte_kni_common.h>
40 #include <kni_fifo.h>
41 #include "kni_dev.h"
42
43 #define WD_TIMEOUT 5 /*jiffies */
44
45 #define MBUF_BURST_SZ 32
46
47 #define KNI_WAIT_RESPONSE_TIMEOUT 300 /* 3 seconds */
48
49 /* typedef for rx function */
50 typedef void (*kni_net_rx_t)(struct kni_dev *kni);
51
52 static int kni_net_tx(struct sk_buff *skb, struct net_device *dev);
53 static void kni_net_rx_normal(struct kni_dev *kni);
54 static void kni_net_rx_lo_fifo(struct kni_dev *kni);
55 static void kni_net_rx_lo_fifo_skb(struct kni_dev *kni);
56 static int kni_net_process_request(struct kni_dev *kni,
57                         struct rte_kni_request *req);
58
59 /* kni rx function pointer, with default to normal rx */
60 static kni_net_rx_t kni_net_rx_func = kni_net_rx_normal;
61
62 /*
63  * Open and close
64  */
65 static int
66 kni_net_open(struct net_device *dev)
67 {
68         int ret;
69         struct rte_kni_request req;
70         struct kni_dev *kni = netdev_priv(dev);
71
72         netif_start_queue(dev);
73
74         memset(&req, 0, sizeof(req));
75         req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
76
77         /* Setting if_up to non-zero means up */
78         req.if_up = 1;
79         ret = kni_net_process_request(kni, &req);
80
81         return (ret == 0) ? req.result : ret;
82 }
83
84 static int
85 kni_net_release(struct net_device *dev)
86 {
87         int ret;
88         struct rte_kni_request req;
89         struct kni_dev *kni = netdev_priv(dev);
90
91         netif_stop_queue(dev); /* can't transmit any more */
92
93         memset(&req, 0, sizeof(req));
94         req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
95
96         /* Setting if_up to 0 means down */
97         req.if_up = 0;
98         ret = kni_net_process_request(kni, &req);
99
100         return (ret == 0) ? req.result : ret;
101 }
102
103 /*
104  * Configuration changes (passed on by ifconfig)
105  */
106 static int
107 kni_net_config(struct net_device *dev, struct ifmap *map)
108 {
109         if (dev->flags & IFF_UP) /* can't act on a running interface */
110                 return -EBUSY;
111
112         /* ignore other fields */
113         return 0;
114 }
115
116 /*
117  * RX: normal working mode
118  */
119 static void
120 kni_net_rx_normal(struct kni_dev *kni)
121 {
122         unsigned ret;
123         uint32_t len;
124         unsigned i, num_rx, num_fq;
125         struct rte_kni_mbuf *kva;
126         struct rte_kni_mbuf *va[MBUF_BURST_SZ];
127         void * data_kva;
128
129         struct sk_buff *skb;
130         struct net_device *dev = kni->net_dev;
131
132         /* Get the number of free entries in free_q */
133         num_fq = kni_fifo_free_count(kni->free_q);
134         if (num_fq == 0) {
135                 /* No room on the free_q, bail out */
136                 return;
137         }
138
139         /* Calculate the number of entries to dequeue from rx_q */
140         num_rx = min(num_fq, (unsigned)MBUF_BURST_SZ);
141
142         /* Burst dequeue from rx_q */
143         num_rx = kni_fifo_get(kni->rx_q, (void **)va, num_rx);
144         if (num_rx == 0)
145                 return;
146
147         /* Transfer received packets to netif */
148         for (i = 0; i < num_rx; i++) {
149                 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
150                 len = kva->data_len;
151                 data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va
152                                 + kni->mbuf_kva;
153
154                 skb = dev_alloc_skb(len + 2);
155                 if (!skb) {
156                         KNI_ERR("Out of mem, dropping pkts\n");
157                         /* Update statistics */
158                         kni->stats.rx_dropped++;
159                 }
160                 else {
161                         /* Align IP on 16B boundary */
162                         skb_reserve(skb, 2);
163                         memcpy(skb_put(skb, len), data_kva, len);
164                         skb->dev = dev;
165                         skb->protocol = eth_type_trans(skb, dev);
166                         skb->ip_summed = CHECKSUM_UNNECESSARY;
167
168                         /* Call netif interface */
169                         netif_rx_ni(skb);
170
171                         /* Update statistics */
172                         kni->stats.rx_bytes += len;
173                         kni->stats.rx_packets++;
174                 }
175         }
176
177         /* Burst enqueue mbufs into free_q */
178         ret = kni_fifo_put(kni->free_q, (void **)va, num_rx);
179         if (ret != num_rx)
180                 /* Failing should not happen */
181                 KNI_ERR("Fail to enqueue entries into free_q\n");
182 }
183
184 /*
185  * RX: loopback with enqueue/dequeue fifos.
186  */
187 static void
188 kni_net_rx_lo_fifo(struct kni_dev *kni)
189 {
190         unsigned ret;
191         uint32_t len;
192         unsigned i, num, num_rq, num_tq, num_aq, num_fq;
193         struct rte_kni_mbuf *kva;
194         struct rte_kni_mbuf *va[MBUF_BURST_SZ];
195         void * data_kva;
196
197         struct rte_kni_mbuf *alloc_kva;
198         struct rte_kni_mbuf *alloc_va[MBUF_BURST_SZ];
199         void *alloc_data_kva;
200
201         /* Get the number of entries in rx_q */
202         num_rq = kni_fifo_count(kni->rx_q);
203
204         /* Get the number of free entrie in tx_q */
205         num_tq = kni_fifo_free_count(kni->tx_q);
206
207         /* Get the number of entries in alloc_q */
208         num_aq = kni_fifo_count(kni->alloc_q);
209
210         /* Get the number of free entries in free_q */
211         num_fq = kni_fifo_free_count(kni->free_q);
212
213         /* Calculate the number of entries to be dequeued from rx_q */
214         num = min(num_rq, num_tq);
215         num = min(num, num_aq);
216         num = min(num, num_fq);
217         num = min(num, (unsigned)MBUF_BURST_SZ);
218
219         /* Return if no entry to dequeue from rx_q */
220         if (num == 0)
221                 return;
222
223         /* Burst dequeue from rx_q */
224         ret = kni_fifo_get(kni->rx_q, (void **)va, num);
225         if (ret == 0)
226                 return; /* Failing should not happen */
227
228         /* Dequeue entries from alloc_q */
229         ret = kni_fifo_get(kni->alloc_q, (void **)alloc_va, num);
230         if (ret) {
231                 num = ret;
232                 /* Copy mbufs */
233                 for (i = 0; i < num; i++) {
234                         kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
235                         len = kva->pkt_len;
236                         data_kva = kva->buf_addr + kva->data_off -
237                                         kni->mbuf_va + kni->mbuf_kva;
238
239                         alloc_kva = (void *)alloc_va[i] - kni->mbuf_va +
240                                                         kni->mbuf_kva;
241                         alloc_data_kva = alloc_kva->buf_addr +
242                                         alloc_kva->data_off - kni->mbuf_va +
243                                                         kni->mbuf_kva;
244                         memcpy(alloc_data_kva, data_kva, len);
245                         alloc_kva->pkt_len = len;
246                         alloc_kva->data_len = len;
247
248                         kni->stats.tx_bytes += len;
249                         kni->stats.rx_bytes += len;
250                 }
251
252                 /* Burst enqueue mbufs into tx_q */
253                 ret = kni_fifo_put(kni->tx_q, (void **)alloc_va, num);
254                 if (ret != num)
255                         /* Failing should not happen */
256                         KNI_ERR("Fail to enqueue mbufs into tx_q\n");
257         }
258
259         /* Burst enqueue mbufs into free_q */
260         ret = kni_fifo_put(kni->free_q, (void **)va, num);
261         if (ret != num)
262                 /* Failing should not happen */
263                 KNI_ERR("Fail to enqueue mbufs into free_q\n");
264
265         /**
266          * Update statistic, and enqueue/dequeue failure is impossible,
267          * as all queues are checked at first.
268          */
269         kni->stats.tx_packets += num;
270         kni->stats.rx_packets += num;
271 }
272
273 /*
274  * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
275  */
276 static void
277 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
278 {
279         unsigned ret;
280         uint32_t len;
281         unsigned i, num_rq, num_fq, num;
282         struct rte_kni_mbuf *kva;
283         struct rte_kni_mbuf *va[MBUF_BURST_SZ];
284         void * data_kva;
285
286         struct sk_buff *skb;
287         struct net_device *dev = kni->net_dev;
288
289         /* Get the number of entries in rx_q */
290         num_rq = kni_fifo_count(kni->rx_q);
291
292         /* Get the number of free entries in free_q */
293         num_fq = kni_fifo_free_count(kni->free_q);
294
295         /* Calculate the number of entries to dequeue from rx_q */
296         num = min(num_rq, num_fq);
297         num = min(num, (unsigned)MBUF_BURST_SZ);
298
299         /* Return if no entry to dequeue from rx_q */
300         if (num == 0)
301                 return;
302
303         /* Burst dequeue mbufs from rx_q */
304         ret = kni_fifo_get(kni->rx_q, (void **)va, num);
305         if (ret == 0)
306                 return;
307
308         /* Copy mbufs to sk buffer and then call tx interface */
309         for (i = 0; i < num; i++) {
310                 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
311                 len = kva->data_len;
312                 data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va +
313                                 kni->mbuf_kva;
314
315                 skb = dev_alloc_skb(len + 2);
316                 if (skb == NULL)
317                         KNI_ERR("Out of mem, dropping pkts\n");
318                 else {
319                         /* Align IP on 16B boundary */
320                         skb_reserve(skb, 2);
321                         memcpy(skb_put(skb, len), data_kva, len);
322                         skb->dev = dev;
323                         skb->ip_summed = CHECKSUM_UNNECESSARY;
324                         dev_kfree_skb(skb);
325                 }
326
327                 /* Simulate real usage, allocate/copy skb twice */
328                 skb = dev_alloc_skb(len + 2);
329                 if (skb == NULL) {
330                         KNI_ERR("Out of mem, dropping pkts\n");
331                         kni->stats.rx_dropped++;
332                 }
333                 else {
334                         /* Align IP on 16B boundary */
335                         skb_reserve(skb, 2);
336                         memcpy(skb_put(skb, len), data_kva, len);
337                         skb->dev = dev;
338                         skb->ip_summed = CHECKSUM_UNNECESSARY;
339
340                         kni->stats.rx_bytes += len;
341                         kni->stats.rx_packets++;
342
343                         /* call tx interface */
344                         kni_net_tx(skb, dev);
345                 }
346         }
347
348         /* enqueue all the mbufs from rx_q into free_q */
349         ret = kni_fifo_put(kni->free_q, (void **)&va, num);
350         if (ret != num)
351                 /* Failing should not happen */
352                 KNI_ERR("Fail to enqueue mbufs into free_q\n");
353 }
354
355 /* rx interface */
356 void
357 kni_net_rx(struct kni_dev *kni)
358 {
359         /**
360          * It doesn't need to check if it is NULL pointer,
361          * as it has a default value
362          */
363         (*kni_net_rx_func)(kni);
364 }
365
366 /*
367  * Transmit a packet (called by the kernel)
368  */
369 #ifdef RTE_KNI_VHOST
370 static int
371 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
372 {
373         struct kni_dev *kni = netdev_priv(dev);
374
375         dev_kfree_skb(skb);
376         kni->stats.tx_dropped++;
377
378         return NETDEV_TX_OK;
379 }
380 #else
381 static int
382 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
383 {
384         int len = 0;
385         unsigned ret;
386         struct kni_dev *kni = netdev_priv(dev);
387         struct rte_kni_mbuf *pkt_kva = NULL;
388         struct rte_kni_mbuf *pkt_va = NULL;
389
390         dev->trans_start = jiffies; /* save the timestamp */
391
392         /* Check if the length of skb is less than mbuf size */
393         if (skb->len > kni->mbuf_size)
394                 goto drop;
395
396         /**
397          * Check if it has at least one free entry in tx_q and
398          * one entry in alloc_q.
399          */
400         if (kni_fifo_free_count(kni->tx_q) == 0 ||
401                         kni_fifo_count(kni->alloc_q) == 0) {
402                 /**
403                  * If no free entry in tx_q or no entry in alloc_q,
404                  * drops skb and goes out.
405                  */
406                 goto drop;
407         }
408
409         /* dequeue a mbuf from alloc_q */
410         ret = kni_fifo_get(kni->alloc_q, (void **)&pkt_va, 1);
411         if (likely(ret == 1)) {
412                 void *data_kva;
413
414                 pkt_kva = (void *)pkt_va - kni->mbuf_va + kni->mbuf_kva;
415                 data_kva = pkt_kva->buf_addr + pkt_kva->data_off - kni->mbuf_va
416                                 + kni->mbuf_kva;
417
418                 len = skb->len;
419                 memcpy(data_kva, skb->data, len);
420                 if (unlikely(len < ETH_ZLEN)) {
421                         memset(data_kva + len, 0, ETH_ZLEN - len);
422                         len = ETH_ZLEN;
423                 }
424                 pkt_kva->pkt_len = len;
425                 pkt_kva->data_len = len;
426
427                 /* enqueue mbuf into tx_q */
428                 ret = kni_fifo_put(kni->tx_q, (void **)&pkt_va, 1);
429                 if (unlikely(ret != 1)) {
430                         /* Failing should not happen */
431                         KNI_ERR("Fail to enqueue mbuf into tx_q\n");
432                         goto drop;
433                 }
434         } else {
435                 /* Failing should not happen */
436                 KNI_ERR("Fail to dequeue mbuf from alloc_q\n");
437                 goto drop;
438         }
439
440         /* Free skb and update statistics */
441         dev_kfree_skb(skb);
442         kni->stats.tx_bytes += len;
443         kni->stats.tx_packets++;
444
445         return NETDEV_TX_OK;
446
447 drop:
448         /* Free skb and update statistics */
449         dev_kfree_skb(skb);
450         kni->stats.tx_dropped++;
451
452         return NETDEV_TX_OK;
453 }
454 #endif
455
456 /*
457  * Deal with a transmit timeout.
458  */
459 static void
460 kni_net_tx_timeout (struct net_device *dev)
461 {
462         struct kni_dev *kni = netdev_priv(dev);
463
464         KNI_DBG("Transmit timeout at %ld, latency %ld\n", jiffies,
465                         jiffies - dev->trans_start);
466
467         kni->stats.tx_errors++;
468         netif_wake_queue(dev);
469         return;
470 }
471
472 /*
473  * Ioctl commands
474  */
475 static int
476 kni_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
477 {
478         KNI_DBG("kni_net_ioctl %d\n",
479                 ((struct kni_dev *)netdev_priv(dev))->group_id);
480
481         return 0;
482 }
483
484 static void
485 kni_net_set_rx_mode(struct net_device *dev)
486 {
487 }
488
489 static int
490 kni_net_change_mtu(struct net_device *dev, int new_mtu)
491 {
492         int ret;
493         struct rte_kni_request req;
494         struct kni_dev *kni = netdev_priv(dev);
495
496         KNI_DBG("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
497
498         memset(&req, 0, sizeof(req));
499         req.req_id = RTE_KNI_REQ_CHANGE_MTU;
500         req.new_mtu = new_mtu;
501         ret = kni_net_process_request(kni, &req);
502         if (ret == 0 && req.result == 0)
503                 dev->mtu = new_mtu;
504
505         return (ret == 0) ? req.result : ret;
506 }
507
508 /*
509  * Checks if the user space application provided the resp message
510  */
511 void
512 kni_net_poll_resp(struct kni_dev *kni)
513 {
514         if (kni_fifo_count(kni->resp_q))
515                 wake_up_interruptible(&kni->wq);
516 }
517
518 /*
519  * It can be called to process the request.
520  */
521 static int
522 kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
523 {
524         int ret = -1;
525         void *resp_va;
526         unsigned num;
527         int ret_val;
528
529         if (!kni || !req) {
530                 KNI_ERR("No kni instance or request\n");
531                 return -EINVAL;
532         }
533
534         mutex_lock(&kni->sync_lock);
535
536         /* Construct data */
537         memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
538         num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
539         if (num < 1) {
540                 KNI_ERR("Cannot send to req_q\n");
541                 ret = -EBUSY;
542                 goto fail;
543         }
544
545         ret_val = wait_event_interruptible_timeout(kni->wq,
546                         kni_fifo_count(kni->resp_q), 3 * HZ);
547         if (signal_pending(current) || ret_val <= 0) {
548                 ret = -ETIME;
549                 goto fail;
550         }
551         num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
552         if (num != 1 || resp_va != kni->sync_va) {
553                 /* This should never happen */
554                 KNI_ERR("No data in resp_q\n");
555                 ret = -ENODATA;
556                 goto fail;
557         }
558
559         memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
560         ret = 0;
561
562 fail:
563         mutex_unlock(&kni->sync_lock);
564         return ret;
565 }
566
567 /*
568  * Return statistics to the caller
569  */
570 static struct net_device_stats *
571 kni_net_stats(struct net_device *dev)
572 {
573         struct kni_dev *kni = netdev_priv(dev);
574         return &kni->stats;
575 }
576
577 /*
578  *  Fill the eth header
579  */
580 static int
581 kni_net_header(struct sk_buff *skb, struct net_device *dev,
582                 unsigned short type, const void *daddr,
583                 const void *saddr, unsigned int len)
584 {
585         struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
586
587         memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
588         memcpy(eth->h_dest,   daddr ? daddr : dev->dev_addr, dev->addr_len);
589         eth->h_proto = htons(type);
590
591         return dev->hard_header_len;
592 }
593
594
595 /*
596  * Re-fill the eth header
597  */
598 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0))
599 static int
600 kni_net_rebuild_header(struct sk_buff *skb)
601 {
602         struct net_device *dev = skb->dev;
603         struct ethhdr *eth = (struct ethhdr *) skb->data;
604
605         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
606         memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
607
608         return 0;
609 }
610 #endif /* < 4.1.0  */
611
612 /**
613  * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
614  * @netdev: network interface device structure
615  * @p: pointer to an address structure
616  *
617  * Returns 0 on success, negative on failure
618  **/
619 static int kni_net_set_mac(struct net_device *netdev, void *p)
620 {
621         struct sockaddr *addr = p;
622         if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
623                 return -EADDRNOTAVAIL;
624         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
625         return 0;
626 }
627
628 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
629 static int kni_net_change_carrier(struct net_device *dev, bool new_carrier)
630 {
631         if (new_carrier)
632                 netif_carrier_on(dev);
633         else
634                 netif_carrier_off(dev);
635         return 0;
636 }
637 #endif
638
639 static const struct header_ops kni_net_header_ops = {
640         .create  = kni_net_header,
641 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0))
642         .rebuild = kni_net_rebuild_header,
643 #endif /* < 4.1.0  */
644         .cache   = NULL,  /* disable caching */
645 };
646
647 static const struct net_device_ops kni_net_netdev_ops = {
648         .ndo_open = kni_net_open,
649         .ndo_stop = kni_net_release,
650         .ndo_set_config = kni_net_config,
651         .ndo_start_xmit = kni_net_tx,
652         .ndo_change_mtu = kni_net_change_mtu,
653         .ndo_do_ioctl = kni_net_ioctl,
654         .ndo_set_rx_mode = kni_net_set_rx_mode,
655         .ndo_get_stats = kni_net_stats,
656         .ndo_tx_timeout = kni_net_tx_timeout,
657         .ndo_set_mac_address = kni_net_set_mac,
658 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
659         .ndo_change_carrier = kni_net_change_carrier,
660 #endif
661 };
662
663 void
664 kni_net_init(struct net_device *dev)
665 {
666         struct kni_dev *kni = netdev_priv(dev);
667
668         KNI_DBG("kni_net_init\n");
669
670         init_waitqueue_head(&kni->wq);
671         mutex_init(&kni->sync_lock);
672
673         ether_setup(dev); /* assign some of the fields */
674         dev->netdev_ops      = &kni_net_netdev_ops;
675         dev->header_ops      = &kni_net_header_ops;
676         dev->watchdog_timeo = WD_TIMEOUT;
677 }
678
679 void
680 kni_net_config_lo_mode(char *lo_str)
681 {
682         if (!lo_str) {
683                 KNI_PRINT("loopback disabled");
684                 return;
685         }
686
687         if (!strcmp(lo_str, "lo_mode_none"))
688                 KNI_PRINT("loopback disabled");
689         else if (!strcmp(lo_str, "lo_mode_fifo")) {
690                 KNI_PRINT("loopback mode=lo_mode_fifo enabled");
691                 kni_net_rx_func = kni_net_rx_lo_fifo;
692         } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
693                 KNI_PRINT("loopback mode=lo_mode_fifo_skb enabled");
694                 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
695         } else
696                 KNI_PRINT("Incognizant parameter, loopback disabled");
697 }