X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;ds=sidebyside;f=lib%2Flibrte_eal%2Flinuxapp%2Fkni%2Fkni_net.c;h=fc82193a5ca4c51c816b5a797b350418fa06ae7d;hb=73983b2bdae8a285c3cca3bbd18a7e965c3f1cca;hp=d6f9af8858f7781d540f3317332e4e6e310a1a06;hpb=f2e7592c474cf096266b02c71b292a696d19099e;p=dpdk.git diff --git a/lib/librte_eal/linuxapp/kni/kni_net.c b/lib/librte_eal/linuxapp/kni/kni_net.c old mode 100755 new mode 100644 index d6f9af8858..fc82193a5c --- a/lib/librte_eal/linuxapp/kni/kni_net.c +++ b/lib/librte_eal/linuxapp/kni/kni_net.c @@ -1,26 +1,25 @@ /*- * GPL LICENSE SUMMARY - * - * Copyright(c) 2010-2013 Intel Corporation. All rights reserved. - * - * This program is free software; you can redistribute it and/or modify + * + * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. - * The full GNU General Public License is included in this distribution + * The full GNU General Public License is included in this distribution * in the file called LICENSE.GPL. - * + * * Contact Information: * Intel Corporation - * */ /* @@ -37,8 +36,10 @@ #include #include -#include #include +#include + +#include "compat.h" #include "kni_dev.h" #define WD_TIMEOUT 5 /*jiffies */ @@ -60,68 +61,6 @@ static int kni_net_process_request(struct kni_dev *kni, /* kni rx function pointer, with default to normal rx */ static kni_net_rx_t kni_net_rx_func = kni_net_rx_normal; - -/** - * Adds num elements into the fifo. Return the number actually written - */ -static inline unsigned -kni_fifo_put(struct rte_kni_fifo *fifo, void **data, unsigned num) -{ - unsigned i = 0; - unsigned fifo_write = fifo->write; - unsigned fifo_read = fifo->read; - unsigned new_write = fifo_write; - - for (i = 0; i < num; i++) { - new_write = (new_write + 1) & (fifo->len - 1); - - if (new_write == fifo_read) - break; - fifo->buffer[fifo_write] = data[i]; - fifo_write = new_write; - } - fifo->write = fifo_write; - return i; -} - -/** - * Get up to num elements from the fifo. Return the number actully read - */ -static inline unsigned -kni_fifo_get(struct rte_kni_fifo *fifo, void **data, unsigned num) -{ - unsigned i = 0; - unsigned new_read = fifo->read; - unsigned fifo_write = fifo->write; - for (i = 0; i < num; i++) { - if (new_read == fifo_write) - break; - - data[i] = fifo->buffer[new_read]; - new_read = (new_read + 1) & (fifo->len - 1); - } - fifo->read = new_read; - return i; -} - -/** - * Get the num of elements in the fifo - */ -static inline unsigned -kni_fifo_count(struct rte_kni_fifo *fifo) -{ - return (fifo->len + fifo->write - fifo->read) &( fifo->len - 1); -} - -/** - * Get the num of available lements in the fifo - */ -static inline unsigned -kni_fifo_free_count(struct rte_kni_fifo *fifo) -{ - return (fifo->read - fifo->write - 1) & (fifo->len - 1); -} - /* * Open and close */ @@ -132,21 +71,6 @@ kni_net_open(struct net_device *dev) struct rte_kni_request req; struct kni_dev *kni = netdev_priv(dev); - KNI_DBG("kni_net_open %d\n", kni->port_id); - - /* - * Assign the hardware address of the board: use "\0KNIx", where - * x is KNI index. The first byte is '\0' to avoid being a multicast - * address (the first byte of multicast addrs is odd). - */ - - if (kni->lad_dev) - memcpy(dev->dev_addr, kni->lad_dev->dev_addr, ETH_ALEN); - else { - memcpy(dev->dev_addr, "\0KNI0", ETH_ALEN); - dev->dev_addr[ETH_ALEN-1] += kni->port_id; /* \0KNI1 */ - } - netif_start_queue(dev); memset(&req, 0, sizeof(req)); @@ -156,7 +80,7 @@ kni_net_open(struct net_device *dev) req.if_up = 1; ret = kni_net_process_request(kni, &req); - return (ret == 0 ? req.result : ret); + return (ret == 0) ? req.result : ret; } static int @@ -175,7 +99,7 @@ kni_net_release(struct net_device *dev) req.if_up = 0; ret = kni_net_process_request(kni, &req); - return (ret == 0 ? req.result : ret); + return (ret == 0) ? req.result : ret; } /* @@ -199,7 +123,7 @@ kni_net_rx_normal(struct kni_dev *kni) { unsigned ret; uint32_t len; - unsigned i, num, num_rq, num_fq; + unsigned i, num_rx, num_fq; struct rte_kni_mbuf *kva; struct rte_kni_mbuf *va[MBUF_BURST_SZ]; void * data_kva; @@ -207,57 +131,74 @@ kni_net_rx_normal(struct kni_dev *kni) struct sk_buff *skb; struct net_device *dev = kni->net_dev; - /* Get the number of entries in rx_q */ - num_rq = kni_fifo_count(kni->rx_q); - /* Get the number of free entries in free_q */ num_fq = kni_fifo_free_count(kni->free_q); - - /* Calculate the number of entries to dequeue in rx_q */ - num = min(num_rq, num_fq); - num = min(num, (unsigned)MBUF_BURST_SZ); - - /* Return if no entry in rx_q and no free entry in free_q */ - if (num == 0) + if (num_fq == 0) { + /* No room on the free_q, bail out */ return; + } + + /* Calculate the number of entries to dequeue from rx_q */ + num_rx = min(num_fq, (unsigned)MBUF_BURST_SZ); /* Burst dequeue from rx_q */ - ret = kni_fifo_get(kni->rx_q, (void **)va, num); - if (ret == 0) - return; /* Failing should not happen */ + num_rx = kni_fifo_get(kni->rx_q, (void **)va, num_rx); + if (num_rx == 0) + return; /* Transfer received packets to netif */ - for (i = 0; i < num; i++) { + for (i = 0; i < num_rx; i++) { kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva; - len = kva->data_len; - data_kva = kva->data - kni->mbuf_va + kni->mbuf_kva; + len = kva->pkt_len; + + data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va + + kni->mbuf_kva; skb = dev_alloc_skb(len + 2); if (!skb) { KNI_ERR("Out of mem, dropping pkts\n"); /* Update statistics */ kni->stats.rx_dropped++; + continue; } - else { - /* Align IP on 16B boundary */ - skb_reserve(skb, 2); + + /* Align IP on 16B boundary */ + skb_reserve(skb, 2); + + if (kva->nb_segs == 1) { memcpy(skb_put(skb, len), data_kva, len); - skb->dev = dev; - skb->protocol = eth_type_trans(skb, dev); - skb->ip_summed = CHECKSUM_UNNECESSARY; + } else { + int nb_segs; + int kva_nb_segs = kva->nb_segs; - /* Call netif interface */ - netif_receive_skb(skb); + for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) { + memcpy(skb_put(skb, kva->data_len), + data_kva, kva->data_len); - /* Update statistics */ - kni->stats.rx_bytes += len; - kni->stats.rx_packets++; + if (!kva->next) + break; + + kva = kva->next - kni->mbuf_va + kni->mbuf_kva; + data_kva = kva->buf_addr + kva->data_off + - kni->mbuf_va + kni->mbuf_kva; + } } + + skb->dev = dev; + skb->protocol = eth_type_trans(skb, dev); + skb->ip_summed = CHECKSUM_UNNECESSARY; + + /* Call netif interface */ + netif_rx_ni(skb); + + /* Update statistics */ + kni->stats.rx_bytes += len; + kni->stats.rx_packets++; } /* Burst enqueue mbufs into free_q */ - ret = kni_fifo_put(kni->free_q, (void **)va, num); - if (ret != num) + ret = kni_fifo_put(kni->free_q, (void **)va, num_rx); + if (ret != num_rx) /* Failing should not happen */ KNI_ERR("Fail to enqueue entries into free_q\n"); } @@ -314,13 +255,14 @@ kni_net_rx_lo_fifo(struct kni_dev *kni) for (i = 0; i < num; i++) { kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva; len = kva->pkt_len; - data_kva = kva->data - kni->mbuf_va + - kni->mbuf_kva; + data_kva = kva->buf_addr + kva->data_off - + kni->mbuf_va + kni->mbuf_kva; alloc_kva = (void *)alloc_va[i] - kni->mbuf_va + kni->mbuf_kva; - alloc_data_kva = alloc_kva->data - kni->mbuf_va + - kni->mbuf_kva; + alloc_data_kva = alloc_kva->buf_addr + + alloc_kva->data_off - kni->mbuf_va + + kni->mbuf_kva; memcpy(alloc_data_kva, data_kva, len); alloc_kva->pkt_len = len; alloc_kva->data_len = len; @@ -388,8 +330,9 @@ kni_net_rx_lo_fifo_skb(struct kni_dev *kni) /* Copy mbufs to sk buffer and then call tx interface */ for (i = 0; i < num; i++) { kva = (void *)va[i] - kni->mbuf_va + kni->mbuf_kva; - len = kva->data_len; - data_kva = kva->data - kni->mbuf_va + kni->mbuf_kva; + len = kva->pkt_len; + data_kva = kva->buf_addr + kva->data_off - kni->mbuf_va + + kni->mbuf_kva; skb = dev_alloc_skb(len + 2); if (skb == NULL) @@ -399,7 +342,6 @@ kni_net_rx_lo_fifo_skb(struct kni_dev *kni) skb_reserve(skb, 2); memcpy(skb_put(skb, len), data_kva, len); skb->dev = dev; - skb->protocol = eth_type_trans(skb, dev); skb->ip_summed = CHECKSUM_UNNECESSARY; dev_kfree_skb(skb); } @@ -409,21 +351,39 @@ kni_net_rx_lo_fifo_skb(struct kni_dev *kni) if (skb == NULL) { KNI_ERR("Out of mem, dropping pkts\n"); kni->stats.rx_dropped++; + continue; } - else { - /* Align IP on 16B boundary */ - skb_reserve(skb, 2); + + /* Align IP on 16B boundary */ + skb_reserve(skb, 2); + + if (kva->nb_segs == 1) { memcpy(skb_put(skb, len), data_kva, len); - skb->dev = dev; - skb->protocol = eth_type_trans(skb, dev); - skb->ip_summed = CHECKSUM_UNNECESSARY; + } else { + int nb_segs; + int kva_nb_segs = kva->nb_segs; - kni->stats.rx_bytes += len; - kni->stats.rx_packets++; + for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) { + memcpy(skb_put(skb, kva->data_len), + data_kva, kva->data_len); - /* call tx interface */ - kni_net_tx(skb, dev); + if (!kva->next) + break; + + kva = kva->next - kni->mbuf_va + kni->mbuf_kva; + data_kva = kva->buf_addr + kva->data_off + - kni->mbuf_va + kni->mbuf_kva; + } } + + skb->dev = dev; + skb->ip_summed = CHECKSUM_UNNECESSARY; + + kni->stats.rx_bytes += len; + kni->stats.rx_packets++; + + /* call tx interface */ + kni_net_tx(skb, dev); } /* enqueue all the mbufs from rx_q into free_q */ @@ -447,6 +407,18 @@ kni_net_rx(struct kni_dev *kni) /* * Transmit a packet (called by the kernel) */ +#ifdef RTE_KNI_VHOST +static int +kni_net_tx(struct sk_buff *skb, struct net_device *dev) +{ + struct kni_dev *kni = netdev_priv(dev); + + dev_kfree_skb(skb); + kni->stats.tx_dropped++; + + return NETDEV_TX_OK; +} +#else static int kni_net_tx(struct sk_buff *skb, struct net_device *dev) { @@ -456,7 +428,12 @@ kni_net_tx(struct sk_buff *skb, struct net_device *dev) struct rte_kni_mbuf *pkt_kva = NULL; struct rte_kni_mbuf *pkt_va = NULL; - dev->trans_start = jiffies; /* save the timestamp */ + /* save the timestamp */ +#ifdef HAVE_TRANS_START_HELPER + netif_trans_update(dev); +#else + dev->trans_start = jiffies; +#endif /* Check if the length of skb is less than mbuf size */ if (skb->len > kni->mbuf_size) @@ -481,7 +458,8 @@ kni_net_tx(struct sk_buff *skb, struct net_device *dev) void *data_kva; pkt_kva = (void *)pkt_va - kni->mbuf_va + kni->mbuf_kva; - data_kva = pkt_kva->data - kni->mbuf_va + kni->mbuf_kva; + data_kva = pkt_kva->buf_addr + pkt_kva->data_off - kni->mbuf_va + + kni->mbuf_kva; len = skb->len; memcpy(data_kva, skb->data, len); @@ -519,6 +497,7 @@ drop: return NETDEV_TX_OK; } +#endif /* * Deal with a transmit timeout. @@ -542,12 +521,17 @@ kni_net_tx_timeout (struct net_device *dev) static int kni_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) { - struct kni_dev *kni = netdev_priv(dev); - KNI_DBG("kni_net_ioctl %d\n", kni->port_id); + KNI_DBG("kni_net_ioctl %d\n", + ((struct kni_dev *)netdev_priv(dev))->group_id); return 0; } +static void +kni_net_set_rx_mode(struct net_device *dev) +{ +} + static int kni_net_change_mtu(struct net_device *dev, int new_mtu) { @@ -564,7 +548,7 @@ kni_net_change_mtu(struct net_device *dev, int new_mtu) if (ret == 0 && req.result == 0) dev->mtu = new_mtu; - return (ret == 0 ? req.result : ret); + return (ret == 0) ? req.result : ret; } /* @@ -573,14 +557,10 @@ kni_net_change_mtu(struct net_device *dev, int new_mtu) void kni_net_poll_resp(struct kni_dev *kni) { - int i = kni_fifo_count(kni->resp_q); - - if (i) { + if (kni_fifo_count(kni->resp_q)) wake_up_interruptible(&kni->wq); - } } - /* * It can be called to process the request. */ @@ -622,7 +602,6 @@ kni_net_process_request(struct kni_dev *kni, struct rte_kni_request *req) goto fail; } - memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request)); ret = 0; @@ -655,13 +634,14 @@ kni_net_header(struct sk_buff *skb, struct net_device *dev, memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len); eth->h_proto = htons(type); - return (dev->hard_header_len); + return dev->hard_header_len; } /* * Re-fill the eth header */ +#ifdef HAVE_REBUILD_HEADER static int kni_net_rebuild_header(struct sk_buff *skb) { @@ -673,11 +653,40 @@ kni_net_rebuild_header(struct sk_buff *skb) return 0; } +#endif /* < 4.1.0 */ +/** + * kni_net_set_mac - Change the Ethernet Address of the KNI NIC + * @netdev: network interface device structure + * @p: pointer to an address structure + * + * Returns 0 on success, negative on failure + **/ +static int kni_net_set_mac(struct net_device *netdev, void *p) +{ + struct sockaddr *addr = p; + if (!is_valid_ether_addr((unsigned char *)(addr->sa_data))) + return -EADDRNOTAVAIL; + memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); + return 0; +} + +#ifdef HAVE_CHANGE_CARRIER_CB +static int kni_net_change_carrier(struct net_device *dev, bool new_carrier) +{ + if (new_carrier) + netif_carrier_on(dev); + else + netif_carrier_off(dev); + return 0; +} +#endif static const struct header_ops kni_net_header_ops = { .create = kni_net_header, +#ifdef HAVE_REBUILD_HEADER .rebuild = kni_net_rebuild_header, +#endif /* < 4.1.0 */ .cache = NULL, /* disable caching */ }; @@ -688,8 +697,13 @@ static const struct net_device_ops kni_net_netdev_ops = { .ndo_start_xmit = kni_net_tx, .ndo_change_mtu = kni_net_change_mtu, .ndo_do_ioctl = kni_net_ioctl, + .ndo_set_rx_mode = kni_net_set_rx_mode, .ndo_get_stats = kni_net_stats, .ndo_tx_timeout = kni_net_tx_timeout, + .ndo_set_mac_address = kni_net_set_mac, +#ifdef HAVE_CHANGE_CARRIER_CB + .ndo_change_carrier = kni_net_change_carrier, +#endif }; void @@ -727,4 +741,3 @@ kni_net_config_lo_mode(char *lo_str) } else KNI_PRINT("Incognizant parameter, loopback disabled"); } -