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 void kni_net_rx_normal(struct kni_dev *kni);
54 /* kni rx function pointer, with default to normal rx */
55 static kni_net_rx_t kni_net_rx_func = kni_net_rx_normal;
57 /* physical address to kernel virtual address */
61 return phys_to_virt((unsigned long)pa);
64 /* physical address to virtual address */
66 pa2va(void *pa, struct rte_kni_mbuf *m)
70 va = (void *)((unsigned long)pa +
71 (unsigned long)m->buf_addr -
72 (unsigned long)m->buf_physaddr);
76 /* mbuf data kernel virtual address from mbuf kernel virtual address */
78 kva2data_kva(struct rte_kni_mbuf *m)
80 return phys_to_virt(m->buf_physaddr + m->data_off);
83 /* virtual address to physical address */
85 va2pa(void *va, struct rte_kni_mbuf *m)
89 pa = (void *)((unsigned long)va -
90 ((unsigned long)m->buf_addr -
91 (unsigned long)m->buf_physaddr));
96 * It can be called to process the request.
99 kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
107 pr_err("No kni instance or request\n");
111 mutex_lock(&kni->sync_lock);
114 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
115 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
117 pr_err("Cannot send to req_q\n");
122 ret_val = wait_event_interruptible_timeout(kni->wq,
123 kni_fifo_count(kni->resp_q), 3 * HZ);
124 if (signal_pending(current) || ret_val <= 0) {
128 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
129 if (num != 1 || resp_va != kni->sync_va) {
130 /* This should never happen */
131 pr_err("No data in resp_q\n");
136 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
140 mutex_unlock(&kni->sync_lock);
148 kni_net_open(struct net_device *dev)
151 struct rte_kni_request req;
152 struct kni_dev *kni = netdev_priv(dev);
154 netif_start_queue(dev);
156 memset(&req, 0, sizeof(req));
157 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
159 /* Setting if_up to non-zero means up */
161 ret = kni_net_process_request(kni, &req);
163 return (ret == 0) ? req.result : ret;
167 kni_net_release(struct net_device *dev)
170 struct rte_kni_request req;
171 struct kni_dev *kni = netdev_priv(dev);
173 netif_stop_queue(dev); /* can't transmit any more */
175 memset(&req, 0, sizeof(req));
176 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
178 /* Setting if_up to 0 means down */
180 ret = kni_net_process_request(kni, &req);
182 return (ret == 0) ? req.result : ret;
186 * Configuration changes (passed on by ifconfig)
189 kni_net_config(struct net_device *dev, struct ifmap *map)
191 if (dev->flags & IFF_UP) /* can't act on a running interface */
194 /* ignore other fields */
199 * Transmit a packet (called by the kernel)
202 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
206 struct kni_dev *kni = netdev_priv(dev);
207 struct rte_kni_mbuf *pkt_kva = NULL;
211 /* save the timestamp */
212 #ifdef HAVE_TRANS_START_HELPER
213 netif_trans_update(dev);
215 dev->trans_start = jiffies;
218 /* Check if the length of skb is less than mbuf size */
219 if (skb->len > kni->mbuf_size)
223 * Check if it has at least one free entry in tx_q and
224 * one entry in alloc_q.
226 if (kni_fifo_free_count(kni->tx_q) == 0 ||
227 kni_fifo_count(kni->alloc_q) == 0) {
229 * If no free entry in tx_q or no entry in alloc_q,
230 * drops skb and goes out.
235 /* dequeue a mbuf from alloc_q */
236 ret = kni_fifo_get(kni->alloc_q, &pkt_pa, 1);
237 if (likely(ret == 1)) {
240 pkt_kva = pa2kva(pkt_pa);
241 data_kva = kva2data_kva(pkt_kva);
242 pkt_va = pa2va(pkt_pa, pkt_kva);
245 memcpy(data_kva, skb->data, len);
246 if (unlikely(len < ETH_ZLEN)) {
247 memset(data_kva + len, 0, ETH_ZLEN - len);
250 pkt_kva->pkt_len = len;
251 pkt_kva->data_len = len;
253 /* enqueue mbuf into tx_q */
254 ret = kni_fifo_put(kni->tx_q, &pkt_va, 1);
255 if (unlikely(ret != 1)) {
256 /* Failing should not happen */
257 pr_err("Fail to enqueue mbuf into tx_q\n");
261 /* Failing should not happen */
262 pr_err("Fail to dequeue mbuf from alloc_q\n");
266 /* Free skb and update statistics */
268 kni->stats.tx_bytes += len;
269 kni->stats.tx_packets++;
274 /* Free skb and update statistics */
276 kni->stats.tx_dropped++;
282 * RX: normal working mode
285 kni_net_rx_normal(struct kni_dev *kni)
289 uint32_t i, num_rx, num_fq;
290 struct rte_kni_mbuf *kva;
293 struct net_device *dev = kni->net_dev;
295 /* Get the number of free entries in free_q */
296 num_fq = kni_fifo_free_count(kni->free_q);
298 /* No room on the free_q, bail out */
302 /* Calculate the number of entries to dequeue from rx_q */
303 num_rx = min_t(uint32_t, num_fq, MBUF_BURST_SZ);
305 /* Burst dequeue from rx_q */
306 num_rx = kni_fifo_get(kni->rx_q, kni->pa, num_rx);
310 /* Transfer received packets to netif */
311 for (i = 0; i < num_rx; i++) {
312 kva = pa2kva(kni->pa[i]);
314 data_kva = kva2data_kva(kva);
315 kni->va[i] = pa2va(kni->pa[i], kva);
317 skb = dev_alloc_skb(len + 2);
319 /* Update statistics */
320 kni->stats.rx_dropped++;
324 /* Align IP on 16B boundary */
327 if (kva->nb_segs == 1) {
328 memcpy(skb_put(skb, len), data_kva, len);
331 int kva_nb_segs = kva->nb_segs;
333 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
334 memcpy(skb_put(skb, kva->data_len),
335 data_kva, kva->data_len);
340 kva = pa2kva(va2pa(kva->next, kva));
341 data_kva = kva2data_kva(kva);
346 skb->protocol = eth_type_trans(skb, dev);
347 skb->ip_summed = CHECKSUM_UNNECESSARY;
349 /* Call netif interface */
352 /* Update statistics */
353 kni->stats.rx_bytes += len;
354 kni->stats.rx_packets++;
357 /* Burst enqueue mbufs into free_q */
358 ret = kni_fifo_put(kni->free_q, kni->va, num_rx);
360 /* Failing should not happen */
361 pr_err("Fail to enqueue entries into free_q\n");
365 * RX: loopback with enqueue/dequeue fifos.
368 kni_net_rx_lo_fifo(struct kni_dev *kni)
372 uint32_t i, num, num_rq, num_tq, num_aq, num_fq;
373 struct rte_kni_mbuf *kva;
375 struct rte_kni_mbuf *alloc_kva;
376 void *alloc_data_kva;
378 /* Get the number of entries in rx_q */
379 num_rq = kni_fifo_count(kni->rx_q);
381 /* Get the number of free entrie in tx_q */
382 num_tq = kni_fifo_free_count(kni->tx_q);
384 /* Get the number of entries in alloc_q */
385 num_aq = kni_fifo_count(kni->alloc_q);
387 /* Get the number of free entries in free_q */
388 num_fq = kni_fifo_free_count(kni->free_q);
390 /* Calculate the number of entries to be dequeued from rx_q */
391 num = min(num_rq, num_tq);
392 num = min(num, num_aq);
393 num = min(num, num_fq);
394 num = min_t(uint32_t, num, MBUF_BURST_SZ);
396 /* Return if no entry to dequeue from rx_q */
400 /* Burst dequeue from rx_q */
401 ret = kni_fifo_get(kni->rx_q, kni->pa, num);
403 return; /* Failing should not happen */
405 /* Dequeue entries from alloc_q */
406 ret = kni_fifo_get(kni->alloc_q, kni->alloc_pa, num);
410 for (i = 0; i < num; i++) {
411 kva = pa2kva(kni->pa[i]);
413 data_kva = kva2data_kva(kva);
414 kni->va[i] = pa2va(kni->pa[i], kva);
416 alloc_kva = pa2kva(kni->alloc_pa[i]);
417 alloc_data_kva = kva2data_kva(alloc_kva);
418 kni->alloc_va[i] = pa2va(kni->alloc_pa[i], alloc_kva);
420 memcpy(alloc_data_kva, data_kva, len);
421 alloc_kva->pkt_len = len;
422 alloc_kva->data_len = len;
424 kni->stats.tx_bytes += len;
425 kni->stats.rx_bytes += len;
428 /* Burst enqueue mbufs into tx_q */
429 ret = kni_fifo_put(kni->tx_q, kni->alloc_va, num);
431 /* Failing should not happen */
432 pr_err("Fail to enqueue mbufs into tx_q\n");
435 /* Burst enqueue mbufs into free_q */
436 ret = kni_fifo_put(kni->free_q, kni->va, num);
438 /* Failing should not happen */
439 pr_err("Fail to enqueue mbufs into free_q\n");
442 * Update statistic, and enqueue/dequeue failure is impossible,
443 * as all queues are checked at first.
445 kni->stats.tx_packets += num;
446 kni->stats.rx_packets += num;
450 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
453 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
457 uint32_t i, num_rq, num_fq, num;
458 struct rte_kni_mbuf *kva;
461 struct net_device *dev = kni->net_dev;
463 /* Get the number of entries in rx_q */
464 num_rq = kni_fifo_count(kni->rx_q);
466 /* Get the number of free entries in free_q */
467 num_fq = kni_fifo_free_count(kni->free_q);
469 /* Calculate the number of entries to dequeue from rx_q */
470 num = min(num_rq, num_fq);
471 num = min_t(uint32_t, num, MBUF_BURST_SZ);
473 /* Return if no entry to dequeue from rx_q */
477 /* Burst dequeue mbufs from rx_q */
478 ret = kni_fifo_get(kni->rx_q, kni->pa, num);
482 /* Copy mbufs to sk buffer and then call tx interface */
483 for (i = 0; i < num; i++) {
484 kva = pa2kva(kni->pa[i]);
486 data_kva = kva2data_kva(kva);
487 kni->va[i] = pa2va(kni->pa[i], kva);
489 skb = dev_alloc_skb(len + 2);
491 /* Align IP on 16B boundary */
493 memcpy(skb_put(skb, len), data_kva, len);
495 skb->ip_summed = CHECKSUM_UNNECESSARY;
499 /* Simulate real usage, allocate/copy skb twice */
500 skb = dev_alloc_skb(len + 2);
502 kni->stats.rx_dropped++;
506 /* Align IP on 16B boundary */
509 if (kva->nb_segs == 1) {
510 memcpy(skb_put(skb, len), data_kva, len);
513 int kva_nb_segs = kva->nb_segs;
515 for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
516 memcpy(skb_put(skb, kva->data_len),
517 data_kva, kva->data_len);
522 kva = pa2kva(va2pa(kva->next, kva));
523 data_kva = kva2data_kva(kva);
528 skb->ip_summed = CHECKSUM_UNNECESSARY;
530 kni->stats.rx_bytes += len;
531 kni->stats.rx_packets++;
533 /* call tx interface */
534 kni_net_tx(skb, dev);
537 /* enqueue all the mbufs from rx_q into free_q */
538 ret = kni_fifo_put(kni->free_q, kni->va, num);
540 /* Failing should not happen */
541 pr_err("Fail to enqueue mbufs into free_q\n");
546 kni_net_rx(struct kni_dev *kni)
549 * It doesn't need to check if it is NULL pointer,
550 * as it has a default value
552 (*kni_net_rx_func)(kni);
556 * Deal with a transmit timeout.
559 kni_net_tx_timeout(struct net_device *dev)
561 struct kni_dev *kni = netdev_priv(dev);
563 pr_debug("Transmit timeout at %ld, latency %ld\n", jiffies,
564 jiffies - dev_trans_start(dev));
566 kni->stats.tx_errors++;
567 netif_wake_queue(dev);
574 kni_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
576 pr_debug("kni_net_ioctl group:%d cmd:%d\n",
577 ((struct kni_dev *)netdev_priv(dev))->group_id, cmd);
583 kni_net_set_rx_mode(struct net_device *dev)
588 kni_net_change_mtu(struct net_device *dev, int new_mtu)
591 struct rte_kni_request req;
592 struct kni_dev *kni = netdev_priv(dev);
594 pr_debug("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
596 memset(&req, 0, sizeof(req));
597 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
598 req.new_mtu = new_mtu;
599 ret = kni_net_process_request(kni, &req);
600 if (ret == 0 && req.result == 0)
603 return (ret == 0) ? req.result : ret;
607 * Checks if the user space application provided the resp message
610 kni_net_poll_resp(struct kni_dev *kni)
612 if (kni_fifo_count(kni->resp_q))
613 wake_up_interruptible(&kni->wq);
617 * Return statistics to the caller
619 static struct net_device_stats *
620 kni_net_stats(struct net_device *dev)
622 struct kni_dev *kni = netdev_priv(dev);
628 * Fill the eth header
631 kni_net_header(struct sk_buff *skb, struct net_device *dev,
632 unsigned short type, const void *daddr,
633 const void *saddr, uint32_t len)
635 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
637 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
638 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
639 eth->h_proto = htons(type);
641 return dev->hard_header_len;
645 * Re-fill the eth header
647 #ifdef HAVE_REBUILD_HEADER
649 kni_net_rebuild_header(struct sk_buff *skb)
651 struct net_device *dev = skb->dev;
652 struct ethhdr *eth = (struct ethhdr *) skb->data;
654 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
655 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
662 * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
663 * @netdev: network interface device structure
664 * @p: pointer to an address structure
666 * Returns 0 on success, negative on failure
669 kni_net_set_mac(struct net_device *netdev, void *p)
671 struct sockaddr *addr = p;
673 if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
674 return -EADDRNOTAVAIL;
675 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
679 #ifdef HAVE_CHANGE_CARRIER_CB
681 kni_net_change_carrier(struct net_device *dev, bool new_carrier)
684 netif_carrier_on(dev);
686 netif_carrier_off(dev);
691 static const struct header_ops kni_net_header_ops = {
692 .create = kni_net_header,
693 #ifdef HAVE_REBUILD_HEADER
694 .rebuild = kni_net_rebuild_header,
696 .cache = NULL, /* disable caching */
699 static const struct net_device_ops kni_net_netdev_ops = {
700 .ndo_open = kni_net_open,
701 .ndo_stop = kni_net_release,
702 .ndo_set_config = kni_net_config,
703 .ndo_start_xmit = kni_net_tx,
704 .ndo_change_mtu = kni_net_change_mtu,
705 .ndo_do_ioctl = kni_net_ioctl,
706 .ndo_set_rx_mode = kni_net_set_rx_mode,
707 .ndo_get_stats = kni_net_stats,
708 .ndo_tx_timeout = kni_net_tx_timeout,
709 .ndo_set_mac_address = kni_net_set_mac,
710 #ifdef HAVE_CHANGE_CARRIER_CB
711 .ndo_change_carrier = kni_net_change_carrier,
716 kni_net_init(struct net_device *dev)
718 struct kni_dev *kni = netdev_priv(dev);
720 init_waitqueue_head(&kni->wq);
721 mutex_init(&kni->sync_lock);
723 ether_setup(dev); /* assign some of the fields */
724 dev->netdev_ops = &kni_net_netdev_ops;
725 dev->header_ops = &kni_net_header_ops;
726 dev->watchdog_timeo = WD_TIMEOUT;
730 kni_net_config_lo_mode(char *lo_str)
733 pr_debug("loopback disabled");
737 if (!strcmp(lo_str, "lo_mode_none"))
738 pr_debug("loopback disabled");
739 else if (!strcmp(lo_str, "lo_mode_fifo")) {
740 pr_debug("loopback mode=lo_mode_fifo enabled");
741 kni_net_rx_func = kni_net_rx_lo_fifo;
742 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
743 pr_debug("loopback mode=lo_mode_fifo_skb enabled");
744 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
746 pr_debug("Incognizant parameter, loopback disabled");