4 * Copyright(c) 2010-2013 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
64 #define KNI_MZ_CHECK(mz) do { if (mz) goto fail; } while (0)
70 char name[IFNAMSIZ]; /**< KNI interface name */
71 uint8_t port_id; /**< Port id KNI associate with */
72 struct rte_mempool *pktmbuf_pool; /**< pkt mbuf mempool */
73 unsigned mbuf_size; /**< mbuf size */
75 struct rte_kni_fifo *tx_q; /**< TX queue */
76 struct rte_kni_fifo *rx_q; /**< RX queue */
77 struct rte_kni_fifo *alloc_q; /**< Allocated mbufs queue */
78 struct rte_kni_fifo *free_q; /**< To be freed mbufs queue */
80 /* For request & response */
81 struct rte_kni_fifo *req_q; /**< Request queue */
82 struct rte_kni_fifo *resp_q; /**< Response queue */
83 void * sync_addr; /**< Req/Resp Mem address */
85 struct rte_kni_ops ops; /**< operations for request */
86 uint8_t port_in_use : 1; /**< kni creation flag */
90 KNI_REQ_NO_REGISTER = 0,
94 static void kni_free_mbufs(struct rte_kni *kni);
95 static void kni_allocate_mbufs(struct rte_kni *kni);
97 static volatile int kni_fd = -1;
99 static const struct rte_memzone *
100 kni_memzone_reserve(const char *name, size_t len, int socket_id,
103 const struct rte_memzone *mz = rte_memzone_lookup(name);
106 mz = rte_memzone_reserve(name, len, socket_id, flags);
112 rte_kni_create(uint8_t port_id,
114 struct rte_mempool *pktmbuf_pool,
115 struct rte_kni_ops *ops)
118 struct rte_kni_device_info dev_info;
119 struct rte_eth_dev_info eth_dev_info;
121 char itf_name[IFNAMSIZ];
123 char obj_name[OBJNAMSIZ];
124 char mz_name[RTE_MEMZONE_NAMESIZE];
125 const struct rte_memzone *mz;
127 if (port_id >= RTE_MAX_ETHPORTS || pktmbuf_pool == NULL)
130 /* Check FD and open once */
132 kni_fd = open("/dev/" KNI_DEVICE, O_RDWR);
134 RTE_LOG(ERR, KNI, "Can not open /dev/%s\n",
140 rte_eth_dev_info_get(port_id, ð_dev_info);
141 RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
142 eth_dev_info.pci_dev->addr.bus,
143 eth_dev_info.pci_dev->addr.devid,
144 eth_dev_info.pci_dev->addr.function,
145 eth_dev_info.pci_dev->id.vendor_id,
146 eth_dev_info.pci_dev->id.device_id);
147 dev_info.bus = eth_dev_info.pci_dev->addr.bus;
148 dev_info.devid = eth_dev_info.pci_dev->addr.devid;
149 dev_info.function = eth_dev_info.pci_dev->addr.function;
150 dev_info.vendor_id = eth_dev_info.pci_dev->id.vendor_id;
151 dev_info.device_id = eth_dev_info.pci_dev->id.device_id;
152 dev_info.port_id = port_id;
154 rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%d", port_id);
155 mz = kni_memzone_reserve(mz_name, sizeof(struct rte_kni),
157 KNI_MZ_CHECK(mz == NULL);
160 if (ctx->port_in_use != 0) {
161 RTE_LOG(ERR, KNI, "Port %d has been used\n", port_id);
164 memset(ctx, 0, sizeof(struct rte_kni));
166 memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops));
168 rte_snprintf(itf_name, IFNAMSIZ, "vEth%u", port_id);
169 rte_snprintf(ctx->name, IFNAMSIZ, itf_name);
170 rte_snprintf(dev_info.name, IFNAMSIZ, itf_name);
173 rte_snprintf(obj_name, OBJNAMSIZ, "kni_tx_%d", port_id);
174 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
175 KNI_MZ_CHECK(mz == NULL);
176 ctx->tx_q = mz->addr;
177 kni_fifo_init(ctx->tx_q, KNI_FIFO_COUNT_MAX);
178 dev_info.tx_phys = mz->phys_addr;
181 rte_snprintf(obj_name, OBJNAMSIZ, "kni_rx_%d", port_id);
182 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
183 KNI_MZ_CHECK(mz == NULL);
184 ctx->rx_q = mz->addr;
185 kni_fifo_init(ctx->rx_q, KNI_FIFO_COUNT_MAX);
186 dev_info.rx_phys = mz->phys_addr;
189 rte_snprintf(obj_name, OBJNAMSIZ, "kni_alloc_%d", port_id);
190 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
191 KNI_MZ_CHECK(mz == NULL);
192 ctx->alloc_q = mz->addr;
193 kni_fifo_init(ctx->alloc_q, KNI_FIFO_COUNT_MAX);
194 dev_info.alloc_phys = mz->phys_addr;
197 rte_snprintf(obj_name, OBJNAMSIZ, "kni_free_%d", port_id);
198 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
199 KNI_MZ_CHECK(mz == NULL);
200 ctx->free_q = mz->addr;
201 kni_fifo_init(ctx->free_q, KNI_FIFO_COUNT_MAX);
202 dev_info.free_phys = mz->phys_addr;
205 rte_snprintf(obj_name, OBJNAMSIZ, "kni_req_%d", port_id);
206 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
207 KNI_MZ_CHECK(mz == NULL);
208 ctx->req_q = mz->addr;
209 kni_fifo_init(ctx->req_q, KNI_FIFO_COUNT_MAX);
210 dev_info.req_phys = mz->phys_addr;
213 rte_snprintf(obj_name, OBJNAMSIZ, "kni_resp_%d", port_id);
214 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
215 KNI_MZ_CHECK(mz == NULL);
216 ctx->resp_q = mz->addr;
217 kni_fifo_init(ctx->resp_q, KNI_FIFO_COUNT_MAX);
218 dev_info.resp_phys = mz->phys_addr;
220 /* Req/Resp sync mem area */
221 rte_snprintf(obj_name, OBJNAMSIZ, "kni_sync_%d", port_id);
222 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
223 KNI_MZ_CHECK(mz == NULL);
224 ctx->sync_addr = mz->addr;
225 dev_info.sync_va = mz->addr;
226 dev_info.sync_phys = mz->phys_addr;
229 rte_snprintf(mz_name, sizeof(mz_name), "MP_%s", pktmbuf_pool->name);
230 mz = rte_memzone_lookup(mz_name);
231 KNI_MZ_CHECK(mz == NULL);
232 dev_info.mbuf_va = mz->addr;
233 dev_info.mbuf_phys = mz->phys_addr;
234 ctx->pktmbuf_pool = pktmbuf_pool;
235 ctx->port_id = port_id;
236 ctx->mbuf_size = mbuf_size;
238 /* Configure the buffer size which will be checked in kernel module */
239 dev_info.mbuf_size = ctx->mbuf_size;
241 ret = ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info);
242 KNI_MZ_CHECK(ret < 0);
244 ctx->port_in_use = 1;
254 kni_free_fifo(struct rte_kni_fifo *fifo)
257 struct rte_mbuf *pkt;
260 ret = kni_fifo_get(fifo, (void **)&pkt, 1);
262 rte_pktmbuf_free(pkt);
267 rte_kni_release(struct rte_kni *kni)
269 if (!kni || kni->port_in_use == 0)
272 if (ioctl(kni_fd, RTE_KNI_IOCTL_RELEASE, &kni->port_id) < 0) {
273 RTE_LOG(ERR, KNI, "Fail to release kni device\n");
277 /* mbufs in all fifo should be released, except request/response */
278 kni_free_fifo(kni->tx_q);
279 kni_free_fifo(kni->rx_q);
280 kni_free_fifo(kni->alloc_q);
281 kni_free_fifo(kni->free_q);
282 memset(kni, 0, sizeof(struct rte_kni));
288 rte_kni_handle_request(struct rte_kni *kni)
291 struct rte_kni_request *req;
296 /* Get request mbuf */
297 ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
299 return 0; /* It is OK of can not getting the request mbuf */
301 if (req != kni->sync_addr) {
302 rte_panic("Wrong req pointer %p\n", req);
305 /* Analyze the request and call the relevant actions for it */
306 switch (req->req_id) {
307 case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
308 if (kni->ops.change_mtu)
309 req->result = kni->ops.change_mtu(kni->port_id,
312 case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
313 if (kni->ops.config_network_if)
314 req->result = kni->ops.config_network_if(kni->port_id,
318 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
319 req->result = -EINVAL;
323 /* Construct response mbuf and put it back to resp_q */
324 ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
326 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
327 return -1; /* It is an error of can't putting the mbuf back */
334 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
336 unsigned ret = kni_fifo_put(kni->rx_q, (void **)mbufs, num);
338 /* Get mbufs from free_q and then free them */
345 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
347 unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
349 /* Allocate mbufs and then put them into alloc_q */
350 kni_allocate_mbufs(kni);
356 kni_free_mbufs(struct rte_kni *kni)
359 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
361 ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
362 if (likely(ret > 0)) {
363 for (i = 0; i < ret; i++)
364 rte_pktmbuf_free(pkts[i]);
369 kni_allocate_mbufs(struct rte_kni *kni)
372 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
374 /* Check if pktmbuf pool has been configured */
375 if (kni->pktmbuf_pool == NULL) {
376 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
380 for (i = 0; i < MAX_MBUF_BURST_NUM; i++) {
381 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
382 if (unlikely(pkts[i] == NULL)) {
384 RTE_LOG(ERR, KNI, "Out of memory\n");
389 /* No pkt mbuf alocated */
393 ret = kni_fifo_put(kni->alloc_q, (void **)pkts, i);
395 /* Check if any mbufs not put into alloc_q, and then free them */
396 if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
399 for (j = ret; j < i; j++)
400 rte_pktmbuf_free(pkts[j]);
405 rte_kni_get_port_id(struct rte_kni *kni)
414 rte_kni_info_get(uint8_t port_id)
417 const struct rte_memzone *mz;
418 char mz_name[RTE_MEMZONE_NAMESIZE];
420 if(port_id >= RTE_MAX_ETHPORTS)
423 rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%d", port_id);
424 mz = rte_memzone_lookup(mz_name);
429 if (0 == kni->port_in_use)
435 static enum kni_ops_status
436 kni_check_request_register(struct rte_kni_ops *ops)
438 /* check if KNI request ops has been registered*/
440 return KNI_REQ_NO_REGISTER;
442 if((NULL == ops->change_mtu) && (NULL == ops->config_network_if))
443 return KNI_REQ_NO_REGISTER;
445 return KNI_REQ_REGISTERED;
449 rte_kni_register_handlers(struct rte_kni *kni,struct rte_kni_ops *ops)
451 enum kni_ops_status req_status;
454 RTE_LOG(ERR, KNI, "Invalid KNI request operation.\n");
459 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
463 req_status = kni_check_request_register(&kni->ops);
464 if ( KNI_REQ_REGISTERED == req_status) {
465 RTE_LOG(ERR, KNI, "The KNI request operation"
466 "has already registered.\n");
470 memcpy(&kni->ops, ops, sizeof(struct rte_kni_ops));
475 rte_kni_unregister_handlers(struct rte_kni *kni)
478 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
482 if (NULL == &kni->ops) {
483 RTE_LOG(ERR, KNI, "The invalid KNI unregister operation.\n");
487 kni->ops.change_mtu = NULL;
488 kni->ops.config_network_if = NULL;