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>
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;
64 * Adds num elements into the fifo. Return the number actually written
66 static inline unsigned
67 kni_fifo_put(struct rte_kni_fifo *fifo, void **data, unsigned num)
70 unsigned fifo_write = fifo->write;
71 unsigned fifo_read = fifo->read;
72 unsigned new_write = fifo_write;
74 for (i = 0; i < num; i++) {
75 new_write = (new_write + 1) & (fifo->len - 1);
77 if (new_write == fifo_read)
79 fifo->buffer[fifo_write] = data[i];
80 fifo_write = new_write;
82 fifo->write = fifo_write;
87 * Get up to num elements from the fifo. Return the number actully read
89 static inline unsigned
90 kni_fifo_get(struct rte_kni_fifo *fifo, void **data, unsigned num)
93 unsigned new_read = fifo->read;
94 unsigned fifo_write = fifo->write;
95 for (i = 0; i < num; i++) {
96 if (new_read == fifo_write)
99 data[i] = fifo->buffer[new_read];
100 new_read = (new_read + 1) & (fifo->len - 1);
102 fifo->read = new_read;
107 * Get the num of elements in the fifo
109 static inline unsigned
110 kni_fifo_count(struct rte_kni_fifo *fifo)
112 return (fifo->len + fifo->write - fifo->read) &( fifo->len - 1);
116 * Get the num of available lements in the fifo
118 static inline unsigned
119 kni_fifo_free_count(struct rte_kni_fifo *fifo)
121 return (fifo->read - fifo->write - 1) & (fifo->len - 1);
128 kni_net_open(struct net_device *dev)
131 struct rte_kni_request req;
132 struct kni_dev *kni = netdev_priv(dev);
134 KNI_DBG("kni_net_open %d\n", kni->port_id);
137 * Assign the hardware address of the board: use "\0KNIx", where
138 * x is KNI index. The first byte is '\0' to avoid being a multicast
139 * address (the first byte of multicast addrs is odd).
143 memcpy(dev->dev_addr, kni->lad_dev->dev_addr, ETH_ALEN);
145 memcpy(dev->dev_addr, "\0KNI0", ETH_ALEN);
146 dev->dev_addr[ETH_ALEN-1] += kni->port_id; /* \0KNI1 */
149 netif_start_queue(dev);
151 memset(&req, 0, sizeof(req));
152 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
154 /* Setting if_up to non-zero means up */
156 ret = kni_net_process_request(kni, &req);
158 return (ret == 0 ? req.result : ret);
162 kni_net_release(struct net_device *dev)
165 struct rte_kni_request req;
166 struct kni_dev *kni = netdev_priv(dev);
168 netif_stop_queue(dev); /* can't transmit any more */
170 memset(&req, 0, sizeof(req));
171 req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
173 /* Setting if_up to 0 means down */
175 ret = kni_net_process_request(kni, &req);
177 return (ret == 0 ? req.result : ret);
181 * Configuration changes (passed on by ifconfig)
184 kni_net_config(struct net_device *dev, struct ifmap *map)
186 if (dev->flags & IFF_UP) /* can't act on a running interface */
189 /* ignore other fields */
194 * RX: normal working mode
197 kni_net_rx_normal(struct kni_dev *kni)
201 unsigned i, num, num_rq, num_fq;
202 struct rte_kni_mbuf *kva;
203 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
207 struct net_device *dev = kni->net_dev;
209 /* Get the number of entries in rx_q */
210 num_rq = kni_fifo_count(kni->rx_q);
212 /* Get the number of free entries in free_q */
213 num_fq = kni_fifo_free_count(kni->free_q);
215 /* Calculate the number of entries to dequeue in rx_q */
216 num = min(num_rq, num_fq);
217 num = min(num, (unsigned)MBUF_BURST_SZ);
219 /* Return if no entry in rx_q and no free entry in free_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 /* Transfer received packets to netif */
229 for (i = 0; i < num; i++) {
230 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
232 data_kva = kva->data - kni->mbuf_va + kni->mbuf_kva;
234 skb = dev_alloc_skb(len + 2);
236 KNI_ERR("Out of mem, dropping pkts\n");
237 /* Update statistics */
238 kni->stats.rx_dropped++;
241 /* Align IP on 16B boundary */
243 memcpy(skb_put(skb, len), data_kva, len);
245 skb->protocol = eth_type_trans(skb, dev);
246 skb->ip_summed = CHECKSUM_UNNECESSARY;
248 /* Call netif interface */
249 netif_receive_skb(skb);
251 /* Update statistics */
252 kni->stats.rx_bytes += len;
253 kni->stats.rx_packets++;
257 /* Burst enqueue mbufs into free_q */
258 ret = kni_fifo_put(kni->free_q, (void **)va, num);
260 /* Failing should not happen */
261 KNI_ERR("Fail to enqueue entries into free_q\n");
265 * RX: loopback with enqueue/dequeue fifos.
268 kni_net_rx_lo_fifo(struct kni_dev *kni)
272 unsigned i, num, num_rq, num_tq, num_aq, num_fq;
273 struct rte_kni_mbuf *kva;
274 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
277 struct rte_kni_mbuf *alloc_kva;
278 struct rte_kni_mbuf *alloc_va[MBUF_BURST_SZ];
279 void *alloc_data_kva;
281 /* Get the number of entries in rx_q */
282 num_rq = kni_fifo_count(kni->rx_q);
284 /* Get the number of free entrie in tx_q */
285 num_tq = kni_fifo_free_count(kni->tx_q);
287 /* Get the number of entries in alloc_q */
288 num_aq = kni_fifo_count(kni->alloc_q);
290 /* Get the number of free entries in free_q */
291 num_fq = kni_fifo_free_count(kni->free_q);
293 /* Calculate the number of entries to be dequeued from rx_q */
294 num = min(num_rq, num_tq);
295 num = min(num, num_aq);
296 num = min(num, num_fq);
297 num = min(num, (unsigned)MBUF_BURST_SZ);
299 /* Return if no entry to dequeue from rx_q */
303 /* Burst dequeue from rx_q */
304 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
306 return; /* Failing should not happen */
308 /* Dequeue entries from alloc_q */
309 ret = kni_fifo_get(kni->alloc_q, (void **)alloc_va, num);
313 for (i = 0; i < num; i++) {
314 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
316 data_kva = kva->data - kni->mbuf_va +
319 alloc_kva = (void *)alloc_va[i] - kni->mbuf_va +
321 alloc_data_kva = alloc_kva->data - kni->mbuf_va +
323 memcpy(alloc_data_kva, data_kva, len);
324 alloc_kva->pkt_len = len;
325 alloc_kva->data_len = len;
327 kni->stats.tx_bytes += len;
328 kni->stats.rx_bytes += len;
331 /* Burst enqueue mbufs into tx_q */
332 ret = kni_fifo_put(kni->tx_q, (void **)alloc_va, num);
334 /* Failing should not happen */
335 KNI_ERR("Fail to enqueue mbufs into tx_q\n");
338 /* Burst enqueue mbufs into free_q */
339 ret = kni_fifo_put(kni->free_q, (void **)va, num);
341 /* Failing should not happen */
342 KNI_ERR("Fail to enqueue mbufs into free_q\n");
345 * Update statistic, and enqueue/dequeue failure is impossible,
346 * as all queues are checked at first.
348 kni->stats.tx_packets += num;
349 kni->stats.rx_packets += num;
353 * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
356 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
360 unsigned i, num_rq, num_fq, num;
361 struct rte_kni_mbuf *kva;
362 struct rte_kni_mbuf *va[MBUF_BURST_SZ];
366 struct net_device *dev = kni->net_dev;
368 /* Get the number of entries in rx_q */
369 num_rq = kni_fifo_count(kni->rx_q);
371 /* Get the number of free entries in free_q */
372 num_fq = kni_fifo_free_count(kni->free_q);
374 /* Calculate the number of entries to dequeue from rx_q */
375 num = min(num_rq, num_fq);
376 num = min(num, (unsigned)MBUF_BURST_SZ);
378 /* Return if no entry to dequeue from rx_q */
382 /* Burst dequeue mbufs from rx_q */
383 ret = kni_fifo_get(kni->rx_q, (void **)va, num);
387 /* Copy mbufs to sk buffer and then call tx interface */
388 for (i = 0; i < num; i++) {
389 kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva;
391 data_kva = kva->data - kni->mbuf_va + kni->mbuf_kva;
393 skb = dev_alloc_skb(len + 2);
395 KNI_ERR("Out of mem, dropping pkts\n");
397 /* Align IP on 16B boundary */
399 memcpy(skb_put(skb, len), data_kva, len);
401 skb->protocol = eth_type_trans(skb, dev);
402 skb->ip_summed = CHECKSUM_UNNECESSARY;
406 /* Simulate real usage, allocate/copy skb twice */
407 skb = dev_alloc_skb(len + 2);
409 KNI_ERR("Out of mem, dropping pkts\n");
410 kni->stats.rx_dropped++;
413 /* Align IP on 16B boundary */
415 memcpy(skb_put(skb, len), data_kva, len);
417 skb->protocol = eth_type_trans(skb, dev);
418 skb->ip_summed = CHECKSUM_UNNECESSARY;
420 kni->stats.rx_bytes += len;
421 kni->stats.rx_packets++;
423 /* call tx interface */
424 kni_net_tx(skb, dev);
428 /* enqueue all the mbufs from rx_q into free_q */
429 ret = kni_fifo_put(kni->free_q, (void **)&va, num);
431 /* Failing should not happen */
432 KNI_ERR("Fail to enqueue mbufs into free_q\n");
437 kni_net_rx(struct kni_dev *kni)
440 * It doesn't need to check if it is NULL pointer,
441 * as it has a default value
443 (*kni_net_rx_func)(kni);
447 * Transmit a packet (called by the kernel)
450 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
454 struct kni_dev *kni = netdev_priv(dev);
455 struct rte_kni_mbuf *pkt_kva = NULL;
456 struct rte_kni_mbuf *pkt_va = NULL;
458 dev->trans_start = jiffies; /* save the timestamp */
460 /* Check if the length of skb is less than mbuf size */
461 if (skb->len > kni->mbuf_size)
465 * Check if it has at least one free entry in tx_q and
466 * one entry in alloc_q.
468 if (kni_fifo_free_count(kni->tx_q) == 0 ||
469 kni_fifo_count(kni->alloc_q) == 0) {
471 * If no free entry in tx_q or no entry in alloc_q,
472 * drops skb and goes out.
477 /* dequeue a mbuf from alloc_q */
478 ret = kni_fifo_get(kni->alloc_q, (void **)&pkt_va, 1);
479 if (likely(ret == 1)) {
482 pkt_kva = (void *)pkt_va - kni->mbuf_va + kni->mbuf_kva;
483 data_kva = pkt_kva->data - kni->mbuf_va + kni->mbuf_kva;
486 memcpy(data_kva, skb->data, len);
487 if (unlikely(len < ETH_ZLEN)) {
488 memset(data_kva + len, 0, ETH_ZLEN - len);
491 pkt_kva->pkt_len = len;
492 pkt_kva->data_len = len;
494 /* enqueue mbuf into tx_q */
495 ret = kni_fifo_put(kni->tx_q, (void **)&pkt_va, 1);
496 if (unlikely(ret != 1)) {
497 /* Failing should not happen */
498 KNI_ERR("Fail to enqueue mbuf into tx_q\n");
502 /* Failing should not happen */
503 KNI_ERR("Fail to dequeue mbuf from alloc_q\n");
507 /* Free skb and update statistics */
509 kni->stats.tx_bytes += len;
510 kni->stats.tx_packets++;
515 /* Free skb and update statistics */
517 kni->stats.tx_dropped++;
523 * Deal with a transmit timeout.
526 kni_net_tx_timeout (struct net_device *dev)
528 struct kni_dev *kni = netdev_priv(dev);
530 KNI_DBG("Transmit timeout at %ld, latency %ld\n", jiffies,
531 jiffies - dev->trans_start);
533 kni->stats.tx_errors++;
534 netif_wake_queue(dev);
542 kni_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
544 struct kni_dev *kni = netdev_priv(dev);
545 KNI_DBG("kni_net_ioctl %d\n", kni->port_id);
551 kni_net_change_mtu(struct net_device *dev, int new_mtu)
554 struct rte_kni_request req;
555 struct kni_dev *kni = netdev_priv(dev);
557 KNI_DBG("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
559 memset(&req, 0, sizeof(req));
560 req.req_id = RTE_KNI_REQ_CHANGE_MTU;
561 req.new_mtu = new_mtu;
562 ret = kni_net_process_request(kni, &req);
563 if (ret == 0 && req.result == 0)
566 return (ret == 0 ? req.result : ret);
570 * Checks if the user space application provided the resp message
573 kni_net_poll_resp(struct kni_dev *kni)
575 if (kni_fifo_count(kni->resp_q))
576 wake_up_interruptible(&kni->wq);
580 * It can be called to process the request.
583 kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req)
591 KNI_ERR("No kni instance or request\n");
595 mutex_lock(&kni->sync_lock);
598 memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
599 num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
601 KNI_ERR("Cannot send to req_q\n");
606 ret_val = wait_event_interruptible_timeout(kni->wq,
607 kni_fifo_count(kni->resp_q), 3 * HZ);
608 if (signal_pending(current) || ret_val <= 0) {
612 num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
613 if (num != 1 || resp_va != kni->sync_va) {
614 /* This should never happen */
615 KNI_ERR("No data in resp_q\n");
620 memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
624 mutex_unlock(&kni->sync_lock);
629 * Return statistics to the caller
631 static struct net_device_stats *
632 kni_net_stats(struct net_device *dev)
634 struct kni_dev *kni = netdev_priv(dev);
639 * Fill the eth header
642 kni_net_header(struct sk_buff *skb, struct net_device *dev,
643 unsigned short type, const void *daddr,
644 const void *saddr, unsigned int len)
646 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
648 memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
649 memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
650 eth->h_proto = htons(type);
652 return (dev->hard_header_len);
657 * Re-fill the eth header
660 kni_net_rebuild_header(struct sk_buff *skb)
662 struct net_device *dev = skb->dev;
663 struct ethhdr *eth = (struct ethhdr *) skb->data;
665 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
666 memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
672 static const struct header_ops kni_net_header_ops = {
673 .create = kni_net_header,
674 .rebuild = kni_net_rebuild_header,
675 .cache = NULL, /* disable caching */
678 static const struct net_device_ops kni_net_netdev_ops = {
679 .ndo_open = kni_net_open,
680 .ndo_stop = kni_net_release,
681 .ndo_set_config = kni_net_config,
682 .ndo_start_xmit = kni_net_tx,
683 .ndo_change_mtu = kni_net_change_mtu,
684 .ndo_do_ioctl = kni_net_ioctl,
685 .ndo_get_stats = kni_net_stats,
686 .ndo_tx_timeout = kni_net_tx_timeout,
690 kni_net_init(struct net_device *dev)
692 struct kni_dev *kni = netdev_priv(dev);
694 KNI_DBG("kni_net_init\n");
696 init_waitqueue_head(&kni->wq);
697 mutex_init(&kni->sync_lock);
699 ether_setup(dev); /* assign some of the fields */
700 dev->netdev_ops = &kni_net_netdev_ops;
701 dev->header_ops = &kni_net_header_ops;
702 dev->watchdog_timeo = WD_TIMEOUT;
706 kni_net_config_lo_mode(char *lo_str)
709 KNI_PRINT("loopback disabled");
713 if (!strcmp(lo_str, "lo_mode_none"))
714 KNI_PRINT("loopback disabled");
715 else if (!strcmp(lo_str, "lo_mode_fifo")) {
716 KNI_PRINT("loopback mode=lo_mode_fifo enabled");
717 kni_net_rx_func = kni_net_rx_lo_fifo;
718 } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
719 KNI_PRINT("loopback mode=lo_mode_fifo_skb enabled");
720 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
722 KNI_PRINT("Incognizant parameter, loopback disabled");