4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef RTE_EXEC_ENV_LINUXAPP
36 #error "KNI is not supported"
42 #include <sys/ioctl.h>
43 #include <netinet/in.h>
46 #include <rte_string_fns.h>
47 #include <rte_ethdev.h>
48 #include <rte_malloc.h>
51 #include <rte_memzone.h>
52 #include <exec-env/rte_kni_common.h>
53 #include "rte_kni_fifo.h"
55 #define MAX_MBUF_BURST_NUM 32
57 /* Maximum number of ring entries */
58 #define KNI_FIFO_COUNT_MAX 1024
59 #define KNI_FIFO_SIZE (KNI_FIFO_COUNT_MAX * sizeof(void *) + \
60 sizeof(struct rte_kni_fifo))
62 #define KNI_REQUEST_MBUF_NUM_MAX 32
68 char name[IFNAMSIZ]; /**< KNI interface name */
69 uint8_t port_id; /**< Port id KNI associate with */
70 struct rte_mempool *pktmbuf_pool; /**< pkt mbuf mempool */
71 unsigned mbuf_size; /**< mbuf size */
73 struct rte_kni_fifo *tx_q; /**< TX queue */
74 struct rte_kni_fifo *rx_q; /**< RX queue */
75 struct rte_kni_fifo *alloc_q; /**< Allocated mbufs queue */
76 struct rte_kni_fifo *free_q; /**< To be freed mbufs queue */
78 /* For request & response */
79 struct rte_kni_fifo *req_q; /**< Request queue */
80 struct rte_kni_fifo *resp_q; /**< Response queue */
81 void * sync_addr; /**< Req/Resp Mem address */
83 struct rte_kni_ops ops; /**< operations for request */
86 static void kni_free_mbufs(struct rte_kni *kni);
87 static void kni_allocate_mbufs(struct rte_kni *kni);
89 static int kni_fd = -1;
93 rte_kni_create(uint8_t port_id,
95 struct rte_mempool *pktmbuf_pool,
96 struct rte_kni_ops *ops)
98 struct rte_kni_device_info dev_info;
99 struct rte_eth_dev_info eth_dev_info;
101 char itf_name[IFNAMSIZ];
103 char obj_name[OBJNAMSIZ];
104 const struct rte_memzone *mz;
106 if (port_id >= RTE_MAX_ETHPORTS || pktmbuf_pool == NULL || !ops)
109 /* Check FD and open once */
111 kni_fd = open("/dev/" KNI_DEVICE, O_RDWR);
113 RTE_LOG(ERR, KNI, "Can not open /dev/%s\n",
119 rte_eth_dev_info_get(port_id, ð_dev_info);
120 RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
121 eth_dev_info.pci_dev->addr.bus,
122 eth_dev_info.pci_dev->addr.devid,
123 eth_dev_info.pci_dev->addr.function,
124 eth_dev_info.pci_dev->id.vendor_id,
125 eth_dev_info.pci_dev->id.device_id);
126 dev_info.bus = eth_dev_info.pci_dev->addr.bus;
127 dev_info.devid = eth_dev_info.pci_dev->addr.devid;
128 dev_info.function = eth_dev_info.pci_dev->addr.function;
129 dev_info.vendor_id = eth_dev_info.pci_dev->id.vendor_id;
130 dev_info.device_id = eth_dev_info.pci_dev->id.device_id;
132 ctx = rte_zmalloc("kni devs", sizeof(struct rte_kni), 0);
134 rte_panic("Cannot allocate memory for kni dev\n");
135 memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops));
137 rte_snprintf(itf_name, IFNAMSIZ, "vEth%u", port_id);
138 rte_snprintf(ctx->name, IFNAMSIZ, itf_name);
139 rte_snprintf(dev_info.name, IFNAMSIZ, itf_name);
142 rte_snprintf(obj_name, OBJNAMSIZ, "kni_tx_%d", port_id);
143 mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
144 if (mz == NULL || mz->addr == NULL)
145 rte_panic("Cannot create kni_tx_%d queue\n", port_id);
146 ctx->tx_q = mz->addr;
147 kni_fifo_init(ctx->tx_q, KNI_FIFO_COUNT_MAX);
148 dev_info.tx_phys = mz->phys_addr;
151 rte_snprintf(obj_name, OBJNAMSIZ, "kni_rx_%d", port_id);
152 mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
153 if (mz == NULL || mz->addr == NULL)
154 rte_panic("Cannot create kni_rx_%d queue\n", port_id);
155 ctx->rx_q = mz->addr;
156 kni_fifo_init(ctx->rx_q, KNI_FIFO_COUNT_MAX);
157 dev_info.rx_phys = mz->phys_addr;
160 rte_snprintf(obj_name, OBJNAMSIZ, "kni_alloc_%d", port_id);
161 mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
162 if (mz == NULL || mz->addr == NULL)
163 rte_panic("Cannot create kni_alloc_%d queue\n", port_id);
164 ctx->alloc_q = mz->addr;
165 kni_fifo_init(ctx->alloc_q, KNI_FIFO_COUNT_MAX);
166 dev_info.alloc_phys = mz->phys_addr;
169 rte_snprintf(obj_name, OBJNAMSIZ, "kni_free_%d", port_id);
170 mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
171 if (mz == NULL || mz->addr == NULL)
172 rte_panic("Cannot create kni_free_%d queue\n", port_id);
173 ctx->free_q = mz->addr;
174 kni_fifo_init(ctx->free_q, KNI_FIFO_COUNT_MAX);
175 dev_info.free_phys = mz->phys_addr;
178 rte_snprintf(obj_name, OBJNAMSIZ, "kni_req_%d", port_id);
179 mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
180 if (mz == NULL || mz->addr == NULL)
181 rte_panic("Cannot create kni_req_%d ring\n", port_id);
182 ctx->req_q = mz->addr;
183 kni_fifo_init(ctx->req_q, KNI_FIFO_COUNT_MAX);
184 dev_info.req_phys = mz->phys_addr;
187 rte_snprintf(obj_name, OBJNAMSIZ, "kni_resp_%d", port_id);
188 mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
189 if (mz == NULL || mz->addr == NULL)
190 rte_panic("Cannot create kni_resp_%d ring\n", port_id);
191 ctx->resp_q = mz->addr;
192 kni_fifo_init(ctx->resp_q, KNI_FIFO_COUNT_MAX);
193 dev_info.resp_phys = mz->phys_addr;
195 /* Req/Resp sync mem area */
196 rte_snprintf(obj_name, OBJNAMSIZ, "kni_sync_%d", port_id);
197 mz = rte_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
198 if (mz == NULL || mz->addr == NULL)
199 rte_panic("Cannot create kni_sync_%d mem\n", port_id);
200 ctx->sync_addr = mz->addr;
201 dev_info.sync_va = mz->addr;
202 dev_info.sync_phys = mz->phys_addr;
205 mz = rte_memzone_lookup("MP_mbuf_pool");
207 RTE_LOG(ERR, KNI, "Can not find MP_mbuf_pool\n");
210 dev_info.mbuf_va = mz->addr;
211 dev_info.mbuf_phys = mz->phys_addr;
212 ctx->pktmbuf_pool = pktmbuf_pool;
213 ctx->port_id = port_id;
214 ctx->mbuf_size = mbuf_size;
216 /* Configure the buffer size which will be checked in kernel module */
217 dev_info.mbuf_size = ctx->mbuf_size;
219 if (ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info) < 0) {
220 RTE_LOG(ERR, KNI, "Fail to create kni device\n");
234 * It is called in the same lcore of receiving packets, and polls the request
235 * mbufs sent from kernel space. Then analyzes it and calls the specific
236 * actions for the specific requests. Finally constructs the response mbuf and
237 * puts it back to the resp_q.
240 kni_request_handler(struct rte_kni *kni)
243 struct rte_kni_request *req;
248 /* Get request mbuf */
249 ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
251 return 0; /* It is OK of can not getting the request mbuf */
253 if (req != kni->sync_addr) {
254 rte_panic("Wrong req pointer %p\n", req);
257 /* Analyze the request and call the relevant actions for it */
258 switch (req->req_id) {
259 case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
260 if (kni->ops.change_mtu)
261 req->result = kni->ops.change_mtu(kni->port_id,
264 case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
265 if (kni->ops.config_network_if)
266 req->result = kni->ops.config_network_if(kni->port_id,
270 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
271 req->result = -EINVAL;
275 /* Construct response mbuf and put it back to resp_q */
276 ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
278 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
279 return -1; /* It is an error of can't putting the mbuf back */
286 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
288 unsigned ret = kni_fifo_put(kni->rx_q, (void **)mbufs, num);
290 /* Get mbufs from free_q and then free them */
293 /* Handle the requests from kernel space */
294 kni_request_handler(kni);
300 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
302 unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
304 /* Allocate mbufs and then put them into alloc_q */
305 kni_allocate_mbufs(kni);
311 kni_free_mbufs(struct rte_kni *kni)
314 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
316 ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
317 if (likely(ret > 0)) {
318 for (i = 0; i < ret; i++)
319 rte_pktmbuf_free(pkts[i]);
324 kni_allocate_mbufs(struct rte_kni *kni)
327 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
329 /* Check if pktmbuf pool has been configured */
330 if (kni->pktmbuf_pool == NULL) {
331 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
335 for (i = 0; i < MAX_MBUF_BURST_NUM; i++) {
336 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
337 if (unlikely(pkts[i] == NULL)) {
339 RTE_LOG(ERR, KNI, "Out of memory\n");
344 /* No pkt mbuf alocated */
348 ret = kni_fifo_put(kni->alloc_q, (void **)pkts, i);
350 /* Check if any mbufs not put into alloc_q, and then free them */
351 if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
354 for (j = ret; j < i; j++)
355 rte_pktmbuf_free(pkts[j]);
360 rte_kni_get_port_id(struct rte_kni *kni)