4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
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.
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.
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.
21 * Contact Information:
26 * This code is inspired from the book "Linux Device Drivers" by
27 * Alessandro Rubini and Jonathan Corbet, published by O'Reilly & Associates
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>
39 #include <exec-env/rte_kni_common.h>
45 #define WD_TIMEOUT 5 /*jiffies */
47 #define MBUF_BURST_SZ 32
49 #define KNI_WAIT_RESPONSE_TIMEOUT 300 /* 3 seconds */
51 /* typedef for rx function */
52 typedef void (*kni_net_rx_t)(struct kni_dev *kni);
54 static int kni_net_tx(struct sk_buff *skb, struct net_device *dev);
55 static void kni_net_rx_normal(struct kni_dev *kni);
56 static void kni_net_rx_lo_fifo(struct kni_dev *kni);
57 static void kni_net_rx_lo_fifo_skb(struct kni_dev *kni);
58 static int kni_net_process_request(struct kni_dev *kni,
59 struct rte_kni_request *req);
61 /* kni rx function pointer, with default to normal rx */
62 static kni_net_rx_t kni_net_rx_func = kni_net_rx_normal;
64 /* physical address to kernel virtual address */
68 return phys_to_virt((unsigned long)pa);
71 /* physical address to virtual address */
73 pa2va(void *pa, struct rte_kni_mbuf *m)
77 va = (void *)((unsigned long)pa +
78 (unsigned long)m->buf_addr -
79 (unsigned long)m->buf_physaddr);
83 /* mbuf data kernel virtual address from mbuf kernel virtual address */
85 kva2data_kva(struct rte_kni_mbuf *m)
87 return phys_to_virt(m->buf_physaddr + m->data_off);
90 /* virtual address to physical address */
92 va2pa(void *va, struct rte_kni_mbuf *m)
96 pa = (void *)((unsigned long)va -
97 ((unsigned long)m->buf_addr -
98 (unsigned long)m->buf_physaddr));
106 kni_net_open(struct net_device *dev)
109 struct rte_kni_request req;
110 struct kni_dev *kni = netdev_priv(dev);
112 netif_start_queue(dev);
114 memset(&req, 0, sizeof(req));
115 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
117 /* Setting if_up to non-zero means up */
119 ret = kni_net_process_request(kni, &req);
121 return (ret == 0) ? req.result : ret;
125 kni_net_release(struct net_device *dev)
128 struct rte_kni_request req;
129 struct kni_dev *kni = netdev_priv(dev);
131 netif_stop_queue(dev); /* can't transmit any more */
133 memset(&req, 0, sizeof(req));
134 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
136 /* Setting if_up to 0 means down */
138 ret = kni_net_process_request(kni, &req);
140 return (ret == 0) ? req.result : ret;
144 * Configuration changes (passed on by ifconfig)
147 kni_net_config(struct net_device *dev, struct ifmap *map)
149 if (dev->flags & IFF_UP) /* can't act on a running interface */
152 /* ignore other fields */
157 * RX: normal working mode
160 kni_net_rx_normal(struct kni_dev *kni)
164 unsigned i, num_rx, num_fq;
165 struct rte_kni_mbuf *kva;
166 void *pa[MBUF_BURST_SZ];
167 void *va[MBUF_BURST_SZ];
171 struct net_device *dev = kni->net_dev;
173 /* Get the number of free entries in free_q */
174 num_fq = kni_fifo_free_count(kni->free_q);
176 /* No room on the free_q, bail out */
180 /* Calculate the number of entries to dequeue from rx_q */
181 num_rx = min(num_fq, (unsigned)MBUF_BURST_SZ);
183 /* Burst dequeue from rx_q */
184 num_rx = kni_fifo_get(kni->rx_q, pa, num_rx);
188 /* Transfer received packets to netif */
189 for (i = 0; i < num_rx; i++) {
192 data_kva = kva2data_kva(kva);
193 va[i] = pa2va(pa[i], kva);
195 skb = dev_alloc_skb(len + 2);
197 KNI_ERR("Out of mem, dropping pkts\n");
198 /* Update statistics */
199 kni->stats.rx_dropped++;
203 /* Align IP on 16B boundary */
206 if (kva->nb_segs == 1) {
207 memcpy(skb_put(skb, len), data_kva, len);
210 int kva_nb_segs = kva->nb_segs;
212 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
213 memcpy(skb_put(skb, kva->data_len),
214 data_kva, kva->data_len);
219 kva = pa2kva(va2pa(kva->next, kva));
220 data_kva = kva2data_kva(kva);
225 skb->protocol = eth_type_trans(skb, dev);
226 skb->ip_summed = CHECKSUM_UNNECESSARY;
228 /* Call netif interface */
231 /* Update statistics */
232 kni->stats.rx_bytes += len;
233 kni->stats.rx_packets++;
236 /* Burst enqueue mbufs into free_q */
237 ret = kni_fifo_put(kni->free_q, va, num_rx);
239 /* Failing should not happen */
240 KNI_ERR("Fail to enqueue entries into free_q\n");
244 * RX: loopback with enqueue/dequeue fifos.
247 kni_net_rx_lo_fifo(struct kni_dev *kni)
251 unsigned i, num, num_rq, num_tq, num_aq, num_fq;
252 struct rte_kni_mbuf *kva;
253 void *pa[MBUF_BURST_SZ];
254 void *va[MBUF_BURST_SZ];
257 struct rte_kni_mbuf *alloc_kva;
258 void *alloc_pa[MBUF_BURST_SZ];
259 void *alloc_va[MBUF_BURST_SZ];
260 void *alloc_data_kva;
262 /* Get the number of entries in rx_q */
263 num_rq = kni_fifo_count(kni->rx_q);
265 /* Get the number of free entrie in tx_q */
266 num_tq = kni_fifo_free_count(kni->tx_q);
268 /* Get the number of entries in alloc_q */
269 num_aq = kni_fifo_count(kni->alloc_q);
271 /* Get the number of free entries in free_q */
272 num_fq = kni_fifo_free_count(kni->free_q);
274 /* Calculate the number of entries to be dequeued from rx_q */
275 num = min(num_rq, num_tq);
276 num = min(num, num_aq);
277 num = min(num, num_fq);
278 num = min(num, (unsigned)MBUF_BURST_SZ);
280 /* Return if no entry to dequeue from rx_q */
284 /* Burst dequeue from rx_q */
285 ret = kni_fifo_get(kni->rx_q, pa, num);
287 return; /* Failing should not happen */
289 /* Dequeue entries from alloc_q */
290 ret = kni_fifo_get(kni->alloc_q, alloc_pa, num);
294 for (i = 0; i < num; i++) {
297 data_kva = kva2data_kva(kva);
298 va[i] = pa2va(pa[i], kva);
300 alloc_kva = pa2kva(alloc_pa[i]);
301 alloc_data_kva = kva2data_kva(alloc_kva);
302 alloc_va[i] = pa2va(alloc_pa[i], alloc_kva);
304 memcpy(alloc_data_kva, data_kva, len);
305 alloc_kva->pkt_len = len;
306 alloc_kva->data_len = len;
308 kni->stats.tx_bytes += len;
309 kni->stats.rx_bytes += len;
312 /* Burst enqueue mbufs into tx_q */
313 ret = kni_fifo_put(kni->tx_q, alloc_va, num);
315 /* Failing should not happen */
316 KNI_ERR("Fail to enqueue mbufs into tx_q\n");
319 /* Burst enqueue mbufs into free_q */
320 ret = kni_fifo_put(kni->free_q, va, num);
322 /* Failing should not happen */
323 KNI_ERR("Fail to enqueue mbufs into free_q\n");
326 * Update statistic, and enqueue/dequeue failure is impossible,
327 * as all queues are checked at first.
329 kni->stats.tx_packets += num;
330 kni->stats.rx_packets += num;
334 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
337 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
341 unsigned i, num_rq, num_fq, num;
342 struct rte_kni_mbuf *kva;
343 void *pa[MBUF_BURST_SZ];
344 void *va[MBUF_BURST_SZ];
348 struct net_device *dev = kni->net_dev;
350 /* Get the number of entries in rx_q */
351 num_rq = kni_fifo_count(kni->rx_q);
353 /* Get the number of free entries in free_q */
354 num_fq = kni_fifo_free_count(kni->free_q);
356 /* Calculate the number of entries to dequeue from rx_q */
357 num = min(num_rq, num_fq);
358 num = min(num, (unsigned)MBUF_BURST_SZ);
360 /* Return if no entry to dequeue from rx_q */
364 /* Burst dequeue mbufs from rx_q */
365 ret = kni_fifo_get(kni->rx_q, pa, num);
369 /* Copy mbufs to sk buffer and then call tx interface */
370 for (i = 0; i < num; i++) {
373 data_kva = kva2data_kva(kva);
374 va[i] = pa2va(pa[i], kva);
376 skb = dev_alloc_skb(len + 2);
378 KNI_ERR("Out of mem, dropping pkts\n");
380 /* Align IP on 16B boundary */
382 memcpy(skb_put(skb, len), data_kva, len);
384 skb->ip_summed = CHECKSUM_UNNECESSARY;
388 /* Simulate real usage, allocate/copy skb twice */
389 skb = dev_alloc_skb(len + 2);
391 KNI_ERR("Out of mem, dropping pkts\n");
392 kni->stats.rx_dropped++;
396 /* Align IP on 16B boundary */
399 if (kva->nb_segs == 1) {
400 memcpy(skb_put(skb, len), data_kva, len);
403 int kva_nb_segs = kva->nb_segs;
405 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
406 memcpy(skb_put(skb, kva->data_len),
407 data_kva, kva->data_len);
412 kva = pa2kva(va2pa(kva->next, kva));
413 data_kva = kva2data_kva(kva);
418 skb->ip_summed = CHECKSUM_UNNECESSARY;
420 kni->stats.rx_bytes += len;
421 kni->stats.rx_packets++;
423 /* call tx interface */
424 kni_net_tx(skb, dev);
427 /* enqueue all the mbufs from rx_q into free_q */
428 ret = kni_fifo_put(kni->free_q, va, num);
430 /* Failing should not happen */
431 KNI_ERR("Fail to enqueue mbufs into free_q\n");
436 kni_net_rx(struct kni_dev *kni)
439 * It doesn't need to check if it is NULL pointer,
440 * as it has a default value
442 (*kni_net_rx_func)(kni);
446 * Transmit a packet (called by the kernel)
450 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
452 struct kni_dev *kni = netdev_priv(dev);
455 kni->stats.tx_dropped++;
461 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
465 struct kni_dev *kni = netdev_priv(dev);
466 struct rte_kni_mbuf *pkt_kva = NULL;
470 /* save the timestamp */
471 #ifdef HAVE_TRANS_START_HELPER
472 netif_trans_update(dev);
474 dev->trans_start = jiffies;
477 /* Check if the length of skb is less than mbuf size */
478 if (skb->len > kni->mbuf_size)
482 * Check if it has at least one free entry in tx_q and
483 * one entry in alloc_q.
485 if (kni_fifo_free_count(kni->tx_q) == 0 ||
486 kni_fifo_count(kni->alloc_q) == 0) {
488 * If no free entry in tx_q or no entry in alloc_q,
489 * drops skb and goes out.
494 /* dequeue a mbuf from alloc_q */
495 ret = kni_fifo_get(kni->alloc_q, &pkt_pa, 1);
496 if (likely(ret == 1)) {
499 pkt_kva = pa2kva(pkt_pa);
500 data_kva = kva2data_kva(pkt_kva);
501 pkt_va = pa2va(pkt_pa, pkt_kva);
504 memcpy(data_kva, skb->data, len);
505 if (unlikely(len < ETH_ZLEN)) {
506 memset(data_kva + len, 0, ETH_ZLEN - len);
509 pkt_kva->pkt_len = len;
510 pkt_kva->data_len = len;
512 /* enqueue mbuf into tx_q */
513 ret = kni_fifo_put(kni->tx_q, &pkt_va, 1);
514 if (unlikely(ret != 1)) {
515 /* Failing should not happen */
516 KNI_ERR("Fail to enqueue mbuf into tx_q\n");
520 /* Failing should not happen */
521 KNI_ERR("Fail to dequeue mbuf from alloc_q\n");
525 /* Free skb and update statistics */
527 kni->stats.tx_bytes += len;
528 kni->stats.tx_packets++;
533 /* Free skb and update statistics */
535 kni->stats.tx_dropped++;
542 * Deal with a transmit timeout.
545 kni_net_tx_timeout (struct net_device *dev)
547 struct kni_dev *kni = netdev_priv(dev);
549 KNI_DBG("Transmit timeout at %ld, latency %ld\n", jiffies,
550 jiffies - dev_trans_start(dev));
552 kni->stats.tx_errors++;
553 netif_wake_queue(dev);
561 kni_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
563 KNI_DBG("kni_net_ioctl %d\n",
564 ((struct kni_dev *)netdev_priv(dev))->group_id);
570 kni_net_set_rx_mode(struct net_device *dev)
575 kni_net_change_mtu(struct net_device *dev, int new_mtu)
578 struct rte_kni_request req;
579 struct kni_dev *kni = netdev_priv(dev);
581 KNI_DBG("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
583 memset(&req, 0, sizeof(req));
584 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
585 req.new_mtu = new_mtu;
586 ret = kni_net_process_request(kni, &req);
587 if (ret == 0 && req.result == 0)
590 return (ret == 0) ? req.result : ret;
594 * Checks if the user space application provided the resp message
597 kni_net_poll_resp(struct kni_dev *kni)
599 if (kni_fifo_count(kni->resp_q))
600 wake_up_interruptible(&kni->wq);
604 * It can be called to process the request.
607 kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
615 KNI_ERR("No kni instance or request\n");
619 mutex_lock(&kni->sync_lock);
622 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
623 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
625 KNI_ERR("Cannot send to req_q\n");
630 ret_val = wait_event_interruptible_timeout(kni->wq,
631 kni_fifo_count(kni->resp_q), 3 * HZ);
632 if (signal_pending(current) || ret_val <= 0) {
636 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
637 if (num != 1 || resp_va != kni->sync_va) {
638 /* This should never happen */
639 KNI_ERR("No data in resp_q\n");
644 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
648 mutex_unlock(&kni->sync_lock);
653 * Return statistics to the caller
655 static struct net_device_stats *
656 kni_net_stats(struct net_device *dev)
658 struct kni_dev *kni = netdev_priv(dev);
663 * Fill the eth header
666 kni_net_header(struct sk_buff *skb, struct net_device *dev,
667 unsigned short type, const void *daddr,
668 const void *saddr, unsigned int len)
670 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
672 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
673 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
674 eth->h_proto = htons(type);
676 return dev->hard_header_len;
681 * Re-fill the eth header
683 #ifdef HAVE_REBUILD_HEADER
685 kni_net_rebuild_header(struct sk_buff *skb)
687 struct net_device *dev = skb->dev;
688 struct ethhdr *eth = (struct ethhdr *) skb->data;
690 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
691 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
698 * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
699 * @netdev: network interface device structure
700 * @p: pointer to an address structure
702 * Returns 0 on success, negative on failure
704 static int kni_net_set_mac(struct net_device *netdev, void *p)
706 struct sockaddr *addr = p;
707 if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
708 return -EADDRNOTAVAIL;
709 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
713 #ifdef HAVE_CHANGE_CARRIER_CB
714 static int kni_net_change_carrier(struct net_device *dev, bool new_carrier)
717 netif_carrier_on(dev);
719 netif_carrier_off(dev);
724 static const struct header_ops kni_net_header_ops = {
725 .create = kni_net_header,
726 #ifdef HAVE_REBUILD_HEADER
727 .rebuild = kni_net_rebuild_header,
729 .cache = NULL, /* disable caching */
732 static const struct net_device_ops kni_net_netdev_ops = {
733 .ndo_open = kni_net_open,
734 .ndo_stop = kni_net_release,
735 .ndo_set_config = kni_net_config,
736 .ndo_start_xmit = kni_net_tx,
737 .ndo_change_mtu = kni_net_change_mtu,
738 .ndo_do_ioctl = kni_net_ioctl,
739 .ndo_set_rx_mode = kni_net_set_rx_mode,
740 .ndo_get_stats = kni_net_stats,
741 .ndo_tx_timeout = kni_net_tx_timeout,
742 .ndo_set_mac_address = kni_net_set_mac,
743 #ifdef HAVE_CHANGE_CARRIER_CB
744 .ndo_change_carrier = kni_net_change_carrier,
749 kni_net_init(struct net_device *dev)
751 struct kni_dev *kni = netdev_priv(dev);
753 KNI_DBG("kni_net_init\n");
755 init_waitqueue_head(&kni->wq);
756 mutex_init(&kni->sync_lock);
758 ether_setup(dev); /* assign some of the fields */
759 dev->netdev_ops = &kni_net_netdev_ops;
760 dev->header_ops = &kni_net_header_ops;
761 dev->watchdog_timeo = WD_TIMEOUT;
765 kni_net_config_lo_mode(char *lo_str)
768 KNI_PRINT("loopback disabled");
772 if (!strcmp(lo_str, "lo_mode_none"))
773 KNI_PRINT("loopback disabled");
774 else if (!strcmp(lo_str, "lo_mode_fifo")) {
775 KNI_PRINT("loopback mode=lo_mode_fifo enabled");
776 kni_net_rx_func = kni_net_rx_lo_fifo;
777 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
778 KNI_PRINT("loopback mode=lo_mode_fifo_skb enabled");
779 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
781 KNI_PRINT("Incognizant parameter, loopback disabled");