4 * Copyright(c) 2010-2013 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, num_rq, 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 entries in rx_q */
143 num_rq = kni_fifo_count(kni->rx_q);
145 /* Get the number of free entries in free_q */
146 num_fq = kni_fifo_free_count(kni->free_q);
148 /* Calculate the number of entries to dequeue in rx_q */
149 num = min(num_rq, num_fq);
150 num = min(num, (unsigned)MBUF_BURST_SZ);
152 /* Return if no entry in rx_q and no free entry in free_q */
156 /* Burst dequeue from rx_q */
157 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
159 return; /* Failing should not happen */
161 /* Transfer received packets to netif */
162 for (i = 0; i < num; i++) {
163 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
165 data_kva = kva->data - kni->mbuf_va + kni->mbuf_kva;
167 skb = dev_alloc_skb(len + 2);
169 KNI_ERR("Out of mem, dropping pkts\n");
170 /* Update statistics */
171 kni->stats.rx_dropped++;
174 /* Align IP on 16B boundary */
176 memcpy(skb_put(skb, len), data_kva, len);
178 skb->protocol = eth_type_trans(skb, dev);
179 skb->ip_summed = CHECKSUM_UNNECESSARY;
181 /* Call netif interface */
182 netif_receive_skb(skb);
184 /* Update statistics */
185 kni->stats.rx_bytes += len;
186 kni->stats.rx_packets++;
190 /* Burst enqueue mbufs into free_q */
191 ret = kni_fifo_put(kni->free_q, (void **)va, num);
193 /* Failing should not happen */
194 KNI_ERR("Fail to enqueue entries into free_q\n");
198 * RX: loopback with enqueue/dequeue fifos.
201 kni_net_rx_lo_fifo(struct kni_dev *kni)
205 unsigned i, num, num_rq, num_tq, num_aq, num_fq;
206 struct rte_kni_mbuf *kva;
207 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
210 struct rte_kni_mbuf *alloc_kva;
211 struct rte_kni_mbuf *alloc_va[MBUF_BURST_SZ];
212 void *alloc_data_kva;
214 /* Get the number of entries in rx_q */
215 num_rq = kni_fifo_count(kni->rx_q);
217 /* Get the number of free entrie in tx_q */
218 num_tq = kni_fifo_free_count(kni->tx_q);
220 /* Get the number of entries in alloc_q */
221 num_aq = kni_fifo_count(kni->alloc_q);
223 /* Get the number of free entries in free_q */
224 num_fq = kni_fifo_free_count(kni->free_q);
226 /* Calculate the number of entries to be dequeued from rx_q */
227 num = min(num_rq, num_tq);
228 num = min(num, num_aq);
229 num = min(num, num_fq);
230 num = min(num, (unsigned)MBUF_BURST_SZ);
232 /* Return if no entry to dequeue from rx_q */
236 /* Burst dequeue from rx_q */
237 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
239 return; /* Failing should not happen */
241 /* Dequeue entries from alloc_q */
242 ret = kni_fifo_get(kni->alloc_q, (void **)alloc_va, num);
246 for (i = 0; i < num; i++) {
247 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
249 data_kva = kva->data - kni->mbuf_va +
252 alloc_kva = (void *)alloc_va[i] - kni->mbuf_va +
254 alloc_data_kva = alloc_kva->data - kni->mbuf_va +
256 memcpy(alloc_data_kva, data_kva, len);
257 alloc_kva->pkt_len = len;
258 alloc_kva->data_len = len;
260 kni->stats.tx_bytes += len;
261 kni->stats.rx_bytes += len;
264 /* Burst enqueue mbufs into tx_q */
265 ret = kni_fifo_put(kni->tx_q, (void **)alloc_va, num);
267 /* Failing should not happen */
268 KNI_ERR("Fail to enqueue mbufs into tx_q\n");
271 /* Burst enqueue mbufs into free_q */
272 ret = kni_fifo_put(kni->free_q, (void **)va, num);
274 /* Failing should not happen */
275 KNI_ERR("Fail to enqueue mbufs into free_q\n");
278 * Update statistic, and enqueue/dequeue failure is impossible,
279 * as all queues are checked at first.
281 kni->stats.tx_packets += num;
282 kni->stats.rx_packets += num;
286 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
289 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
293 unsigned i, num_rq, num_fq, num;
294 struct rte_kni_mbuf *kva;
295 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
299 struct net_device *dev = kni->net_dev;
301 /* Get the number of entries in rx_q */
302 num_rq = kni_fifo_count(kni->rx_q);
304 /* Get the number of free entries in free_q */
305 num_fq = kni_fifo_free_count(kni->free_q);
307 /* Calculate the number of entries to dequeue from rx_q */
308 num = min(num_rq, num_fq);
309 num = min(num, (unsigned)MBUF_BURST_SZ);
311 /* Return if no entry to dequeue from rx_q */
315 /* Burst dequeue mbufs from rx_q */
316 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
320 /* Copy mbufs to sk buffer and then call tx interface */
321 for (i = 0; i < num; i++) {
322 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
324 data_kva = kva->data - kni->mbuf_va + kni->mbuf_kva;
326 skb = dev_alloc_skb(len + 2);
328 KNI_ERR("Out of mem, dropping pkts\n");
330 /* Align IP on 16B boundary */
332 memcpy(skb_put(skb, len), data_kva, len);
334 skb->protocol = eth_type_trans(skb, dev);
335 skb->ip_summed = CHECKSUM_UNNECESSARY;
339 /* Simulate real usage, allocate/copy skb twice */
340 skb = dev_alloc_skb(len + 2);
342 KNI_ERR("Out of mem, dropping pkts\n");
343 kni->stats.rx_dropped++;
346 /* Align IP on 16B boundary */
348 memcpy(skb_put(skb, len), data_kva, len);
350 skb->protocol = eth_type_trans(skb, dev);
351 skb->ip_summed = CHECKSUM_UNNECESSARY;
353 kni->stats.rx_bytes += len;
354 kni->stats.rx_packets++;
356 /* call tx interface */
357 kni_net_tx(skb, dev);
361 /* enqueue all the mbufs from rx_q into free_q */
362 ret = kni_fifo_put(kni->free_q, (void **)&va, num);
364 /* Failing should not happen */
365 KNI_ERR("Fail to enqueue mbufs into free_q\n");
370 kni_net_rx(struct kni_dev *kni)
373 * It doesn't need to check if it is NULL pointer,
374 * as it has a default value
376 (*kni_net_rx_func)(kni);
380 * Transmit a packet (called by the kernel)
384 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
386 struct kni_dev *kni = netdev_priv(dev);
389 kni->stats.tx_dropped++;
395 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
399 struct kni_dev *kni = netdev_priv(dev);
400 struct rte_kni_mbuf *pkt_kva = NULL;
401 struct rte_kni_mbuf *pkt_va = NULL;
403 dev->trans_start = jiffies; /* save the timestamp */
405 /* Check if the length of skb is less than mbuf size */
406 if (skb->len > kni->mbuf_size)
410 * Check if it has at least one free entry in tx_q and
411 * one entry in alloc_q.
413 if (kni_fifo_free_count(kni->tx_q) == 0 ||
414 kni_fifo_count(kni->alloc_q) == 0) {
416 * If no free entry in tx_q or no entry in alloc_q,
417 * drops skb and goes out.
422 /* dequeue a mbuf from alloc_q */
423 ret = kni_fifo_get(kni->alloc_q, (void **)&pkt_va, 1);
424 if (likely(ret == 1)) {
427 pkt_kva = (void *)pkt_va - kni->mbuf_va + kni->mbuf_kva;
428 data_kva = pkt_kva->data - kni->mbuf_va + kni->mbuf_kva;
431 memcpy(data_kva, skb->data, len);
432 if (unlikely(len < ETH_ZLEN)) {
433 memset(data_kva + len, 0, ETH_ZLEN - len);
436 pkt_kva->pkt_len = len;
437 pkt_kva->data_len = len;
439 /* enqueue mbuf into tx_q */
440 ret = kni_fifo_put(kni->tx_q, (void **)&pkt_va, 1);
441 if (unlikely(ret != 1)) {
442 /* Failing should not happen */
443 KNI_ERR("Fail to enqueue mbuf into tx_q\n");
447 /* Failing should not happen */
448 KNI_ERR("Fail to dequeue mbuf from alloc_q\n");
452 /* Free skb and update statistics */
454 kni->stats.tx_bytes += len;
455 kni->stats.tx_packets++;
460 /* Free skb and update statistics */
462 kni->stats.tx_dropped++;
469 * Deal with a transmit timeout.
472 kni_net_tx_timeout (struct net_device *dev)
474 struct kni_dev *kni = netdev_priv(dev);
476 KNI_DBG("Transmit timeout at %ld, latency %ld\n", jiffies,
477 jiffies - dev->trans_start);
479 kni->stats.tx_errors++;
480 netif_wake_queue(dev);
488 kni_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
490 KNI_DBG("kni_net_ioctl %d\n",
491 ((struct kni_dev *)netdev_priv(dev))->group_id);
497 kni_net_change_mtu(struct net_device *dev, int new_mtu)
500 struct rte_kni_request req;
501 struct kni_dev *kni = netdev_priv(dev);
503 KNI_DBG("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
505 memset(&req, 0, sizeof(req));
506 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
507 req.new_mtu = new_mtu;
508 ret = kni_net_process_request(kni, &req);
509 if (ret == 0 && req.result == 0)
512 return (ret == 0 ? req.result : ret);
516 * Checks if the user space application provided the resp message
519 kni_net_poll_resp(struct kni_dev *kni)
521 if (kni_fifo_count(kni->resp_q))
522 wake_up_interruptible(&kni->wq);
526 * It can be called to process the request.
529 kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
537 KNI_ERR("No kni instance or request\n");
541 mutex_lock(&kni->sync_lock);
544 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
545 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
547 KNI_ERR("Cannot send to req_q\n");
552 ret_val = wait_event_interruptible_timeout(kni->wq,
553 kni_fifo_count(kni->resp_q), 3 * HZ);
554 if (signal_pending(current) || ret_val <= 0) {
558 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
559 if (num != 1 || resp_va != kni->sync_va) {
560 /* This should never happen */
561 KNI_ERR("No data in resp_q\n");
566 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
570 mutex_unlock(&kni->sync_lock);
575 * Return statistics to the caller
577 static struct net_device_stats *
578 kni_net_stats(struct net_device *dev)
580 struct kni_dev *kni = netdev_priv(dev);
585 * Fill the eth header
588 kni_net_header(struct sk_buff *skb, struct net_device *dev,
589 unsigned short type, const void *daddr,
590 const void *saddr, unsigned int len)
592 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
594 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
595 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
596 eth->h_proto = htons(type);
598 return (dev->hard_header_len);
603 * Re-fill the eth header
606 kni_net_rebuild_header(struct sk_buff *skb)
608 struct net_device *dev = skb->dev;
609 struct ethhdr *eth = (struct ethhdr *) skb->data;
611 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
612 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
618 static const struct header_ops kni_net_header_ops = {
619 .create = kni_net_header,
620 .rebuild = kni_net_rebuild_header,
621 .cache = NULL, /* disable caching */
624 static const struct net_device_ops kni_net_netdev_ops = {
625 .ndo_open = kni_net_open,
626 .ndo_stop = kni_net_release,
627 .ndo_set_config = kni_net_config,
628 .ndo_start_xmit = kni_net_tx,
629 .ndo_change_mtu = kni_net_change_mtu,
630 .ndo_do_ioctl = kni_net_ioctl,
631 .ndo_get_stats = kni_net_stats,
632 .ndo_tx_timeout = kni_net_tx_timeout,
636 kni_net_init(struct net_device *dev)
638 struct kni_dev *kni = netdev_priv(dev);
640 KNI_DBG("kni_net_init\n");
642 init_waitqueue_head(&kni->wq);
643 mutex_init(&kni->sync_lock);
645 ether_setup(dev); /* assign some of the fields */
646 dev->netdev_ops = &kni_net_netdev_ops;
647 dev->header_ops = &kni_net_header_ops;
648 dev->watchdog_timeo = WD_TIMEOUT;
652 kni_net_config_lo_mode(char *lo_str)
655 KNI_PRINT("loopback disabled");
659 if (!strcmp(lo_str, "lo_mode_none"))
660 KNI_PRINT("loopback disabled");
661 else if (!strcmp(lo_str, "lo_mode_fifo")) {
662 KNI_PRINT("loopback mode=lo_mode_fifo enabled");
663 kni_net_rx_func = kni_net_rx_lo_fifo;
664 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
665 KNI_PRINT("loopback mode=lo_mode_fifo_skb enabled");
666 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
668 KNI_PRINT("Incognizant parameter, loopback disabled");