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 KNI_WAIT_RESPONSE_TIMEOUT 300 /* 3 seconds */
49 /* typedef for rx function */
50 typedef void (*kni_net_rx_t)(struct kni_dev *kni);
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);
59 /* kni rx function pointer, with default to normal rx */
60 static kni_net_rx_t kni_net_rx_func = kni_net_rx_normal;
62 /* physical address to kernel virtual address */
66 return phys_to_virt((unsigned long)pa);
69 /* physical address to virtual address */
71 pa2va(void *pa, struct rte_kni_mbuf *m)
75 va = (void *)((unsigned long)pa +
76 (unsigned long)m->buf_addr -
77 (unsigned long)m->buf_physaddr);
81 /* mbuf data kernel virtual address from mbuf kernel virtual address */
83 kva2data_kva(struct rte_kni_mbuf *m)
85 return phys_to_virt(m->buf_physaddr + m->data_off);
88 /* virtual address to physical address */
90 va2pa(void *va, struct rte_kni_mbuf *m)
94 pa = (void *)((unsigned long)va -
95 ((unsigned long)m->buf_addr -
96 (unsigned long)m->buf_physaddr));
104 kni_net_open(struct net_device *dev)
107 struct rte_kni_request req;
108 struct kni_dev *kni = netdev_priv(dev);
110 netif_start_queue(dev);
112 memset(&req, 0, sizeof(req));
113 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
115 /* Setting if_up to non-zero means up */
117 ret = kni_net_process_request(kni, &req);
119 return (ret == 0) ? req.result : ret;
123 kni_net_release(struct net_device *dev)
126 struct rte_kni_request req;
127 struct kni_dev *kni = netdev_priv(dev);
129 netif_stop_queue(dev); /* can't transmit any more */
131 memset(&req, 0, sizeof(req));
132 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
134 /* Setting if_up to 0 means down */
136 ret = kni_net_process_request(kni, &req);
138 return (ret == 0) ? req.result : ret;
142 * Configuration changes (passed on by ifconfig)
145 kni_net_config(struct net_device *dev, struct ifmap *map)
147 if (dev->flags & IFF_UP) /* can't act on a running interface */
150 /* ignore other fields */
155 * RX: normal working mode
158 kni_net_rx_normal(struct kni_dev *kni)
162 unsigned int i, num_rx, num_fq;
163 struct rte_kni_mbuf *kva;
166 struct net_device *dev = kni->net_dev;
168 /* Get the number of free entries in free_q */
169 num_fq = kni_fifo_free_count(kni->free_q);
171 /* No room on the free_q, bail out */
175 /* Calculate the number of entries to dequeue from rx_q */
176 num_rx = min_t(unsigned int, num_fq, MBUF_BURST_SZ);
178 /* Burst dequeue from rx_q */
179 num_rx = kni_fifo_get(kni->rx_q, kni->pa, num_rx);
183 /* Transfer received packets to netif */
184 for (i = 0; i < num_rx; i++) {
185 kva = pa2kva(kni->pa[i]);
187 data_kva = kva2data_kva(kva);
188 kni->va[i] = pa2va(kni->pa[i], kva);
190 skb = dev_alloc_skb(len + 2);
192 /* Update statistics */
193 kni->stats.rx_dropped++;
197 /* Align IP on 16B boundary */
200 if (kva->nb_segs == 1) {
201 memcpy(skb_put(skb, len), data_kva, len);
204 int kva_nb_segs = kva->nb_segs;
206 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
207 memcpy(skb_put(skb, kva->data_len),
208 data_kva, kva->data_len);
213 kva = pa2kva(va2pa(kva->next, kva));
214 data_kva = kva2data_kva(kva);
219 skb->protocol = eth_type_trans(skb, dev);
220 skb->ip_summed = CHECKSUM_UNNECESSARY;
222 /* Call netif interface */
225 /* Update statistics */
226 kni->stats.rx_bytes += len;
227 kni->stats.rx_packets++;
230 /* Burst enqueue mbufs into free_q */
231 ret = kni_fifo_put(kni->free_q, kni->va, num_rx);
233 /* Failing should not happen */
234 pr_err("Fail to enqueue entries into free_q\n");
238 * RX: loopback with enqueue/dequeue fifos.
241 kni_net_rx_lo_fifo(struct kni_dev *kni)
245 unsigned int i, num, num_rq, num_tq, num_aq, num_fq;
246 struct rte_kni_mbuf *kva;
248 struct rte_kni_mbuf *alloc_kva;
249 void *alloc_data_kva;
251 /* Get the number of entries in rx_q */
252 num_rq = kni_fifo_count(kni->rx_q);
254 /* Get the number of free entrie in tx_q */
255 num_tq = kni_fifo_free_count(kni->tx_q);
257 /* Get the number of entries in alloc_q */
258 num_aq = kni_fifo_count(kni->alloc_q);
260 /* Get the number of free entries in free_q */
261 num_fq = kni_fifo_free_count(kni->free_q);
263 /* Calculate the number of entries to be dequeued from rx_q */
264 num = min(num_rq, num_tq);
265 num = min(num, num_aq);
266 num = min(num, num_fq);
267 num = min_t(unsigned int, num, MBUF_BURST_SZ);
269 /* Return if no entry to dequeue from rx_q */
273 /* Burst dequeue from rx_q */
274 ret = kni_fifo_get(kni->rx_q, kni->pa, num);
276 return; /* Failing should not happen */
278 /* Dequeue entries from alloc_q */
279 ret = kni_fifo_get(kni->alloc_q, kni->alloc_pa, num);
283 for (i = 0; i < num; i++) {
284 kva = pa2kva(kni->pa[i]);
286 data_kva = kva2data_kva(kva);
287 kni->va[i] = pa2va(kni->pa[i], kva);
289 alloc_kva = pa2kva(kni->alloc_pa[i]);
290 alloc_data_kva = kva2data_kva(alloc_kva);
291 kni->alloc_va[i] = pa2va(kni->alloc_pa[i], alloc_kva);
293 memcpy(alloc_data_kva, data_kva, len);
294 alloc_kva->pkt_len = len;
295 alloc_kva->data_len = len;
297 kni->stats.tx_bytes += len;
298 kni->stats.rx_bytes += len;
301 /* Burst enqueue mbufs into tx_q */
302 ret = kni_fifo_put(kni->tx_q, kni->alloc_va, num);
304 /* Failing should not happen */
305 pr_err("Fail to enqueue mbufs into tx_q\n");
308 /* Burst enqueue mbufs into free_q */
309 ret = kni_fifo_put(kni->free_q, kni->va, num);
311 /* Failing should not happen */
312 pr_err("Fail to enqueue mbufs into free_q\n");
315 * Update statistic, and enqueue/dequeue failure is impossible,
316 * as all queues are checked at first.
318 kni->stats.tx_packets += num;
319 kni->stats.rx_packets += num;
323 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
326 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
330 unsigned int i, num_rq, num_fq, num;
331 struct rte_kni_mbuf *kva;
334 struct net_device *dev = kni->net_dev;
336 /* Get the number of entries in rx_q */
337 num_rq = kni_fifo_count(kni->rx_q);
339 /* Get the number of free entries in free_q */
340 num_fq = kni_fifo_free_count(kni->free_q);
342 /* Calculate the number of entries to dequeue from rx_q */
343 num = min(num_rq, num_fq);
344 num = min_t(unsigned int, num, MBUF_BURST_SZ);
346 /* Return if no entry to dequeue from rx_q */
350 /* Burst dequeue mbufs from rx_q */
351 ret = kni_fifo_get(kni->rx_q, kni->pa, num);
355 /* Copy mbufs to sk buffer and then call tx interface */
356 for (i = 0; i < num; i++) {
357 kva = pa2kva(kni->pa[i]);
359 data_kva = kva2data_kva(kva);
360 kni->va[i] = pa2va(kni->pa[i], kva);
362 skb = dev_alloc_skb(len + 2);
364 /* Align IP on 16B boundary */
366 memcpy(skb_put(skb, len), data_kva, len);
368 skb->ip_summed = CHECKSUM_UNNECESSARY;
372 /* Simulate real usage, allocate/copy skb twice */
373 skb = dev_alloc_skb(len + 2);
375 kni->stats.rx_dropped++;
379 /* Align IP on 16B boundary */
382 if (kva->nb_segs == 1) {
383 memcpy(skb_put(skb, len), data_kva, len);
386 int kva_nb_segs = kva->nb_segs;
388 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
389 memcpy(skb_put(skb, kva->data_len),
390 data_kva, kva->data_len);
395 kva = pa2kva(va2pa(kva->next, kva));
396 data_kva = kva2data_kva(kva);
401 skb->ip_summed = CHECKSUM_UNNECESSARY;
403 kni->stats.rx_bytes += len;
404 kni->stats.rx_packets++;
406 /* call tx interface */
407 kni_net_tx(skb, dev);
410 /* enqueue all the mbufs from rx_q into free_q */
411 ret = kni_fifo_put(kni->free_q, kni->va, num);
413 /* Failing should not happen */
414 pr_err("Fail to enqueue mbufs into free_q\n");
419 kni_net_rx(struct kni_dev *kni)
422 * It doesn't need to check if it is NULL pointer,
423 * as it has a default value
425 (*kni_net_rx_func)(kni);
429 * Transmit a packet (called by the kernel)
433 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
435 struct kni_dev *kni = netdev_priv(dev);
438 kni->stats.tx_dropped++;
444 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
448 struct kni_dev *kni = netdev_priv(dev);
449 struct rte_kni_mbuf *pkt_kva = NULL;
453 /* save the timestamp */
454 #ifdef HAVE_TRANS_START_HELPER
455 netif_trans_update(dev);
457 dev->trans_start = jiffies;
460 /* Check if the length of skb is less than mbuf size */
461 if (skb->len > kni->mbuf_size)
465 * Check if it has at least one free entry in tx_q and
466 * one entry in alloc_q.
468 if (kni_fifo_free_count(kni->tx_q) == 0 ||
469 kni_fifo_count(kni->alloc_q) == 0) {
471 * If no free entry in tx_q or no entry in alloc_q,
472 * drops skb and goes out.
477 /* dequeue a mbuf from alloc_q */
478 ret = kni_fifo_get(kni->alloc_q, &pkt_pa, 1);
479 if (likely(ret == 1)) {
482 pkt_kva = pa2kva(pkt_pa);
483 data_kva = kva2data_kva(pkt_kva);
484 pkt_va = pa2va(pkt_pa, pkt_kva);
487 memcpy(data_kva, skb->data, len);
488 if (unlikely(len < ETH_ZLEN)) {
489 memset(data_kva + len, 0, ETH_ZLEN - len);
492 pkt_kva->pkt_len = len;
493 pkt_kva->data_len = len;
495 /* enqueue mbuf into tx_q */
496 ret = kni_fifo_put(kni->tx_q, &pkt_va, 1);
497 if (unlikely(ret != 1)) {
498 /* Failing should not happen */
499 pr_err("Fail to enqueue mbuf into tx_q\n");
503 /* Failing should not happen */
504 pr_err("Fail to dequeue mbuf from alloc_q\n");
508 /* Free skb and update statistics */
510 kni->stats.tx_bytes += len;
511 kni->stats.tx_packets++;
516 /* Free skb and update statistics */
518 kni->stats.tx_dropped++;
525 * Deal with a transmit timeout.
528 kni_net_tx_timeout(struct net_device *dev)
530 struct kni_dev *kni = netdev_priv(dev);
532 KNI_DBG("Transmit timeout at %ld, latency %ld\n", jiffies,
533 jiffies - dev_trans_start(dev));
535 kni->stats.tx_errors++;
536 netif_wake_queue(dev);
543 kni_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
545 KNI_DBG("kni_net_ioctl %d\n",
546 ((struct kni_dev *)netdev_priv(dev))->group_id);
552 kni_net_set_rx_mode(struct net_device *dev)
557 kni_net_change_mtu(struct net_device *dev, int new_mtu)
560 struct rte_kni_request req;
561 struct kni_dev *kni = netdev_priv(dev);
563 KNI_DBG("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
565 memset(&req, 0, sizeof(req));
566 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
567 req.new_mtu = new_mtu;
568 ret = kni_net_process_request(kni, &req);
569 if (ret == 0 && req.result == 0)
572 return (ret == 0) ? req.result : ret;
576 * Checks if the user space application provided the resp message
579 kni_net_poll_resp(struct kni_dev *kni)
581 if (kni_fifo_count(kni->resp_q))
582 wake_up_interruptible(&kni->wq);
586 * It can be called to process the request.
589 kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
597 pr_err("No kni instance or request\n");
601 mutex_lock(&kni->sync_lock);
604 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
605 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
607 pr_err("Cannot send to req_q\n");
612 ret_val = wait_event_interruptible_timeout(kni->wq,
613 kni_fifo_count(kni->resp_q), 3 * HZ);
614 if (signal_pending(current) || ret_val <= 0) {
618 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
619 if (num != 1 || resp_va != kni->sync_va) {
620 /* This should never happen */
621 pr_err("No data in resp_q\n");
626 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
630 mutex_unlock(&kni->sync_lock);
635 * Return statistics to the caller
637 static struct net_device_stats *
638 kni_net_stats(struct net_device *dev)
640 struct kni_dev *kni = netdev_priv(dev);
646 * Fill the eth header
649 kni_net_header(struct sk_buff *skb, struct net_device *dev,
650 unsigned short type, const void *daddr,
651 const void *saddr, unsigned int len)
653 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
655 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
656 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
657 eth->h_proto = htons(type);
659 return dev->hard_header_len;
663 * Re-fill the eth header
665 #ifdef HAVE_REBUILD_HEADER
667 kni_net_rebuild_header(struct sk_buff *skb)
669 struct net_device *dev = skb->dev;
670 struct ethhdr *eth = (struct ethhdr *) skb->data;
672 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
673 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
680 * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
681 * @netdev: network interface device structure
682 * @p: pointer to an address structure
684 * Returns 0 on success, negative on failure
687 kni_net_set_mac(struct net_device *netdev, void *p)
689 struct sockaddr *addr = p;
691 if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
692 return -EADDRNOTAVAIL;
693 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
697 #ifdef HAVE_CHANGE_CARRIER_CB
699 kni_net_change_carrier(struct net_device *dev, bool new_carrier)
702 netif_carrier_on(dev);
704 netif_carrier_off(dev);
709 static const struct header_ops kni_net_header_ops = {
710 .create = kni_net_header,
711 #ifdef HAVE_REBUILD_HEADER
712 .rebuild = kni_net_rebuild_header,
714 .cache = NULL, /* disable caching */
717 static const struct net_device_ops kni_net_netdev_ops = {
718 .ndo_open = kni_net_open,
719 .ndo_stop = kni_net_release,
720 .ndo_set_config = kni_net_config,
721 .ndo_start_xmit = kni_net_tx,
722 .ndo_change_mtu = kni_net_change_mtu,
723 .ndo_do_ioctl = kni_net_ioctl,
724 .ndo_set_rx_mode = kni_net_set_rx_mode,
725 .ndo_get_stats = kni_net_stats,
726 .ndo_tx_timeout = kni_net_tx_timeout,
727 .ndo_set_mac_address = kni_net_set_mac,
728 #ifdef HAVE_CHANGE_CARRIER_CB
729 .ndo_change_carrier = kni_net_change_carrier,
734 kni_net_init(struct net_device *dev)
736 struct kni_dev *kni = netdev_priv(dev);
738 KNI_DBG("kni_net_init\n");
740 init_waitqueue_head(&kni->wq);
741 mutex_init(&kni->sync_lock);
743 ether_setup(dev); /* assign some of the fields */
744 dev->netdev_ops = &kni_net_netdev_ops;
745 dev->header_ops = &kni_net_header_ops;
746 dev->watchdog_timeo = WD_TIMEOUT;
750 kni_net_config_lo_mode(char *lo_str)
753 pr_debug("loopback disabled");
757 if (!strcmp(lo_str, "lo_mode_none"))
758 pr_debug("loopback disabled");
759 else if (!strcmp(lo_str, "lo_mode_fifo")) {
760 pr_debug("loopback mode=lo_mode_fifo enabled");
761 kni_net_rx_func = kni_net_rx_lo_fifo;
762 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
763 pr_debug("loopback mode=lo_mode_fifo_skb enabled");
764 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
766 pr_debug("Incognizant parameter, loopback disabled");