fa5fd3eaadf3a780dd1e9d871913c88fa310da5a
[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, num_rq, 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 entries in rx_q */
143         num_rq = kni_fifo_count(kni->rx_q);
144
145         /* Get the number of free entries in free_q */
146         num_fq = kni_fifo_free_count(kni->free_q);
147
148         /* Calculate the number of entries to dequeue in rx_q */
149         num = min(num_rq, num_fq);
150         num = min(num, (unsigned)MBUF_BURST_SZ);
151
152         /* Return if no entry in rx_q and no free entry in free_q */
153         if (num == 0)
154                 return;
155
156         /* Burst dequeue from rx_q */
157         ret = kni_fifo_get(kni->rx_q, (void **)va, num);
158         if (ret == 0)
159                 return; /* Failing should not happen */
160
161         /* Transfer received packets to netif */
162         for (i = 0; i < num; i++) {
163                 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
164                 len = kva->data_len;
165                 data_kva = kva->data - kni->mbuf_va + kni->mbuf_kva;
166
167                 skb = dev_alloc_skb(len + 2);
168                 if (!skb) {
169                         KNI_ERR("Out of mem, dropping pkts\n");
170                         /* Update statistics */
171                         kni->stats.rx_dropped++;
172                 }
173                 else {
174                         /* Align IP on 16B boundary */
175                         skb_reserve(skb, 2);
176                         memcpy(skb_put(skb, len), data_kva, len);
177                         skb->dev = dev;
178                         skb->protocol = eth_type_trans(skb, dev);
179                         skb->ip_summed = CHECKSUM_UNNECESSARY;
180
181                         /* Call netif interface */
182                         netif_rx(skb);
183
184                         /* Update statistics */
185                         kni->stats.rx_bytes += len;
186                         kni->stats.rx_packets++;
187                 }
188         }
189
190         /* Burst enqueue mbufs into free_q */
191         ret = kni_fifo_put(kni->free_q, (void **)va, num);
192         if (ret != num)
193                 /* Failing should not happen */
194                 KNI_ERR("Fail to enqueue entries into free_q\n");
195 }
196
197 /*
198  * RX: loopback with enqueue/dequeue fifos.
199  */
200 static void
201 kni_net_rx_lo_fifo(struct kni_dev *kni)
202 {
203         unsigned ret;
204         uint32_t len;
205         unsigned i, num, num_rq, num_tq, num_aq, num_fq;
206         struct rte_kni_mbuf *kva;
207         struct rte_kni_mbuf *va[MBUF_BURST_SZ];
208         void * data_kva;
209
210         struct rte_kni_mbuf *alloc_kva;
211         struct rte_kni_mbuf *alloc_va[MBUF_BURST_SZ];
212         void *alloc_data_kva;
213
214         /* Get the number of entries in rx_q */
215         num_rq = kni_fifo_count(kni->rx_q);
216
217         /* Get the number of free entrie in tx_q */
218         num_tq = kni_fifo_free_count(kni->tx_q);
219
220         /* Get the number of entries in alloc_q */
221         num_aq = kni_fifo_count(kni->alloc_q);
222
223         /* Get the number of free entries in free_q */
224         num_fq = kni_fifo_free_count(kni->free_q);
225
226         /* Calculate the number of entries to be dequeued from rx_q */
227         num = min(num_rq, num_tq);
228         num = min(num, num_aq);
229         num = min(num, num_fq);
230         num = min(num, (unsigned)MBUF_BURST_SZ);
231
232         /* Return if no entry to dequeue from rx_q */
233         if (num == 0)
234                 return;
235
236         /* Burst dequeue from rx_q */
237         ret = kni_fifo_get(kni->rx_q, (void **)va, num);
238         if (ret == 0)
239                 return; /* Failing should not happen */
240
241         /* Dequeue entries from alloc_q */
242         ret = kni_fifo_get(kni->alloc_q, (void **)alloc_va, num);
243         if (ret) {
244                 num = ret;
245                 /* Copy mbufs */
246                 for (i = 0; i < num; i++) {
247                         kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
248                         len = kva->pkt_len;
249                         data_kva = kva->data - kni->mbuf_va +
250                                                 kni->mbuf_kva;
251
252                         alloc_kva = (void *)alloc_va[i] - kni->mbuf_va +
253                                                         kni->mbuf_kva;
254                         alloc_data_kva = alloc_kva->data - kni->mbuf_va +
255                                                                 kni->mbuf_kva;
256                         memcpy(alloc_data_kva, data_kva, len);
257                         alloc_kva->pkt_len = len;
258                         alloc_kva->data_len = len;
259
260                         kni->stats.tx_bytes += len;
261                         kni->stats.rx_bytes += len;
262                 }
263
264                 /* Burst enqueue mbufs into tx_q */
265                 ret = kni_fifo_put(kni->tx_q, (void **)alloc_va, num);
266                 if (ret != num)
267                         /* Failing should not happen */
268                         KNI_ERR("Fail to enqueue mbufs into tx_q\n");
269         }
270
271         /* Burst enqueue mbufs into free_q */
272         ret = kni_fifo_put(kni->free_q, (void **)va, num);
273         if (ret != num)
274                 /* Failing should not happen */
275                 KNI_ERR("Fail to enqueue mbufs into free_q\n");
276
277         /**
278          * Update statistic, and enqueue/dequeue failure is impossible,
279          * as all queues are checked at first.
280          */
281         kni->stats.tx_packets += num;
282         kni->stats.rx_packets += num;
283 }
284
285 /*
286  * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
287  */
288 static void
289 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
290 {
291         unsigned ret;
292         uint32_t len;
293         unsigned i, num_rq, num_fq, num;
294         struct rte_kni_mbuf *kva;
295         struct rte_kni_mbuf *va[MBUF_BURST_SZ];
296         void * data_kva;
297
298         struct sk_buff *skb;
299         struct net_device *dev = kni->net_dev;
300
301         /* Get the number of entries in rx_q */
302         num_rq = kni_fifo_count(kni->rx_q);
303
304         /* Get the number of free entries in free_q */
305         num_fq = kni_fifo_free_count(kni->free_q);
306
307         /* Calculate the number of entries to dequeue from rx_q */
308         num = min(num_rq, num_fq);
309         num = min(num, (unsigned)MBUF_BURST_SZ);
310
311         /* Return if no entry to dequeue from rx_q */
312         if (num == 0)
313                 return;
314
315         /* Burst dequeue mbufs from rx_q */
316         ret = kni_fifo_get(kni->rx_q, (void **)va, num);
317         if (ret == 0)
318                 return;
319
320         /* Copy mbufs to sk buffer and then call tx interface */
321         for (i = 0; i < num; i++) {
322                 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
323                 len = kva->data_len;
324                 data_kva = kva->data - kni->mbuf_va + kni->mbuf_kva;
325
326                 skb = dev_alloc_skb(len + 2);
327                 if (skb == NULL)
328                         KNI_ERR("Out of mem, dropping pkts\n");
329                 else {
330                         /* Align IP on 16B boundary */
331                         skb_reserve(skb, 2);
332                         memcpy(skb_put(skb, len), data_kva, len);
333                         skb->dev = dev;
334                         skb->ip_summed = CHECKSUM_UNNECESSARY;
335                         dev_kfree_skb(skb);
336                 }
337
338                 /* Simulate real usage, allocate/copy skb twice */
339                 skb = dev_alloc_skb(len + 2);
340                 if (skb == NULL) {
341                         KNI_ERR("Out of mem, dropping pkts\n");
342                         kni->stats.rx_dropped++;
343                 }
344                 else {
345                         /* Align IP on 16B boundary */
346                         skb_reserve(skb, 2);
347                         memcpy(skb_put(skb, len), data_kva, len);
348                         skb->dev = dev;
349                         skb->ip_summed = CHECKSUM_UNNECESSARY;
350
351                         kni->stats.rx_bytes += len;
352                         kni->stats.rx_packets++;
353
354                         /* call tx interface */
355                         kni_net_tx(skb, dev);
356                 }
357         }
358
359         /* enqueue all the mbufs from rx_q into free_q */
360         ret = kni_fifo_put(kni->free_q, (void **)&va, num);
361         if (ret != num)
362                 /* Failing should not happen */
363                 KNI_ERR("Fail to enqueue mbufs into free_q\n");
364 }
365
366 /* rx interface */
367 void
368 kni_net_rx(struct kni_dev *kni)
369 {
370         /**
371          * It doesn't need to check if it is NULL pointer,
372          * as it has a default value
373          */
374         (*kni_net_rx_func)(kni);
375 }
376
377 /*
378  * Transmit a packet (called by the kernel)
379  */
380 #ifdef RTE_KNI_VHOST
381 static int
382 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
383 {
384         struct kni_dev *kni = netdev_priv(dev);
385
386         dev_kfree_skb(skb);
387         kni->stats.tx_dropped++;
388
389         return NETDEV_TX_OK;
390 }
391 #else
392 static int
393 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
394 {
395         int len = 0;
396         unsigned ret;
397         struct kni_dev *kni = netdev_priv(dev);
398         struct rte_kni_mbuf *pkt_kva = NULL;
399         struct rte_kni_mbuf *pkt_va = NULL;
400
401         dev->trans_start = jiffies; /* save the timestamp */
402
403         /* Check if the length of skb is less than mbuf size */
404         if (skb->len > kni->mbuf_size)
405                 goto drop;
406
407         /**
408          * Check if it has at least one free entry in tx_q and
409          * one entry in alloc_q.
410          */
411         if (kni_fifo_free_count(kni->tx_q) == 0 ||
412                         kni_fifo_count(kni->alloc_q) == 0) {
413                 /**
414                  * If no free entry in tx_q or no entry in alloc_q,
415                  * drops skb and goes out.
416                  */
417                 goto drop;
418         }
419
420         /* dequeue a mbuf from alloc_q */
421         ret = kni_fifo_get(kni->alloc_q, (void **)&pkt_va, 1);
422         if (likely(ret == 1)) {
423                 void *data_kva;
424
425                 pkt_kva = (void *)pkt_va - kni->mbuf_va + kni->mbuf_kva;
426                 data_kva = pkt_kva->data - kni->mbuf_va + 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 int
495 kni_net_change_mtu(struct net_device *dev, int new_mtu)
496 {
497         int ret;
498         struct rte_kni_request req;
499         struct kni_dev *kni = netdev_priv(dev);
500
501         KNI_DBG("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
502
503         memset(&req, 0, sizeof(req));
504         req.req_id = RTE_KNI_REQ_CHANGE_MTU;
505         req.new_mtu = new_mtu;
506         ret = kni_net_process_request(kni, &req);
507         if (ret == 0 && req.result == 0)
508                 dev->mtu = new_mtu;
509
510         return (ret == 0 ? req.result : ret);
511 }
512
513 /*
514  * Checks if the user space application provided the resp message
515  */
516 void
517 kni_net_poll_resp(struct kni_dev *kni)
518 {
519         if (kni_fifo_count(kni->resp_q))
520                 wake_up_interruptible(&kni->wq);
521 }
522
523 /*
524  * It can be called to process the request.
525  */
526 static int
527 kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
528 {
529         int ret = -1;
530         void *resp_va;
531         unsigned num;
532         int ret_val;
533
534         if (!kni || !req) {
535                 KNI_ERR("No kni instance or request\n");
536                 return -EINVAL;
537         }
538
539         mutex_lock(&kni->sync_lock);
540
541         /* Construct data */
542         memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
543         num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
544         if (num < 1) {
545                 KNI_ERR("Cannot send to req_q\n");
546                 ret = -EBUSY;
547                 goto fail;
548         }
549
550         ret_val = wait_event_interruptible_timeout(kni->wq,
551                         kni_fifo_count(kni->resp_q), 3 * HZ);
552         if (signal_pending(current) || ret_val <= 0) {
553                 ret = -ETIME;
554                 goto fail;
555         }
556         num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
557         if (num != 1 || resp_va != kni->sync_va) {
558                 /* This should never happen */
559                 KNI_ERR("No data in resp_q\n");
560                 ret = -ENODATA;
561                 goto fail;
562         }
563
564         memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
565         ret = 0;
566
567 fail:
568         mutex_unlock(&kni->sync_lock);
569         return ret;
570 }
571
572 /*
573  * Return statistics to the caller
574  */
575 static struct net_device_stats *
576 kni_net_stats(struct net_device *dev)
577 {
578         struct kni_dev *kni = netdev_priv(dev);
579         return &kni->stats;
580 }
581
582 /*
583  *  Fill the eth header
584  */
585 static int
586 kni_net_header(struct sk_buff *skb, struct net_device *dev,
587                 unsigned short type, const void *daddr,
588                 const void *saddr, unsigned int len)
589 {
590         struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
591
592         memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
593         memcpy(eth->h_dest,   daddr ? daddr : dev->dev_addr, dev->addr_len);
594         eth->h_proto = htons(type);
595
596         return (dev->hard_header_len);
597 }
598
599
600 /*
601  * Re-fill the eth header
602  */
603 static int
604 kni_net_rebuild_header(struct sk_buff *skb)
605 {
606         struct net_device *dev = skb->dev;
607         struct ethhdr *eth = (struct ethhdr *) skb->data;
608
609         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
610         memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
611
612         return 0;
613 }
614
615 /**
616  * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
617  * @netdev: network interface device structure
618  * @p: pointer to an address structure
619  *
620  * Returns 0 on success, negative on failure
621  **/
622 static int kni_net_set_mac(struct net_device *netdev, void *p)
623 {
624         struct sockaddr *addr = p;
625         if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
626                 return -EADDRNOTAVAIL;
627         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
628         return 0;
629 }
630
631 static const struct header_ops kni_net_header_ops = {
632         .create  = kni_net_header,
633         .rebuild = kni_net_rebuild_header,
634         .cache   = NULL,  /* disable caching */
635 };
636
637 static const struct net_device_ops kni_net_netdev_ops = {
638         .ndo_open = kni_net_open,
639         .ndo_stop = kni_net_release,
640         .ndo_set_config = kni_net_config,
641         .ndo_start_xmit = kni_net_tx,
642         .ndo_change_mtu = kni_net_change_mtu,
643         .ndo_do_ioctl = kni_net_ioctl,
644         .ndo_get_stats = kni_net_stats,
645         .ndo_tx_timeout = kni_net_tx_timeout,
646         .ndo_set_mac_address = kni_net_set_mac,
647 };
648
649 void
650 kni_net_init(struct net_device *dev)
651 {
652         struct kni_dev *kni = netdev_priv(dev);
653
654         KNI_DBG("kni_net_init\n");
655
656         init_waitqueue_head(&kni->wq);
657         mutex_init(&kni->sync_lock);
658
659         ether_setup(dev); /* assign some of the fields */
660         dev->netdev_ops      = &kni_net_netdev_ops;
661         dev->header_ops      = &kni_net_header_ops;
662         dev->watchdog_timeo = WD_TIMEOUT;
663 }
664
665 void
666 kni_net_config_lo_mode(char *lo_str)
667 {
668         if (!lo_str) {
669                 KNI_PRINT("loopback disabled");
670                 return;
671         }
672
673         if (!strcmp(lo_str, "lo_mode_none"))
674                 KNI_PRINT("loopback disabled");
675         else if (!strcmp(lo_str, "lo_mode_fifo")) {
676                 KNI_PRINT("loopback mode=lo_mode_fifo enabled");
677                 kni_net_rx_func = kni_net_rx_lo_fifo;
678         } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
679                 KNI_PRINT("loopback mode=lo_mode_fifo_skb enabled");
680                 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
681         } else
682                 KNI_PRINT("Incognizant parameter, loopback disabled");
683 }