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