4 * Copyright(c) 2010-2014 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.
34 #ifndef RTE_EXEC_ENV_LINUXAPP
35 #error "KNI is not supported"
41 #include <sys/ioctl.h>
43 #include <rte_string_fns.h>
44 #include <rte_ethdev.h>
45 #include <rte_malloc.h>
48 #include <rte_memzone.h>
49 #include <exec-env/rte_kni_common.h>
50 #include "rte_kni_fifo.h"
52 #define MAX_MBUF_BURST_NUM 32
54 /* Maximum number of ring entries */
55 #define KNI_FIFO_COUNT_MAX 1024
56 #define KNI_FIFO_SIZE (KNI_FIFO_COUNT_MAX * sizeof(void *) + \
57 sizeof(struct rte_kni_fifo))
59 #define KNI_REQUEST_MBUF_NUM_MAX 32
61 #define KNI_MZ_CHECK(mz) do { if (mz) goto fail; } while (0)
67 char name[RTE_KNI_NAMESIZE]; /**< KNI interface name */
68 uint16_t group_id; /**< Group ID of KNI devices */
69 struct rte_mempool *pktmbuf_pool; /**< pkt mbuf mempool */
70 unsigned mbuf_size; /**< mbuf size */
72 struct rte_kni_fifo *tx_q; /**< TX queue */
73 struct rte_kni_fifo *rx_q; /**< RX queue */
74 struct rte_kni_fifo *alloc_q; /**< Allocated mbufs queue */
75 struct rte_kni_fifo *free_q; /**< To be freed mbufs queue */
77 /* For request & response */
78 struct rte_kni_fifo *req_q; /**< Request queue */
79 struct rte_kni_fifo *resp_q; /**< Response queue */
80 void * sync_addr; /**< Req/Resp Mem address */
82 struct rte_kni_ops ops; /**< operations for request */
83 uint8_t in_use : 1; /**< kni in use */
87 KNI_REQ_NO_REGISTER = 0,
91 static void kni_free_mbufs(struct rte_kni *kni);
92 static void kni_allocate_mbufs(struct rte_kni *kni);
94 static volatile int kni_fd = -1;
96 static const struct rte_memzone *
97 kni_memzone_reserve(const char *name, size_t len, int socket_id,
100 const struct rte_memzone *mz = rte_memzone_lookup(name);
103 mz = rte_memzone_reserve(name, len, socket_id, flags);
108 /* It is deprecated and just for backward compatibility */
110 rte_kni_create(uint8_t port_id,
112 struct rte_mempool *pktmbuf_pool,
113 struct rte_kni_ops *ops)
115 struct rte_kni_conf conf;
116 struct rte_eth_dev_info info;
118 memset(&info, 0, sizeof(info));
119 memset(&conf, 0, sizeof(conf));
120 rte_eth_dev_info_get(port_id, &info);
122 rte_snprintf(conf.name, sizeof(conf.name), "vEth%u", port_id);
123 conf.addr = info.pci_dev->addr;
124 conf.id = info.pci_dev->id;
125 conf.group_id = (uint16_t)port_id;
126 conf.mbuf_size = mbuf_size;
128 /* Save the port id for request handling */
129 ops->port_id = port_id;
131 return rte_kni_alloc(pktmbuf_pool, &conf, ops);
135 rte_kni_alloc(struct rte_mempool *pktmbuf_pool,
136 const struct rte_kni_conf *conf,
137 struct rte_kni_ops *ops)
140 struct rte_kni_device_info dev_info;
142 char intf_name[RTE_KNI_NAMESIZE];
144 char obj_name[OBJNAMSIZ];
145 char mz_name[RTE_MEMZONE_NAMESIZE];
146 const struct rte_memzone *mz;
148 if (!pktmbuf_pool || !conf || !conf->name[0])
151 /* Check FD and open once */
153 kni_fd = open("/dev/" KNI_DEVICE, O_RDWR);
155 RTE_LOG(ERR, KNI, "Can not open /dev/%s\n",
161 rte_snprintf(intf_name, RTE_KNI_NAMESIZE, conf->name);
162 rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%s", intf_name);
163 mz = kni_memzone_reserve(mz_name, sizeof(struct rte_kni),
165 KNI_MZ_CHECK(mz == NULL);
169 RTE_LOG(ERR, KNI, "KNI %s is in use\n", ctx->name);
172 memset(ctx, 0, sizeof(struct rte_kni));
174 memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops));
176 memset(&dev_info, 0, sizeof(dev_info));
177 dev_info.bus = conf->addr.bus;
178 dev_info.devid = conf->addr.devid;
179 dev_info.function = conf->addr.function;
180 dev_info.vendor_id = conf->id.vendor_id;
181 dev_info.device_id = conf->id.device_id;
182 dev_info.core_id = conf->core_id;
183 dev_info.force_bind = conf->force_bind;
184 dev_info.group_id = conf->group_id;
185 dev_info.mbuf_size = conf->mbuf_size;
187 rte_snprintf(ctx->name, RTE_KNI_NAMESIZE, intf_name);
188 rte_snprintf(dev_info.name, RTE_KNI_NAMESIZE, intf_name);
190 RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
191 dev_info.bus, dev_info.devid, dev_info.function,
192 dev_info.vendor_id, dev_info.device_id);
195 rte_snprintf(obj_name, OBJNAMSIZ, "kni_tx_%s", intf_name);
196 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
197 KNI_MZ_CHECK(mz == NULL);
198 ctx->tx_q = mz->addr;
199 kni_fifo_init(ctx->tx_q, KNI_FIFO_COUNT_MAX);
200 dev_info.tx_phys = mz->phys_addr;
203 rte_snprintf(obj_name, OBJNAMSIZ, "kni_rx_%s", intf_name);
204 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
205 KNI_MZ_CHECK(mz == NULL);
206 ctx->rx_q = mz->addr;
207 kni_fifo_init(ctx->rx_q, KNI_FIFO_COUNT_MAX);
208 dev_info.rx_phys = mz->phys_addr;
211 rte_snprintf(obj_name, OBJNAMSIZ, "kni_alloc_%s", intf_name);
212 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
213 KNI_MZ_CHECK(mz == NULL);
214 ctx->alloc_q = mz->addr;
215 kni_fifo_init(ctx->alloc_q, KNI_FIFO_COUNT_MAX);
216 dev_info.alloc_phys = mz->phys_addr;
219 rte_snprintf(obj_name, OBJNAMSIZ, "kni_free_%s", intf_name);
220 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
221 KNI_MZ_CHECK(mz == NULL);
222 ctx->free_q = mz->addr;
223 kni_fifo_init(ctx->free_q, KNI_FIFO_COUNT_MAX);
224 dev_info.free_phys = mz->phys_addr;
227 rte_snprintf(obj_name, OBJNAMSIZ, "kni_req_%s", intf_name);
228 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
229 KNI_MZ_CHECK(mz == NULL);
230 ctx->req_q = mz->addr;
231 kni_fifo_init(ctx->req_q, KNI_FIFO_COUNT_MAX);
232 dev_info.req_phys = mz->phys_addr;
235 rte_snprintf(obj_name, OBJNAMSIZ, "kni_resp_%s", intf_name);
236 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
237 KNI_MZ_CHECK(mz == NULL);
238 ctx->resp_q = mz->addr;
239 kni_fifo_init(ctx->resp_q, KNI_FIFO_COUNT_MAX);
240 dev_info.resp_phys = mz->phys_addr;
242 /* Req/Resp sync mem area */
243 rte_snprintf(obj_name, OBJNAMSIZ, "kni_sync_%s", intf_name);
244 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
245 KNI_MZ_CHECK(mz == NULL);
246 ctx->sync_addr = mz->addr;
247 dev_info.sync_va = mz->addr;
248 dev_info.sync_phys = mz->phys_addr;
251 rte_snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_OBJ_NAME,
253 mz = rte_memzone_lookup(mz_name);
254 KNI_MZ_CHECK(mz == NULL);
255 dev_info.mbuf_va = mz->addr;
256 dev_info.mbuf_phys = mz->phys_addr;
257 ctx->pktmbuf_pool = pktmbuf_pool;
258 ctx->group_id = conf->group_id;
259 ctx->mbuf_size = conf->mbuf_size;
261 ret = ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info);
262 KNI_MZ_CHECK(ret < 0);
274 kni_free_fifo(struct rte_kni_fifo *fifo)
277 struct rte_mbuf *pkt;
280 ret = kni_fifo_get(fifo, (void **)&pkt, 1);
282 rte_pktmbuf_free(pkt);
287 rte_kni_release(struct rte_kni *kni)
289 struct rte_kni_device_info dev_info;
291 if (!kni || !kni->in_use)
294 rte_snprintf(dev_info.name, sizeof(dev_info.name), kni->name);
295 if (ioctl(kni_fd, RTE_KNI_IOCTL_RELEASE, &dev_info) < 0) {
296 RTE_LOG(ERR, KNI, "Fail to release kni device\n");
300 /* mbufs in all fifo should be released, except request/response */
301 kni_free_fifo(kni->tx_q);
302 kni_free_fifo(kni->rx_q);
303 kni_free_fifo(kni->alloc_q);
304 kni_free_fifo(kni->free_q);
305 memset(kni, 0, sizeof(struct rte_kni));
311 rte_kni_handle_request(struct rte_kni *kni)
314 struct rte_kni_request *req;
319 /* Get request mbuf */
320 ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
322 return 0; /* It is OK of can not getting the request mbuf */
324 if (req != kni->sync_addr) {
325 rte_panic("Wrong req pointer %p\n", req);
328 /* Analyze the request and call the relevant actions for it */
329 switch (req->req_id) {
330 case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
331 if (kni->ops.change_mtu)
332 req->result = kni->ops.change_mtu(kni->ops.port_id,
335 case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
336 if (kni->ops.config_network_if)
337 req->result = kni->ops.config_network_if(\
338 kni->ops.port_id, req->if_up);
341 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
342 req->result = -EINVAL;
346 /* Construct response mbuf and put it back to resp_q */
347 ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
349 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
350 return -1; /* It is an error of can't putting the mbuf back */
357 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
359 unsigned ret = kni_fifo_put(kni->rx_q, (void **)mbufs, num);
361 /* Get mbufs from free_q and then free them */
368 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
370 unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
372 /* Allocate mbufs and then put them into alloc_q */
373 kni_allocate_mbufs(kni);
379 kni_free_mbufs(struct rte_kni *kni)
382 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
384 ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
385 if (likely(ret > 0)) {
386 for (i = 0; i < ret; i++)
387 rte_pktmbuf_free(pkts[i]);
392 kni_allocate_mbufs(struct rte_kni *kni)
395 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
397 /* Check if pktmbuf pool has been configured */
398 if (kni->pktmbuf_pool == NULL) {
399 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
403 for (i = 0; i < MAX_MBUF_BURST_NUM; i++) {
404 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
405 if (unlikely(pkts[i] == NULL)) {
407 RTE_LOG(ERR, KNI, "Out of memory\n");
412 /* No pkt mbuf alocated */
416 ret = kni_fifo_put(kni->alloc_q, (void **)pkts, i);
418 /* Check if any mbufs not put into alloc_q, and then free them */
419 if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
422 for (j = ret; j < i; j++)
423 rte_pktmbuf_free(pkts[j]);
427 /* It is deprecated and just for backward compatibility */
429 rte_kni_get_port_id(struct rte_kni *kni)
434 return kni->ops.port_id;
438 rte_kni_get(const char *name)
441 const struct rte_memzone *mz;
442 char mz_name[RTE_MEMZONE_NAMESIZE];
444 if (!name || !name[0])
447 rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%s", name);
448 mz = rte_memzone_lookup(mz_name);
460 * It is deprecated and just for backward compatibility.
463 rte_kni_info_get(uint8_t port_id)
465 char name[RTE_MEMZONE_NAMESIZE];
467 if (port_id >= RTE_MAX_ETHPORTS)
470 rte_snprintf(name, RTE_MEMZONE_NAMESIZE, "vEth%u", port_id);
472 return rte_kni_get(name);
475 static enum kni_ops_status
476 kni_check_request_register(struct rte_kni_ops *ops)
478 /* check if KNI request ops has been registered*/
480 return KNI_REQ_NO_REGISTER;
482 if((NULL == ops->change_mtu) && (NULL == ops->config_network_if))
483 return KNI_REQ_NO_REGISTER;
485 return KNI_REQ_REGISTERED;
489 rte_kni_register_handlers(struct rte_kni *kni,struct rte_kni_ops *ops)
491 enum kni_ops_status req_status;
494 RTE_LOG(ERR, KNI, "Invalid KNI request operation.\n");
499 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
503 req_status = kni_check_request_register(&kni->ops);
504 if ( KNI_REQ_REGISTERED == req_status) {
505 RTE_LOG(ERR, KNI, "The KNI request operation"
506 "has already registered.\n");
510 memcpy(&kni->ops, ops, sizeof(struct rte_kni_ops));
515 rte_kni_unregister_handlers(struct rte_kni *kni)
518 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
522 kni->ops.change_mtu = NULL;
523 kni->ops.config_network_if = NULL;