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 <rte_config.h>
40 #include <exec-env/rte_kni_common.h>
44 #define WD_TIMEOUT 5 /*jiffies */
46 #define MBUF_BURST_SZ 32
48 #define KNI_WAIT_RESPONSE_TIMEOUT 300 /* 3 seconds */
50 /* typedef for rx function */
51 typedef void (*kni_net_rx_t)(struct kni_dev *kni);
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);
60 /* kni rx function pointer, with default to normal rx */
61 static kni_net_rx_t kni_net_rx_func = kni_net_rx_normal;
67 kni_net_open(struct net_device *dev)
70 struct rte_kni_request req;
71 struct kni_dev *kni = netdev_priv(dev);
74 memcpy(dev->dev_addr, kni->lad_dev->dev_addr, ETH_ALEN);
77 * Generate random mac address. eth_random_addr() is the newer
78 * version of generating mac address in linux kernel.
80 random_ether_addr(dev->dev_addr);
82 netif_start_queue(dev);
84 memset(&req, 0, sizeof(req));
85 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
87 /* Setting if_up to non-zero means up */
89 ret = kni_net_process_request(kni, &req);
91 return (ret == 0 ? req.result : ret);
95 kni_net_release(struct net_device *dev)
98 struct rte_kni_request req;
99 struct kni_dev *kni = netdev_priv(dev);
101 netif_stop_queue(dev); /* can't transmit any more */
103 memset(&req, 0, sizeof(req));
104 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
106 /* Setting if_up to 0 means down */
108 ret = kni_net_process_request(kni, &req);
110 return (ret == 0 ? req.result : ret);
114 * Configuration changes (passed on by ifconfig)
117 kni_net_config(struct net_device *dev, struct ifmap *map)
119 if (dev->flags & IFF_UP) /* can't act on a running interface */
122 /* ignore other fields */
127 * RX: normal working mode
130 kni_net_rx_normal(struct kni_dev *kni)
134 unsigned i, num_rx, num_fq;
135 struct rte_kni_mbuf *kva;
136 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
140 struct net_device *dev = kni->net_dev;
142 /* Get the number of free entries in free_q */
143 num_fq = kni_fifo_free_count(kni->free_q);
145 /* No room on the free_q, bail out */
149 /* Calculate the number of entries to dequeue from rx_q */
150 num_rx = min(num_fq, (unsigned)MBUF_BURST_SZ);
152 /* Burst dequeue from rx_q */
153 num_rx = kni_fifo_get(kni->rx_q, (void **)va, num_rx);
157 /* Transfer received packets to netif */
158 for (i = 0; i < num_rx; i++) {
159 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
161 data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va
164 skb = dev_alloc_skb(len + 2);
166 KNI_ERR("Out of mem, dropping pkts\n");
167 /* Update statistics */
168 kni->stats.rx_dropped++;
171 /* Align IP on 16B boundary */
173 memcpy(skb_put(skb, len), data_kva, len);
175 skb->protocol = eth_type_trans(skb, dev);
176 skb->ip_summed = CHECKSUM_UNNECESSARY;
178 /* Call netif interface */
181 /* Update statistics */
182 kni->stats.rx_bytes += len;
183 kni->stats.rx_packets++;
187 /* Burst enqueue mbufs into free_q */
188 ret = kni_fifo_put(kni->free_q, (void **)va, num_rx);
190 /* Failing should not happen */
191 KNI_ERR("Fail to enqueue entries into free_q\n");
195 * RX: loopback with enqueue/dequeue fifos.
198 kni_net_rx_lo_fifo(struct kni_dev *kni)
202 unsigned i, num, num_rq, num_tq, num_aq, num_fq;
203 struct rte_kni_mbuf *kva;
204 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
207 struct rte_kni_mbuf *alloc_kva;
208 struct rte_kni_mbuf *alloc_va[MBUF_BURST_SZ];
209 void *alloc_data_kva;
211 /* Get the number of entries in rx_q */
212 num_rq = kni_fifo_count(kni->rx_q);
214 /* Get the number of free entrie in tx_q */
215 num_tq = kni_fifo_free_count(kni->tx_q);
217 /* Get the number of entries in alloc_q */
218 num_aq = kni_fifo_count(kni->alloc_q);
220 /* Get the number of free entries in free_q */
221 num_fq = kni_fifo_free_count(kni->free_q);
223 /* Calculate the number of entries to be dequeued from rx_q */
224 num = min(num_rq, num_tq);
225 num = min(num, num_aq);
226 num = min(num, num_fq);
227 num = min(num, (unsigned)MBUF_BURST_SZ);
229 /* Return if no entry to dequeue from rx_q */
233 /* Burst dequeue from rx_q */
234 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
236 return; /* Failing should not happen */
238 /* Dequeue entries from alloc_q */
239 ret = kni_fifo_get(kni->alloc_q, (void **)alloc_va, num);
243 for (i = 0; i < num; i++) {
244 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
246 data_kva = kva->buf_addr + kva->data_off -
247 kni->mbuf_va + kni->mbuf_kva;
249 alloc_kva = (void *)alloc_va[i] - kni->mbuf_va +
251 alloc_data_kva = alloc_kva->buf_addr +
252 alloc_kva->data_off - kni->mbuf_va +
254 memcpy(alloc_data_kva, data_kva, len);
255 alloc_kva->pkt_len = len;
256 alloc_kva->data_len = len;
258 kni->stats.tx_bytes += len;
259 kni->stats.rx_bytes += len;
262 /* Burst enqueue mbufs into tx_q */
263 ret = kni_fifo_put(kni->tx_q, (void **)alloc_va, num);
265 /* Failing should not happen */
266 KNI_ERR("Fail to enqueue mbufs into tx_q\n");
269 /* Burst enqueue mbufs into free_q */
270 ret = kni_fifo_put(kni->free_q, (void **)va, num);
272 /* Failing should not happen */
273 KNI_ERR("Fail to enqueue mbufs into free_q\n");
276 * Update statistic, and enqueue/dequeue failure is impossible,
277 * as all queues are checked at first.
279 kni->stats.tx_packets += num;
280 kni->stats.rx_packets += num;
284 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
287 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
291 unsigned i, num_rq, num_fq, num;
292 struct rte_kni_mbuf *kva;
293 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
297 struct net_device *dev = kni->net_dev;
299 /* Get the number of entries in rx_q */
300 num_rq = kni_fifo_count(kni->rx_q);
302 /* Get the number of free entries in free_q */
303 num_fq = kni_fifo_free_count(kni->free_q);
305 /* Calculate the number of entries to dequeue from rx_q */
306 num = min(num_rq, num_fq);
307 num = min(num, (unsigned)MBUF_BURST_SZ);
309 /* Return if no entry to dequeue from rx_q */
313 /* Burst dequeue mbufs from rx_q */
314 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
318 /* Copy mbufs to sk buffer and then call tx interface */
319 for (i = 0; i < num; i++) {
320 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
322 data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va +
325 skb = dev_alloc_skb(len + 2);
327 KNI_ERR("Out of mem, dropping pkts\n");
329 /* Align IP on 16B boundary */
331 memcpy(skb_put(skb, len), data_kva, len);
333 skb->ip_summed = CHECKSUM_UNNECESSARY;
337 /* Simulate real usage, allocate/copy skb twice */
338 skb = dev_alloc_skb(len + 2);
340 KNI_ERR("Out of mem, dropping pkts\n");
341 kni->stats.rx_dropped++;
344 /* Align IP on 16B boundary */
346 memcpy(skb_put(skb, len), data_kva, len);
348 skb->ip_summed = CHECKSUM_UNNECESSARY;
350 kni->stats.rx_bytes += len;
351 kni->stats.rx_packets++;
353 /* call tx interface */
354 kni_net_tx(skb, dev);
358 /* enqueue all the mbufs from rx_q into free_q */
359 ret = kni_fifo_put(kni->free_q, (void **)&va, num);
361 /* Failing should not happen */
362 KNI_ERR("Fail to enqueue mbufs into free_q\n");
367 kni_net_rx(struct kni_dev *kni)
370 * It doesn't need to check if it is NULL pointer,
371 * as it has a default value
373 (*kni_net_rx_func)(kni);
377 * Transmit a packet (called by the kernel)
381 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
383 struct kni_dev *kni = netdev_priv(dev);
386 kni->stats.tx_dropped++;
392 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
396 struct kni_dev *kni = netdev_priv(dev);
397 struct rte_kni_mbuf *pkt_kva = NULL;
398 struct rte_kni_mbuf *pkt_va = NULL;
400 dev->trans_start = jiffies; /* save the timestamp */
402 /* Check if the length of skb is less than mbuf size */
403 if (skb->len > kni->mbuf_size)
407 * Check if it has at least one free entry in tx_q and
408 * one entry in alloc_q.
410 if (kni_fifo_free_count(kni->tx_q) == 0 ||
411 kni_fifo_count(kni->alloc_q) == 0) {
413 * If no free entry in tx_q or no entry in alloc_q,
414 * drops skb and goes out.
419 /* dequeue a mbuf from alloc_q */
420 ret = kni_fifo_get(kni->alloc_q, (void **)&pkt_va, 1);
421 if (likely(ret == 1)) {
424 pkt_kva = (void *)pkt_va - kni->mbuf_va + kni->mbuf_kva;
425 data_kva = pkt_kva->buf_addr + pkt_kva->data_off - kni->mbuf_va
429 memcpy(data_kva, skb->data, len);
430 if (unlikely(len < ETH_ZLEN)) {
431 memset(data_kva + len, 0, ETH_ZLEN - len);
434 pkt_kva->pkt_len = len;
435 pkt_kva->data_len = len;
437 /* enqueue mbuf into tx_q */
438 ret = kni_fifo_put(kni->tx_q, (void **)&pkt_va, 1);
439 if (unlikely(ret != 1)) {
440 /* Failing should not happen */
441 KNI_ERR("Fail to enqueue mbuf into tx_q\n");
445 /* Failing should not happen */
446 KNI_ERR("Fail to dequeue mbuf from alloc_q\n");
450 /* Free skb and update statistics */
452 kni->stats.tx_bytes += len;
453 kni->stats.tx_packets++;
458 /* Free skb and update statistics */
460 kni->stats.tx_dropped++;
467 * Deal with a transmit timeout.
470 kni_net_tx_timeout (struct net_device *dev)
472 struct kni_dev *kni = netdev_priv(dev);
474 KNI_DBG("Transmit timeout at %ld, latency %ld\n", jiffies,
475 jiffies - dev->trans_start);
477 kni->stats.tx_errors++;
478 netif_wake_queue(dev);
486 kni_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
488 KNI_DBG("kni_net_ioctl %d\n",
489 ((struct kni_dev *)netdev_priv(dev))->group_id);
495 kni_net_change_mtu(struct net_device *dev, int new_mtu)
498 struct rte_kni_request req;
499 struct kni_dev *kni = netdev_priv(dev);
501 KNI_DBG("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
503 memset(&req, 0, sizeof(req));
504 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
505 req.new_mtu = new_mtu;
506 ret = kni_net_process_request(kni, &req);
507 if (ret == 0 && req.result == 0)
510 return (ret == 0 ? req.result : ret);
514 * Checks if the user space application provided the resp message
517 kni_net_poll_resp(struct kni_dev *kni)
519 if (kni_fifo_count(kni->resp_q))
520 wake_up_interruptible(&kni->wq);
524 * It can be called to process the request.
527 kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
535 KNI_ERR("No kni instance or request\n");
539 mutex_lock(&kni->sync_lock);
542 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
543 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
545 KNI_ERR("Cannot send to req_q\n");
550 ret_val = wait_event_interruptible_timeout(kni->wq,
551 kni_fifo_count(kni->resp_q), 3 * HZ);
552 if (signal_pending(current) || ret_val <= 0) {
556 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
557 if (num != 1 || resp_va != kni->sync_va) {
558 /* This should never happen */
559 KNI_ERR("No data in resp_q\n");
564 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
568 mutex_unlock(&kni->sync_lock);
573 * Return statistics to the caller
575 static struct net_device_stats *
576 kni_net_stats(struct net_device *dev)
578 struct kni_dev *kni = netdev_priv(dev);
583 * Fill the eth header
586 kni_net_header(struct sk_buff *skb, struct net_device *dev,
587 unsigned short type, const void *daddr,
588 const void *saddr, unsigned int len)
590 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
592 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
593 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
594 eth->h_proto = htons(type);
596 return (dev->hard_header_len);
601 * Re-fill the eth header
604 kni_net_rebuild_header(struct sk_buff *skb)
606 struct net_device *dev = skb->dev;
607 struct ethhdr *eth = (struct ethhdr *) skb->data;
609 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
610 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
616 * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
617 * @netdev: network interface device structure
618 * @p: pointer to an address structure
620 * Returns 0 on success, negative on failure
622 static int kni_net_set_mac(struct net_device *netdev, void *p)
624 struct sockaddr *addr = p;
625 if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
626 return -EADDRNOTAVAIL;
627 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
631 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
632 static int kni_net_change_carrier(struct net_device *dev, bool new_carrier)
635 netif_carrier_on(dev);
637 netif_carrier_off(dev);
642 static const struct header_ops kni_net_header_ops = {
643 .create = kni_net_header,
644 .rebuild = kni_net_rebuild_header,
645 .cache = NULL, /* disable caching */
648 static const struct net_device_ops kni_net_netdev_ops = {
649 .ndo_open = kni_net_open,
650 .ndo_stop = kni_net_release,
651 .ndo_set_config = kni_net_config,
652 .ndo_start_xmit = kni_net_tx,
653 .ndo_change_mtu = kni_net_change_mtu,
654 .ndo_do_ioctl = kni_net_ioctl,
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");