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:
25 #include <linux/module.h>
26 #include <linux/net.h>
28 #include <linux/virtio_net.h>
29 #include <linux/wait.h>
31 #include <linux/nsproxy.h>
32 #include <linux/sched.h>
33 #include <linux/if_tun.h>
34 #include <linux/version.h>
41 extern void put_unused_fd(unsigned int fd);
43 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
45 sock_alloc_file(struct socket *sock,
46 int flags, const char *dname);
48 extern int get_unused_fd_flags(unsigned flags);
50 extern void fd_install(unsigned int fd, struct file *file);
52 static int kni_sock_map_fd(struct socket *sock)
55 int fd = get_unused_fd_flags(0);
59 file = sock_alloc_file(sock, 0, NULL);
68 #define kni_sock_map_fd(s) sock_map_fd(s, 0)
71 static struct proto kni_raw_proto = {
74 .obj_size = sizeof(struct kni_vhost_queue),
78 kni_vhost_net_tx(struct kni_dev *kni, struct iovec *iov,
79 unsigned offset, unsigned len)
81 struct rte_kni_mbuf *pkt_kva = NULL;
82 struct rte_kni_mbuf *pkt_va = NULL;
85 KNI_DBG_TX("tx offset=%d, len=%d, iovlen=%d\n",
86 offset, len, (int)iov->iov_len);
89 * Check if it has at least one free entry in tx_q and
90 * one entry in alloc_q.
92 if (kni_fifo_free_count(kni->tx_q) == 0 ||
93 kni_fifo_count(kni->alloc_q) == 0) {
95 * If no free entry in tx_q or no entry in alloc_q,
96 * drops skb and goes out.
101 /* dequeue a mbuf from alloc_q */
102 ret = kni_fifo_get(kni->alloc_q, (void **)&pkt_va, 1);
103 if (likely(ret == 1)) {
106 pkt_kva = (void *)pkt_va - kni->mbuf_va + kni->mbuf_kva;
107 data_kva = pkt_kva->buf_addr + pkt_kva->data_off
108 - kni->mbuf_va + kni->mbuf_kva;
110 memcpy_fromiovecend(data_kva, iov, offset, len);
111 if (unlikely(len < ETH_ZLEN)) {
112 memset(data_kva + len, 0, ETH_ZLEN - len);
115 pkt_kva->pkt_len = len;
116 pkt_kva->data_len = len;
118 /* enqueue mbuf into tx_q */
119 ret = kni_fifo_put(kni->tx_q, (void **)&pkt_va, 1);
120 if (unlikely(ret != 1)) {
121 /* Failing should not happen */
122 KNI_ERR("Fail to enqueue mbuf into tx_q\n");
126 /* Failing should not happen */
127 KNI_ERR("Fail to dequeue mbuf from alloc_q\n");
131 /* update statistics */
132 kni->stats.tx_bytes += len;
133 kni->stats.tx_packets++;
138 /* update statistics */
139 kni->stats.tx_dropped++;
145 kni_vhost_net_rx(struct kni_dev *kni, struct iovec *iov,
146 unsigned offset, unsigned len)
149 struct rte_kni_mbuf *kva;
150 struct rte_kni_mbuf *va;
153 struct kni_vhost_queue *q = kni->vhost_queue;
155 if (unlikely(q == NULL))
158 /* ensure at least one entry in free_q */
159 if (unlikely(kni_fifo_free_count(kni->free_q) == 0))
162 skb = skb_dequeue(&q->sk.sk_receive_queue);
163 if (unlikely(skb == NULL))
166 kva = (struct rte_kni_mbuf*)skb->data;
168 /* free skb to cache */
170 if (unlikely(1 != kni_fifo_put(q->fifo, (void **)&skb, 1)))
171 /* Failing should not happen */
172 KNI_ERR("Fail to enqueue entries into rx cache fifo\n");
174 pkt_len = kva->data_len;
175 if (unlikely(pkt_len > len))
178 KNI_DBG_RX("rx offset=%d, len=%d, pkt_len=%d, iovlen=%d\n",
179 offset, len, pkt_len, (int)iov->iov_len);
181 data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va + kni->mbuf_kva;
182 if (unlikely(memcpy_toiovecend(iov, data_kva, offset, pkt_len)))
185 /* Update statistics */
186 kni->stats.rx_bytes += pkt_len;
187 kni->stats.rx_packets++;
189 /* enqueue mbufs into free_q */
190 va = (void*)kva - kni->mbuf_kva + kni->mbuf_va;
191 if (unlikely(1 != kni_fifo_put(kni->free_q, (void **)&va, 1)))
192 /* Failing should not happen */
193 KNI_ERR("Fail to enqueue entries into free_q\n");
195 KNI_DBG_RX("receive done %d\n", pkt_len);
200 /* Update drop statistics */
201 kni->stats.rx_dropped++;
207 kni_sock_poll(struct file *file, struct socket *sock, poll_table * wait)
209 struct kni_vhost_queue *q =
210 container_of(sock->sk, struct kni_vhost_queue, sk);
212 unsigned int mask = 0;
214 if (unlikely(q == NULL || q->kni == NULL))
218 KNI_DBG("start kni_poll on group %d, wq 0x%16llx\n",
219 kni->group_id, (uint64_t)sock->wq);
221 poll_wait(file, &sock->wq->wait, wait);
223 if (kni_fifo_count(kni->rx_q) > 0)
224 mask |= POLLIN | POLLRDNORM;
226 if (sock_writeable(&q->sk) ||
227 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock->flags) &&
228 sock_writeable(&q->sk)))
229 mask |= POLLOUT | POLLWRNORM;
235 kni_vhost_enqueue(struct kni_dev *kni, struct kni_vhost_queue *q,
236 struct sk_buff *skb, struct rte_kni_mbuf *va)
238 struct rte_kni_mbuf *kva;
240 kva = (void *)(va) - kni->mbuf_va + kni->mbuf_kva;
241 (skb)->data = (unsigned char*)kva;
242 (skb)->len = kva->data_len;
243 skb_queue_tail(&q->sk.sk_receive_queue, skb);
247 kni_vhost_enqueue_burst(struct kni_dev *kni, struct kni_vhost_queue *q,
248 struct sk_buff **skb, struct rte_kni_mbuf **va)
251 for (i = 0; i < RX_BURST_SZ; skb++, va++, i++)
252 kni_vhost_enqueue(kni, q, *skb, *va);
256 kni_chk_vhost_rx(struct kni_dev *kni)
258 struct kni_vhost_queue *q = kni->vhost_queue;
259 unsigned nb_in, nb_mbuf, nb_skb;
260 const unsigned BURST_MASK = RX_BURST_SZ - 1;
261 unsigned nb_burst, nb_backlog, i;
262 struct sk_buff *skb[RX_BURST_SZ];
263 struct rte_kni_mbuf *va[RX_BURST_SZ];
265 if (unlikely(BE_STOP & kni->vq_status)) {
266 kni->vq_status |= BE_FINISH;
270 if (unlikely(q == NULL))
273 nb_skb = kni_fifo_count(q->fifo);
274 nb_mbuf = kni_fifo_count(kni->rx_q);
276 nb_in = min(nb_mbuf, nb_skb);
277 nb_in = min(nb_in, (unsigned)RX_BURST_SZ);
278 nb_burst = (nb_in & ~BURST_MASK);
279 nb_backlog = (nb_in & BURST_MASK);
281 /* enqueue skb_queue per BURST_SIZE bulk */
283 if (unlikely(RX_BURST_SZ != kni_fifo_get(
284 kni->rx_q, (void **)&va,
288 if (unlikely(RX_BURST_SZ != kni_fifo_get(
289 q->fifo, (void **)&skb,
293 kni_vhost_enqueue_burst(kni, q, skb, va);
296 /* all leftover, do one by one */
297 for (i = 0; i < nb_backlog; ++i) {
298 if (unlikely(1 != kni_fifo_get(
299 kni->rx_q,(void **)&va, 1)))
302 if (unlikely(1 != kni_fifo_get(
303 q->fifo, (void **)&skb, 1)))
306 kni_vhost_enqueue(kni, q, *skb, *va);
309 /* Ondemand wake up */
310 if ((nb_in == RX_BURST_SZ) || (nb_skb == 0) ||
311 ((nb_mbuf < RX_BURST_SZ) && (nb_mbuf != 0))) {
312 wake_up_interruptible_poll(sk_sleep(&q->sk),
313 POLLIN | POLLRDNORM | POLLRDBAND);
314 KNI_DBG_RX("RX CHK KICK nb_mbuf %d, nb_skb %d, nb_in %d\n",
315 nb_mbuf, nb_skb, nb_in);
321 /* Failing should not happen */
322 KNI_ERR("Fail to enqueue fifo, it shouldn't happen \n");
329 kni_sock_sndmsg(struct kiocb *iocb, struct socket *sock,
330 struct msghdr *m, size_t total_len)
332 struct kni_vhost_queue *q =
333 container_of(sock->sk, struct kni_vhost_queue, sk);
334 int vnet_hdr_len = 0;
335 unsigned long len = total_len;
337 if (unlikely(q == NULL || q->kni == NULL))
340 KNI_DBG_TX("kni_sndmsg len %ld, flags 0x%08x, nb_iov %d\n",
341 len, q->flags, (int)m->msg_iovlen);
343 #ifdef RTE_KNI_VHOST_VNET_HDR_EN
344 if (likely(q->flags & IFF_VNET_HDR)) {
345 vnet_hdr_len = q->vnet_hdr_sz;
346 if (unlikely(len < vnet_hdr_len))
352 if (unlikely(len < ETH_HLEN + q->vnet_hdr_sz))
355 return kni_vhost_net_tx(q->kni, m->msg_iov, vnet_hdr_len, len);
359 kni_sock_rcvmsg(struct kiocb *iocb, struct socket *sock,
360 struct msghdr *m, size_t len, int flags)
362 int vnet_hdr_len = 0;
364 struct kni_vhost_queue *q =
365 container_of(sock->sk, struct kni_vhost_queue, sk);
366 static struct virtio_net_hdr
367 __attribute__ ((unused)) vnet_hdr = {
369 .gso_type = VIRTIO_NET_HDR_GSO_NONE
372 if (unlikely(q == NULL || q->kni == NULL))
375 #ifdef RTE_KNI_VHOST_VNET_HDR_EN
376 if (likely(q->flags & IFF_VNET_HDR)) {
377 vnet_hdr_len = q->vnet_hdr_sz;
378 if ((len -= vnet_hdr_len) < 0)
383 if (unlikely(0 == (pkt_len = kni_vhost_net_rx(q->kni,
384 m->msg_iov, vnet_hdr_len, len))))
387 #ifdef RTE_KNI_VHOST_VNET_HDR_EN
388 /* no need to copy hdr when no pkt received */
389 if (unlikely(memcpy_toiovecend(m->msg_iov,
390 (void *)&vnet_hdr, 0, vnet_hdr_len)))
393 KNI_DBG_RX("kni_rcvmsg expect_len %ld, flags 0x%08x, pkt_len %d\n",
394 (unsigned long)len, q->flags, pkt_len);
396 return (pkt_len + vnet_hdr_len);
399 /* dummy tap like ioctl */
401 kni_sock_ioctl(struct socket *sock, unsigned int cmd,
404 void __user *argp = (void __user *)arg;
405 struct ifreq __user *ifr = argp;
406 unsigned int __user *up = argp;
407 struct kni_vhost_queue *q =
408 container_of(sock->sk, struct kni_vhost_queue, sk);
411 int __user *sp = argp;
415 KNI_DBG("tap ioctl cmd 0x%08x\n", cmd);
419 KNI_DBG("TUNSETIFF\n");
420 /* ignore the name, just look at flags */
421 if (get_user(u, &ifr->ifr_flags))
425 if ((u & ~IFF_VNET_HDR) != (IFF_NO_PI | IFF_TAP))
433 KNI_DBG("TUNGETIFF\n");
435 kni = rcu_dereference_bh(q->kni);
437 dev_hold(kni->net_dev);
438 rcu_read_unlock_bh();
444 if (copy_to_user(&ifr->ifr_name, kni->net_dev->name, IFNAMSIZ) ||
445 put_user(q->flags, &ifr->ifr_flags))
447 dev_put(kni->net_dev);
451 KNI_DBG("TUNGETFEATURES\n");
452 u = IFF_TAP | IFF_NO_PI;
453 #ifdef RTE_KNI_VHOST_VNET_HDR_EN
461 KNI_DBG("TUNSETSNDBUF\n");
468 case TUNGETVNETHDRSZ:
472 KNI_DBG("TUNGETVNETHDRSZ %d\n", s);
475 case TUNSETVNETHDRSZ:
478 if (s < (int)sizeof(struct virtio_net_hdr))
481 KNI_DBG("TUNSETVNETHDRSZ %d\n", s);
486 KNI_DBG("TUNSETOFFLOAD %lx\n", arg);
487 #ifdef RTE_KNI_VHOST_VNET_HDR_EN
488 /* not support any offload yet */
489 if (!(q->flags & IFF_VNET_HDR))
498 KNI_DBG("NOT SUPPORT\n");
504 kni_sock_compat_ioctl(struct socket *sock, unsigned int cmd,
507 /* 32 bits app on 64 bits OS to be supported later */
508 KNI_PRINT("Not implemented.\n");
513 #define KNI_VHOST_WAIT_WQ_SAFE() \
515 while ((BE_FINISH | BE_STOP) == kni->vq_status) \
521 kni_sock_release(struct socket *sock)
523 struct kni_vhost_queue *q =
524 container_of(sock->sk, struct kni_vhost_queue, sk);
530 if (NULL != (kni = q->kni)) {
531 kni->vq_status = BE_STOP;
532 KNI_VHOST_WAIT_WQ_SAFE();
533 kni->vhost_queue = NULL;
540 sk_set_socket(&q->sk, NULL);
545 KNI_DBG("dummy sock release done\n");
551 kni_sock_getname (struct socket *sock,
552 struct sockaddr *addr,
553 int *sockaddr_len, int peer)
555 KNI_DBG("dummy sock getname\n");
556 ((struct sockaddr_ll*)addr)->sll_family = AF_PACKET;
560 static const struct proto_ops kni_socket_ops = {
561 .getname = kni_sock_getname,
562 .sendmsg = kni_sock_sndmsg,
563 .recvmsg = kni_sock_rcvmsg,
564 .release = kni_sock_release,
565 .poll = kni_sock_poll,
566 .ioctl = kni_sock_ioctl,
567 .compat_ioctl = kni_sock_compat_ioctl,
571 kni_sk_write_space(struct sock *sk)
573 wait_queue_head_t *wqueue;
575 if (!sock_writeable(sk) ||
576 !test_and_clear_bit(SOCK_ASYNC_NOSPACE,
577 &sk->sk_socket->flags))
579 wqueue = sk_sleep(sk);
580 if (wqueue && waitqueue_active(wqueue))
581 wake_up_interruptible_poll(
582 wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
586 kni_sk_destruct(struct sock *sk)
588 struct kni_vhost_queue *q =
589 container_of(sk, struct kni_vhost_queue, sk);
594 /* make sure there's no packet in buffer */
595 while (skb_dequeue(&sk->sk_receive_queue) != NULL)
600 if (q->fifo != NULL) {
605 if (q->cache != NULL) {
612 kni_vhost_backend_init(struct kni_dev *kni)
614 struct kni_vhost_queue *q;
615 struct net *net = current->nsproxy->net_ns;
617 struct rte_kni_fifo *fifo;
618 struct sk_buff *elem;
620 if (kni->vhost_queue != NULL)
623 if (!(q = (struct kni_vhost_queue *)sk_alloc(
624 net, AF_UNSPEC, GFP_KERNEL, &kni_raw_proto)))
627 err = sock_create_lite(AF_UNSPEC, SOCK_RAW, IPPROTO_RAW, &q->sock);
631 sockfd = kni_sock_map_fd(q->sock);
638 q->cache = (struct sk_buff*)
639 kzalloc(RTE_KNI_VHOST_MAX_CACHE_SIZE * sizeof(struct sk_buff),
644 fifo = (struct rte_kni_fifo*)
645 kzalloc(RTE_KNI_VHOST_MAX_CACHE_SIZE * sizeof(void *)
646 + sizeof(struct rte_kni_fifo), GFP_KERNEL);
650 kni_fifo_init(fifo, RTE_KNI_VHOST_MAX_CACHE_SIZE);
652 for (i = 0; i < RTE_KNI_VHOST_MAX_CACHE_SIZE; i++) {
654 kni_fifo_put(fifo, (void**)&elem, 1);
658 /* store sockfd in vhost_queue */
662 q->sock->type = SOCK_RAW;
663 q->sock->state = SS_CONNECTED;
664 q->sock->ops = &kni_socket_ops;
665 sock_init_data(q->sock, &q->sk);
668 q->sk.sk_write_space = kni_sk_write_space;
669 q->sk.sk_destruct = kni_sk_destruct;
670 q->flags = IFF_NO_PI | IFF_TAP;
671 q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
672 #ifdef RTE_KNI_VHOST_VNET_HDR_EN
673 q->flags |= IFF_VNET_HDR;
676 /* bind kni_dev with vhost_queue */
678 kni->vhost_queue = q;
682 kni->vq_status = BE_START;
684 KNI_DBG("backend init sockfd=%d, sock->wq=0x%16llx,"
685 "sk->sk_wq=0x%16llx",
686 q->sockfd, (uint64_t)q->sock->wq,
687 (uint64_t)q->sk.sk_wq);
696 put_unused_fd(sockfd);
700 kni->vhost_queue = NULL;
701 kni->vq_status |= BE_FINISH;
702 sock_release(q->sock);
707 sk_free((struct sock*)q);
712 /* kni vhost sock sysfs */
714 show_sock_fd(struct device *dev, struct device_attribute *attr,
717 struct net_device *net_dev = container_of(dev, struct net_device, dev);
718 struct kni_dev *kni = netdev_priv(net_dev);
720 if (kni->vhost_queue != NULL)
721 sockfd = kni->vhost_queue->sockfd;
722 return snprintf(buf, 10, "%d\n", sockfd);
726 show_sock_en(struct device *dev, struct device_attribute *attr,
729 struct net_device *net_dev = container_of(dev, struct net_device, dev);
730 struct kni_dev *kni = netdev_priv(net_dev);
731 return snprintf(buf, 10, "%u\n", (kni->vhost_queue == NULL ? 0 : 1));
735 set_sock_en(struct device *dev, struct device_attribute *attr,
736 const char *buf, size_t count)
738 struct net_device *net_dev = container_of(dev, struct net_device, dev);
739 struct kni_dev *kni = netdev_priv(net_dev);
743 if (0 != kstrtoul(buf, 0, &en))
747 err = kni_vhost_backend_init(kni);
749 return err ? err : count;
752 static DEVICE_ATTR(sock_fd, S_IRUGO | S_IRUSR, show_sock_fd, NULL);
753 static DEVICE_ATTR(sock_en, S_IRUGO | S_IWUSR, show_sock_en, set_sock_en);
754 static struct attribute *dev_attrs[] = {
755 &dev_attr_sock_fd.attr,
756 &dev_attr_sock_en.attr,
760 static const struct attribute_group dev_attr_grp = {
765 kni_vhost_backend_release(struct kni_dev *kni)
767 struct kni_vhost_queue *q = kni->vhost_queue;
772 /* dettach from kni */
775 KNI_DBG("release backend done\n");
781 kni_vhost_init(struct kni_dev *kni)
783 struct net_device *dev = kni->net_dev;
785 if (sysfs_create_group(&dev->dev.kobj, &dev_attr_grp))
786 sysfs_remove_group(&dev->dev.kobj, &dev_attr_grp);
788 kni->vq_status = BE_STOP;
790 KNI_DBG("kni_vhost_init done\n");