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:
27 * This code is inspired from the book "Linux Device Drivers" by
28 * Alessandro Rubini and Jonathan Corbet, published by O'Reilly & Associates
31 #include <linux/device.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h> /* eth_type_trans */
36 #include <linux/skbuff.h>
37 #include <linux/kthread.h>
38 #include <linux/delay.h>
40 #include <rte_config.h>
41 #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;
65 * Adds num elements into the fifo. Return the number actually written
67 static inline unsigned
68 kni_fifo_put(struct rte_kni_fifo *fifo, void **data, unsigned num)
71 unsigned fifo_write = fifo->write;
72 unsigned fifo_read = fifo->read;
73 unsigned new_write = fifo_write;
75 for (i = 0; i < num; i++) {
76 new_write = (new_write + 1) & (fifo->len - 1);
78 if (new_write == fifo_read)
80 fifo->buffer[fifo_write] = data[i];
81 fifo_write = new_write;
83 fifo->write = fifo_write;
88 * Get up to num elements from the fifo. Return the number actully read
90 static inline unsigned
91 kni_fifo_get(struct rte_kni_fifo *fifo, void **data, unsigned num)
94 unsigned new_read = fifo->read;
95 unsigned fifo_write = fifo->write;
96 for (i = 0; i < num; i++) {
97 if (new_read == fifo_write)
100 data[i] = fifo->buffer[new_read];
101 new_read = (new_read + 1) & (fifo->len - 1);
103 fifo->read = new_read;
108 * Get the num of elements in the fifo
110 static inline unsigned
111 kni_fifo_count(struct rte_kni_fifo *fifo)
113 return (fifo->len + fifo->write - fifo->read) &( fifo->len - 1);
117 * Get the num of available lements in the fifo
119 static inline unsigned
120 kni_fifo_free_count(struct rte_kni_fifo *fifo)
122 return (fifo->read - fifo->write - 1) & (fifo->len - 1);
129 kni_net_open(struct net_device *dev)
132 struct rte_kni_request req;
133 struct kni_dev *kni = netdev_priv(dev);
135 KNI_DBG("kni_net_open %d\n", kni->port_id);
138 * Assign the hardware address of the board: use "\0KNIx", where
139 * x is KNI index. The first byte is '\0' to avoid being a multicast
140 * address (the first byte of multicast addrs is odd).
144 memcpy(dev->dev_addr, kni->lad_dev->dev_addr, ETH_ALEN);
146 memcpy(dev->dev_addr, "\0KNI0", ETH_ALEN);
147 dev->dev_addr[ETH_ALEN-1] += kni->port_id; /* \0KNI1 */
150 netif_start_queue(dev);
152 memset(&req, 0, sizeof(req));
153 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
155 /* Setting if_up to non-zero means up */
157 ret = kni_net_process_request(kni, &req);
159 return (ret == 0 ? req.result : ret);
163 kni_net_release(struct net_device *dev)
166 struct rte_kni_request req;
167 struct kni_dev *kni = netdev_priv(dev);
169 netif_stop_queue(dev); /* can't transmit any more */
171 memset(&req, 0, sizeof(req));
172 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
174 /* Setting if_up to 0 means down */
176 ret = kni_net_process_request(kni, &req);
178 return (ret == 0 ? req.result : ret);
182 * Configuration changes (passed on by ifconfig)
185 kni_net_config(struct net_device *dev, struct ifmap *map)
187 if (dev->flags & IFF_UP) /* can't act on a running interface */
190 /* ignore other fields */
195 * RX: normal working mode
198 kni_net_rx_normal(struct kni_dev *kni)
202 unsigned i, num, num_rq, num_fq;
203 struct rte_kni_mbuf *kva;
204 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
208 struct net_device *dev = kni->net_dev;
210 /* Get the number of entries in rx_q */
211 num_rq = kni_fifo_count(kni->rx_q);
213 /* Get the number of free entries in free_q */
214 num_fq = kni_fifo_free_count(kni->free_q);
216 /* Calculate the number of entries to dequeue in rx_q */
217 num = min(num_rq, num_fq);
218 num = min(num, (unsigned)MBUF_BURST_SZ);
220 /* Return if no entry in rx_q and no free entry in free_q */
224 /* Burst dequeue from rx_q */
225 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
227 return; /* Failing should not happen */
229 /* Transfer received packets to netif */
230 for (i = 0; i < num; i++) {
231 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
233 data_kva = kva->data - kni->mbuf_va + kni->mbuf_kva;
235 skb = dev_alloc_skb(len + 2);
237 KNI_ERR("Out of mem, dropping pkts\n");
238 /* Update statistics */
239 kni->stats.rx_dropped++;
242 /* Align IP on 16B boundary */
244 memcpy(skb_put(skb, len), data_kva, len);
246 skb->protocol = eth_type_trans(skb, dev);
247 skb->ip_summed = CHECKSUM_UNNECESSARY;
249 /* Call netif interface */
250 netif_receive_skb(skb);
252 /* Update statistics */
253 kni->stats.rx_bytes += len;
254 kni->stats.rx_packets++;
258 /* Burst enqueue mbufs into free_q */
259 ret = kni_fifo_put(kni->free_q, (void **)va, num);
261 /* Failing should not happen */
262 KNI_ERR("Fail to enqueue entries into free_q\n");
266 * RX: loopback with enqueue/dequeue fifos.
269 kni_net_rx_lo_fifo(struct kni_dev *kni)
273 unsigned i, num, num_rq, num_tq, num_aq, num_fq;
274 struct rte_kni_mbuf *kva;
275 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
278 struct rte_kni_mbuf *alloc_kva;
279 struct rte_kni_mbuf *alloc_va[MBUF_BURST_SZ];
280 void *alloc_data_kva;
282 /* Get the number of entries in rx_q */
283 num_rq = kni_fifo_count(kni->rx_q);
285 /* Get the number of free entrie in tx_q */
286 num_tq = kni_fifo_free_count(kni->tx_q);
288 /* Get the number of entries in alloc_q */
289 num_aq = kni_fifo_count(kni->alloc_q);
291 /* Get the number of free entries in free_q */
292 num_fq = kni_fifo_free_count(kni->free_q);
294 /* Calculate the number of entries to be dequeued from rx_q */
295 num = min(num_rq, num_tq);
296 num = min(num, num_aq);
297 num = min(num, num_fq);
298 num = min(num, (unsigned)MBUF_BURST_SZ);
300 /* Return if no entry to dequeue from rx_q */
304 /* Burst dequeue from rx_q */
305 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
307 return; /* Failing should not happen */
309 /* Dequeue entries from alloc_q */
310 ret = kni_fifo_get(kni->alloc_q, (void **)alloc_va, num);
314 for (i = 0; i < num; i++) {
315 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
317 data_kva = kva->data - kni->mbuf_va +
320 alloc_kva = (void *)alloc_va[i] - kni->mbuf_va +
322 alloc_data_kva = alloc_kva->data - kni->mbuf_va +
324 memcpy(alloc_data_kva, data_kva, len);
325 alloc_kva->pkt_len = len;
326 alloc_kva->data_len = len;
328 kni->stats.tx_bytes += len;
329 kni->stats.rx_bytes += len;
332 /* Burst enqueue mbufs into tx_q */
333 ret = kni_fifo_put(kni->tx_q, (void **)alloc_va, num);
335 /* Failing should not happen */
336 KNI_ERR("Fail to enqueue mbufs into tx_q\n");
339 /* Burst enqueue mbufs into free_q */
340 ret = kni_fifo_put(kni->free_q, (void **)va, num);
342 /* Failing should not happen */
343 KNI_ERR("Fail to enqueue mbufs into free_q\n");
346 * Update statistic, and enqueue/dequeue failure is impossible,
347 * as all queues are checked at first.
349 kni->stats.tx_packets += num;
350 kni->stats.rx_packets += num;
354 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
357 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
361 unsigned i, num_rq, num_fq, num;
362 struct rte_kni_mbuf *kva;
363 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
367 struct net_device *dev = kni->net_dev;
369 /* Get the number of entries in rx_q */
370 num_rq = kni_fifo_count(kni->rx_q);
372 /* Get the number of free entries in free_q */
373 num_fq = kni_fifo_free_count(kni->free_q);
375 /* Calculate the number of entries to dequeue from rx_q */
376 num = min(num_rq, num_fq);
377 num = min(num, (unsigned)MBUF_BURST_SZ);
379 /* Return if no entry to dequeue from rx_q */
383 /* Burst dequeue mbufs from rx_q */
384 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
388 /* Copy mbufs to sk buffer and then call tx interface */
389 for (i = 0; i < num; i++) {
390 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
392 data_kva = kva->data - kni->mbuf_va + kni->mbuf_kva;
394 skb = dev_alloc_skb(len + 2);
396 KNI_ERR("Out of mem, dropping pkts\n");
398 /* Align IP on 16B boundary */
400 memcpy(skb_put(skb, len), data_kva, len);
402 skb->protocol = eth_type_trans(skb, dev);
403 skb->ip_summed = CHECKSUM_UNNECESSARY;
407 /* Simulate real usage, allocate/copy skb twice */
408 skb = dev_alloc_skb(len + 2);
410 KNI_ERR("Out of mem, dropping pkts\n");
411 kni->stats.rx_dropped++;
414 /* Align IP on 16B boundary */
416 memcpy(skb_put(skb, len), data_kva, len);
418 skb->protocol = eth_type_trans(skb, dev);
419 skb->ip_summed = CHECKSUM_UNNECESSARY;
421 kni->stats.rx_bytes += len;
422 kni->stats.rx_packets++;
424 /* call tx interface */
425 kni_net_tx(skb, dev);
429 /* enqueue all the mbufs from rx_q into free_q */
430 ret = kni_fifo_put(kni->free_q, (void **)&va, num);
432 /* Failing should not happen */
433 KNI_ERR("Fail to enqueue mbufs into free_q\n");
438 kni_net_rx(struct kni_dev *kni)
441 * It doesn't need to check if it is NULL pointer,
442 * as it has a default value
444 (*kni_net_rx_func)(kni);
448 * Transmit a packet (called by the kernel)
451 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
455 struct kni_dev *kni = netdev_priv(dev);
456 struct rte_kni_mbuf *pkt_kva = NULL;
457 struct rte_kni_mbuf *pkt_va = NULL;
459 dev->trans_start = jiffies; /* save the timestamp */
461 /* Check if the length of skb is less than mbuf size */
462 if (skb->len > kni->mbuf_size)
466 * Check if it has at least one free entry in tx_q and
467 * one entry in alloc_q.
469 if (kni_fifo_free_count(kni->tx_q) == 0 ||
470 kni_fifo_count(kni->alloc_q) == 0) {
472 * If no free entry in tx_q or no entry in alloc_q,
473 * drops skb and goes out.
478 /* dequeue a mbuf from alloc_q */
479 ret = kni_fifo_get(kni->alloc_q, (void **)&pkt_va, 1);
480 if (likely(ret == 1)) {
483 pkt_kva = (void *)pkt_va - kni->mbuf_va + kni->mbuf_kva;
484 data_kva = pkt_kva->data - kni->mbuf_va + kni->mbuf_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, (void **)&pkt_va, 1);
497 if (unlikely(ret != 1)) {
498 /* Failing should not happen */
499 KNI_ERR("Fail to enqueue mbuf into tx_q\n");
503 /* Failing should not happen */
504 KNI_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++;
524 * Deal with a transmit timeout.
527 kni_net_tx_timeout (struct net_device *dev)
529 struct kni_dev *kni = netdev_priv(dev);
531 KNI_DBG("Transmit timeout at %ld, latency %ld\n", jiffies,
532 jiffies - dev->trans_start);
534 kni->stats.tx_errors++;
535 netif_wake_queue(dev);
543 kni_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
545 struct kni_dev *kni = netdev_priv(dev);
546 KNI_DBG("kni_net_ioctl %d\n", kni->port_id);
552 kni_net_change_mtu(struct net_device *dev, int new_mtu)
555 struct rte_kni_request req;
556 struct kni_dev *kni = netdev_priv(dev);
558 KNI_DBG("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
560 memset(&req, 0, sizeof(req));
561 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
562 req.new_mtu = new_mtu;
563 ret = kni_net_process_request(kni, &req);
564 if (ret == 0 && req.result == 0)
567 return (ret == 0 ? req.result : ret);
571 * Checks if the user space application provided the resp message
574 kni_net_poll_resp(struct kni_dev *kni)
576 int i = kni_fifo_count(kni->resp_q);
579 wake_up_interruptible(&kni->wq);
585 * It can be called to process the request.
588 kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
596 KNI_ERR("No kni instance or request\n");
600 mutex_lock(&kni->sync_lock);
603 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
604 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
606 KNI_ERR("Cannot send to req_q\n");
611 ret_val = wait_event_interruptible_timeout(kni->wq,
612 kni_fifo_count(kni->resp_q), 3 * HZ);
613 if (signal_pending(current) || ret_val <= 0) {
617 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
618 if (num != 1 || resp_va != kni->sync_va) {
619 /* This should never happen */
620 KNI_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);
645 * Fill the eth header
648 kni_net_header(struct sk_buff *skb, struct net_device *dev,
649 unsigned short type, const void *daddr,
650 const void *saddr, unsigned int len)
652 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
654 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
655 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
656 eth->h_proto = htons(type);
658 return (dev->hard_header_len);
663 * Re-fill the eth header
666 kni_net_rebuild_header(struct sk_buff *skb)
668 struct net_device *dev = skb->dev;
669 struct ethhdr *eth = (struct ethhdr *) skb->data;
671 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
672 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
678 static const struct header_ops kni_net_header_ops = {
679 .create = kni_net_header,
680 .rebuild = kni_net_rebuild_header,
681 .cache = NULL, /* disable caching */
684 static const struct net_device_ops kni_net_netdev_ops = {
685 .ndo_open = kni_net_open,
686 .ndo_stop = kni_net_release,
687 .ndo_set_config = kni_net_config,
688 .ndo_start_xmit = kni_net_tx,
689 .ndo_change_mtu = kni_net_change_mtu,
690 .ndo_do_ioctl = kni_net_ioctl,
691 .ndo_get_stats = kni_net_stats,
692 .ndo_tx_timeout = kni_net_tx_timeout,
696 kni_net_init(struct net_device *dev)
698 struct kni_dev *kni = netdev_priv(dev);
700 KNI_DBG("kni_net_init\n");
702 init_waitqueue_head(&kni->wq);
703 mutex_init(&kni->sync_lock);
705 ether_setup(dev); /* assign some of the fields */
706 dev->netdev_ops = &kni_net_netdev_ops;
707 dev->header_ops = &kni_net_header_ops;
708 dev->watchdog_timeo = WD_TIMEOUT;
712 kni_net_config_lo_mode(char *lo_str)
715 KNI_PRINT("loopback disabled");
719 if (!strcmp(lo_str, "lo_mode_none"))
720 KNI_PRINT("loopback disabled");
721 else if (!strcmp(lo_str, "lo_mode_fifo")) {
722 KNI_PRINT("loopback mode=lo_mode_fifo enabled");
723 kni_net_rx_func = kni_net_rx_lo_fifo;
724 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
725 KNI_PRINT("loopback mode=lo_mode_fifo_skb enabled");
726 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
728 KNI_PRINT("Incognizant parameter, loopback disabled");