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