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>
43 #define WD_TIMEOUT 5 /*jiffies */
45 #define MBUF_BURST_SZ 32
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;
66 kni_net_open(struct net_device *dev)
69 struct rte_kni_request req;
70 struct kni_dev *kni = netdev_priv(dev);
72 netif_start_queue(dev);
74 memset(&req, 0, sizeof(req));
75 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
77 /* Setting if_up to non-zero means up */
79 ret = kni_net_process_request(kni, &req);
81 return (ret == 0) ? req.result : ret;
85 kni_net_release(struct net_device *dev)
88 struct rte_kni_request req;
89 struct kni_dev *kni = netdev_priv(dev);
91 netif_stop_queue(dev); /* can't transmit any more */
93 memset(&req, 0, sizeof(req));
94 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
96 /* Setting if_up to 0 means down */
98 ret = kni_net_process_request(kni, &req);
100 return (ret == 0) ? req.result : ret;
104 * Configuration changes (passed on by ifconfig)
107 kni_net_config(struct net_device *dev, struct ifmap *map)
109 if (dev->flags & IFF_UP) /* can't act on a running interface */
112 /* ignore other fields */
117 * RX: normal working mode
120 kni_net_rx_normal(struct kni_dev *kni)
124 unsigned i, num_rx, num_fq;
125 struct rte_kni_mbuf *kva;
126 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
130 struct net_device *dev = kni->net_dev;
132 /* Get the number of free entries in free_q */
133 num_fq = kni_fifo_free_count(kni->free_q);
135 /* No room on the free_q, bail out */
139 /* Calculate the number of entries to dequeue from rx_q */
140 num_rx = min(num_fq, (unsigned)MBUF_BURST_SZ);
142 /* Burst dequeue from rx_q */
143 num_rx = kni_fifo_get(kni->rx_q, (void **)va, num_rx);
147 /* Transfer received packets to netif */
148 for (i = 0; i < num_rx; i++) {
149 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
151 data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va
154 skb = dev_alloc_skb(len + 2);
156 KNI_ERR("Out of mem, dropping pkts\n");
157 /* Update statistics */
158 kni->stats.rx_dropped++;
161 /* Align IP on 16B boundary */
163 memcpy(skb_put(skb, len), data_kva, len);
165 skb->protocol = eth_type_trans(skb, dev);
166 skb->ip_summed = CHECKSUM_UNNECESSARY;
168 /* Call netif interface */
171 /* Update statistics */
172 kni->stats.rx_bytes += len;
173 kni->stats.rx_packets++;
177 /* Burst enqueue mbufs into free_q */
178 ret = kni_fifo_put(kni->free_q, (void **)va, num_rx);
180 /* Failing should not happen */
181 KNI_ERR("Fail to enqueue entries into free_q\n");
185 * RX: loopback with enqueue/dequeue fifos.
188 kni_net_rx_lo_fifo(struct kni_dev *kni)
192 unsigned i, num, num_rq, num_tq, num_aq, num_fq;
193 struct rte_kni_mbuf *kva;
194 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
197 struct rte_kni_mbuf *alloc_kva;
198 struct rte_kni_mbuf *alloc_va[MBUF_BURST_SZ];
199 void *alloc_data_kva;
201 /* Get the number of entries in rx_q */
202 num_rq = kni_fifo_count(kni->rx_q);
204 /* Get the number of free entrie in tx_q */
205 num_tq = kni_fifo_free_count(kni->tx_q);
207 /* Get the number of entries in alloc_q */
208 num_aq = kni_fifo_count(kni->alloc_q);
210 /* Get the number of free entries in free_q */
211 num_fq = kni_fifo_free_count(kni->free_q);
213 /* Calculate the number of entries to be dequeued from rx_q */
214 num = min(num_rq, num_tq);
215 num = min(num, num_aq);
216 num = min(num, num_fq);
217 num = min(num, (unsigned)MBUF_BURST_SZ);
219 /* Return if no entry to dequeue from rx_q */
223 /* Burst dequeue from rx_q */
224 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
226 return; /* Failing should not happen */
228 /* Dequeue entries from alloc_q */
229 ret = kni_fifo_get(kni->alloc_q, (void **)alloc_va, num);
233 for (i = 0; i < num; i++) {
234 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
236 data_kva = kva->buf_addr + kva->data_off -
237 kni->mbuf_va + kni->mbuf_kva;
239 alloc_kva = (void *)alloc_va[i] - kni->mbuf_va +
241 alloc_data_kva = alloc_kva->buf_addr +
242 alloc_kva->data_off - kni->mbuf_va +
244 memcpy(alloc_data_kva, data_kva, len);
245 alloc_kva->pkt_len = len;
246 alloc_kva->data_len = len;
248 kni->stats.tx_bytes += len;
249 kni->stats.rx_bytes += len;
252 /* Burst enqueue mbufs into tx_q */
253 ret = kni_fifo_put(kni->tx_q, (void **)alloc_va, num);
255 /* Failing should not happen */
256 KNI_ERR("Fail to enqueue mbufs into tx_q\n");
259 /* Burst enqueue mbufs into free_q */
260 ret = kni_fifo_put(kni->free_q, (void **)va, num);
262 /* Failing should not happen */
263 KNI_ERR("Fail to enqueue mbufs into free_q\n");
266 * Update statistic, and enqueue/dequeue failure is impossible,
267 * as all queues are checked at first.
269 kni->stats.tx_packets += num;
270 kni->stats.rx_packets += num;
274 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
277 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
281 unsigned i, num_rq, num_fq, num;
282 struct rte_kni_mbuf *kva;
283 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
287 struct net_device *dev = kni->net_dev;
289 /* Get the number of entries in rx_q */
290 num_rq = kni_fifo_count(kni->rx_q);
292 /* Get the number of free entries in free_q */
293 num_fq = kni_fifo_free_count(kni->free_q);
295 /* Calculate the number of entries to dequeue from rx_q */
296 num = min(num_rq, num_fq);
297 num = min(num, (unsigned)MBUF_BURST_SZ);
299 /* Return if no entry to dequeue from rx_q */
303 /* Burst dequeue mbufs from rx_q */
304 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
308 /* Copy mbufs to sk buffer and then call tx interface */
309 for (i = 0; i < num; i++) {
310 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
312 data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va +
315 skb = dev_alloc_skb(len + 2);
317 KNI_ERR("Out of mem, dropping pkts\n");
319 /* Align IP on 16B boundary */
321 memcpy(skb_put(skb, len), data_kva, len);
323 skb->ip_summed = CHECKSUM_UNNECESSARY;
327 /* Simulate real usage, allocate/copy skb twice */
328 skb = dev_alloc_skb(len + 2);
330 KNI_ERR("Out of mem, dropping pkts\n");
331 kni->stats.rx_dropped++;
334 /* Align IP on 16B boundary */
336 memcpy(skb_put(skb, len), data_kva, len);
338 skb->ip_summed = CHECKSUM_UNNECESSARY;
340 kni->stats.rx_bytes += len;
341 kni->stats.rx_packets++;
343 /* call tx interface */
344 kni_net_tx(skb, dev);
348 /* enqueue all the mbufs from rx_q into free_q */
349 ret = kni_fifo_put(kni->free_q, (void **)&va, num);
351 /* Failing should not happen */
352 KNI_ERR("Fail to enqueue mbufs into free_q\n");
357 kni_net_rx(struct kni_dev *kni)
360 * It doesn't need to check if it is NULL pointer,
361 * as it has a default value
363 (*kni_net_rx_func)(kni);
367 * Transmit a packet (called by the kernel)
371 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
373 struct kni_dev *kni = netdev_priv(dev);
376 kni->stats.tx_dropped++;
382 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
386 struct kni_dev *kni = netdev_priv(dev);
387 struct rte_kni_mbuf *pkt_kva = NULL;
388 struct rte_kni_mbuf *pkt_va = NULL;
390 dev->trans_start = jiffies; /* save the timestamp */
392 /* Check if the length of skb is less than mbuf size */
393 if (skb->len > kni->mbuf_size)
397 * Check if it has at least one free entry in tx_q and
398 * one entry in alloc_q.
400 if (kni_fifo_free_count(kni->tx_q) == 0 ||
401 kni_fifo_count(kni->alloc_q) == 0) {
403 * If no free entry in tx_q or no entry in alloc_q,
404 * drops skb and goes out.
409 /* dequeue a mbuf from alloc_q */
410 ret = kni_fifo_get(kni->alloc_q, (void **)&pkt_va, 1);
411 if (likely(ret == 1)) {
414 pkt_kva = (void *)pkt_va - kni->mbuf_va + kni->mbuf_kva;
415 data_kva = pkt_kva->buf_addr + pkt_kva->data_off - kni->mbuf_va
419 memcpy(data_kva, skb->data, len);
420 if (unlikely(len < ETH_ZLEN)) {
421 memset(data_kva + len, 0, ETH_ZLEN - len);
424 pkt_kva->pkt_len = len;
425 pkt_kva->data_len = len;
427 /* enqueue mbuf into tx_q */
428 ret = kni_fifo_put(kni->tx_q, (void **)&pkt_va, 1);
429 if (unlikely(ret != 1)) {
430 /* Failing should not happen */
431 KNI_ERR("Fail to enqueue mbuf into tx_q\n");
435 /* Failing should not happen */
436 KNI_ERR("Fail to dequeue mbuf from alloc_q\n");
440 /* Free skb and update statistics */
442 kni->stats.tx_bytes += len;
443 kni->stats.tx_packets++;
448 /* Free skb and update statistics */
450 kni->stats.tx_dropped++;
457 * Deal with a transmit timeout.
460 kni_net_tx_timeout (struct net_device *dev)
462 struct kni_dev *kni = netdev_priv(dev);
464 KNI_DBG("Transmit timeout at %ld, latency %ld\n", jiffies,
465 jiffies - dev->trans_start);
467 kni->stats.tx_errors++;
468 netif_wake_queue(dev);
476 kni_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
478 KNI_DBG("kni_net_ioctl %d\n",
479 ((struct kni_dev *)netdev_priv(dev))->group_id);
485 kni_net_set_rx_mode(struct net_device *dev)
490 kni_net_change_mtu(struct net_device *dev, int new_mtu)
493 struct rte_kni_request req;
494 struct kni_dev *kni = netdev_priv(dev);
496 KNI_DBG("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
498 memset(&req, 0, sizeof(req));
499 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
500 req.new_mtu = new_mtu;
501 ret = kni_net_process_request(kni, &req);
502 if (ret == 0 && req.result == 0)
505 return (ret == 0) ? req.result : ret;
509 * Checks if the user space application provided the resp message
512 kni_net_poll_resp(struct kni_dev *kni)
514 if (kni_fifo_count(kni->resp_q))
515 wake_up_interruptible(&kni->wq);
519 * It can be called to process the request.
522 kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
530 KNI_ERR("No kni instance or request\n");
534 mutex_lock(&kni->sync_lock);
537 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
538 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
540 KNI_ERR("Cannot send to req_q\n");
545 ret_val = wait_event_interruptible_timeout(kni->wq,
546 kni_fifo_count(kni->resp_q), 3 * HZ);
547 if (signal_pending(current) || ret_val <= 0) {
551 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
552 if (num != 1 || resp_va != kni->sync_va) {
553 /* This should never happen */
554 KNI_ERR("No data in resp_q\n");
559 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
563 mutex_unlock(&kni->sync_lock);
568 * Return statistics to the caller
570 static struct net_device_stats *
571 kni_net_stats(struct net_device *dev)
573 struct kni_dev *kni = netdev_priv(dev);
578 * Fill the eth header
581 kni_net_header(struct sk_buff *skb, struct net_device *dev,
582 unsigned short type, const void *daddr,
583 const void *saddr, unsigned int len)
585 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
587 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
588 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
589 eth->h_proto = htons(type);
591 return dev->hard_header_len;
596 * Re-fill the eth header
598 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0))
600 kni_net_rebuild_header(struct sk_buff *skb)
602 struct net_device *dev = skb->dev;
603 struct ethhdr *eth = (struct ethhdr *) skb->data;
605 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
606 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
613 * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
614 * @netdev: network interface device structure
615 * @p: pointer to an address structure
617 * Returns 0 on success, negative on failure
619 static int kni_net_set_mac(struct net_device *netdev, void *p)
621 struct sockaddr *addr = p;
622 if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
623 return -EADDRNOTAVAIL;
624 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
628 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
629 static int kni_net_change_carrier(struct net_device *dev, bool new_carrier)
632 netif_carrier_on(dev);
634 netif_carrier_off(dev);
639 static const struct header_ops kni_net_header_ops = {
640 .create = kni_net_header,
641 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0))
642 .rebuild = kni_net_rebuild_header,
644 .cache = NULL, /* disable caching */
647 static const struct net_device_ops kni_net_netdev_ops = {
648 .ndo_open = kni_net_open,
649 .ndo_stop = kni_net_release,
650 .ndo_set_config = kni_net_config,
651 .ndo_start_xmit = kni_net_tx,
652 .ndo_change_mtu = kni_net_change_mtu,
653 .ndo_do_ioctl = kni_net_ioctl,
654 .ndo_set_rx_mode = kni_net_set_rx_mode,
655 .ndo_get_stats = kni_net_stats,
656 .ndo_tx_timeout = kni_net_tx_timeout,
657 .ndo_set_mac_address = kni_net_set_mac,
658 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
659 .ndo_change_carrier = kni_net_change_carrier,
664 kni_net_init(struct net_device *dev)
666 struct kni_dev *kni = netdev_priv(dev);
668 KNI_DBG("kni_net_init\n");
670 init_waitqueue_head(&kni->wq);
671 mutex_init(&kni->sync_lock);
673 ether_setup(dev); /* assign some of the fields */
674 dev->netdev_ops = &kni_net_netdev_ops;
675 dev->header_ops = &kni_net_header_ops;
676 dev->watchdog_timeo = WD_TIMEOUT;
680 kni_net_config_lo_mode(char *lo_str)
683 KNI_PRINT("loopback disabled");
687 if (!strcmp(lo_str, "lo_mode_none"))
688 KNI_PRINT("loopback disabled");
689 else if (!strcmp(lo_str, "lo_mode_fifo")) {
690 KNI_PRINT("loopback mode=lo_mode_fifo enabled");
691 kni_net_rx_func = kni_net_rx_lo_fifo;
692 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
693 KNI_PRINT("loopback mode=lo_mode_fifo_skb enabled");
694 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
696 KNI_PRINT("Incognizant parameter, loopback disabled");