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.
34 #ifndef RTE_EXEC_ENV_LINUXAPP
35 #error "KNI is not supported"
41 #include <sys/ioctl.h>
42 #include <netinet/in.h>
45 #include <rte_string_fns.h>
46 #include <rte_ethdev.h>
47 #include <rte_malloc.h>
50 #include <rte_memzone.h>
51 #include <exec-env/rte_kni_common.h>
52 #include "rte_kni_fifo.h"
54 #define MAX_MBUF_BURST_NUM 32
56 /* Maximum number of ring entries */
57 #define KNI_FIFO_COUNT_MAX 1024
58 #define KNI_FIFO_SIZE (KNI_FIFO_COUNT_MAX * sizeof(void *) + \
59 sizeof(struct rte_kni_fifo))
61 #define KNI_REQUEST_MBUF_NUM_MAX 32
63 #define KNI_MZ_CHECK(mz) do { if (mz) goto fail; } while (0)
69 char name[IFNAMSIZ]; /**< KNI interface name */
70 uint8_t port_id; /**< Port id KNI associate with */
71 struct rte_mempool *pktmbuf_pool; /**< pkt mbuf mempool */
72 unsigned mbuf_size; /**< mbuf size */
74 struct rte_kni_fifo *tx_q; /**< TX queue */
75 struct rte_kni_fifo *rx_q; /**< RX queue */
76 struct rte_kni_fifo *alloc_q; /**< Allocated mbufs queue */
77 struct rte_kni_fifo *free_q; /**< To be freed mbufs queue */
79 /* For request & response */
80 struct rte_kni_fifo *req_q; /**< Request queue */
81 struct rte_kni_fifo *resp_q; /**< Response queue */
82 void * sync_addr; /**< Req/Resp Mem address */
84 struct rte_kni_ops ops; /**< operations for request */
85 uint8_t port_in_use : 1; /**< kni creation flag */
89 KNI_REQ_NO_REGISTER = 0,
93 static void kni_free_mbufs(struct rte_kni *kni);
94 static void kni_allocate_mbufs(struct rte_kni *kni);
96 static volatile int kni_fd = -1;
98 static const struct rte_memzone *
99 kni_memzone_reserve(const char *name, size_t len, int socket_id,
102 const struct rte_memzone *mz = rte_memzone_lookup(name);
105 mz = rte_memzone_reserve(name, len, socket_id, flags);
111 rte_kni_create(uint8_t port_id,
113 struct rte_mempool *pktmbuf_pool,
114 struct rte_kni_ops *ops)
117 struct rte_kni_device_info dev_info;
118 struct rte_eth_dev_info eth_dev_info;
120 char itf_name[IFNAMSIZ];
122 char obj_name[OBJNAMSIZ];
123 char mz_name[RTE_MEMZONE_NAMESIZE];
124 const struct rte_memzone *mz;
126 if (port_id >= RTE_MAX_ETHPORTS || pktmbuf_pool == NULL)
129 /* Check FD and open once */
131 kni_fd = open("/dev/" KNI_DEVICE, O_RDWR);
133 RTE_LOG(ERR, KNI, "Can not open /dev/%s\n",
139 rte_eth_dev_info_get(port_id, ð_dev_info);
140 RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
141 eth_dev_info.pci_dev->addr.bus,
142 eth_dev_info.pci_dev->addr.devid,
143 eth_dev_info.pci_dev->addr.function,
144 eth_dev_info.pci_dev->id.vendor_id,
145 eth_dev_info.pci_dev->id.device_id);
146 dev_info.bus = eth_dev_info.pci_dev->addr.bus;
147 dev_info.devid = eth_dev_info.pci_dev->addr.devid;
148 dev_info.function = eth_dev_info.pci_dev->addr.function;
149 dev_info.vendor_id = eth_dev_info.pci_dev->id.vendor_id;
150 dev_info.device_id = eth_dev_info.pci_dev->id.device_id;
151 dev_info.port_id = port_id;
153 rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%d", port_id);
154 mz = kni_memzone_reserve(mz_name, sizeof(struct rte_kni),
156 KNI_MZ_CHECK(mz == NULL);
159 if (ctx->port_in_use != 0) {
160 RTE_LOG(ERR, KNI, "Port %d has been used\n", port_id);
163 memset(ctx, 0, sizeof(struct rte_kni));
165 memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops));
167 rte_snprintf(itf_name, IFNAMSIZ, "vEth%u", port_id);
168 rte_snprintf(ctx->name, IFNAMSIZ, itf_name);
169 rte_snprintf(dev_info.name, IFNAMSIZ, itf_name);
172 rte_snprintf(obj_name, OBJNAMSIZ, "kni_tx_%d", port_id);
173 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
174 KNI_MZ_CHECK(mz == NULL);
175 ctx->tx_q = mz->addr;
176 kni_fifo_init(ctx->tx_q, KNI_FIFO_COUNT_MAX);
177 dev_info.tx_phys = mz->phys_addr;
180 rte_snprintf(obj_name, OBJNAMSIZ, "kni_rx_%d", port_id);
181 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
182 KNI_MZ_CHECK(mz == NULL);
183 ctx->rx_q = mz->addr;
184 kni_fifo_init(ctx->rx_q, KNI_FIFO_COUNT_MAX);
185 dev_info.rx_phys = mz->phys_addr;
188 rte_snprintf(obj_name, OBJNAMSIZ, "kni_alloc_%d", port_id);
189 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
190 KNI_MZ_CHECK(mz == NULL);
191 ctx->alloc_q = mz->addr;
192 kni_fifo_init(ctx->alloc_q, KNI_FIFO_COUNT_MAX);
193 dev_info.alloc_phys = mz->phys_addr;
196 rte_snprintf(obj_name, OBJNAMSIZ, "kni_free_%d", port_id);
197 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
198 KNI_MZ_CHECK(mz == NULL);
199 ctx->free_q = mz->addr;
200 kni_fifo_init(ctx->free_q, KNI_FIFO_COUNT_MAX);
201 dev_info.free_phys = mz->phys_addr;
204 rte_snprintf(obj_name, OBJNAMSIZ, "kni_req_%d", port_id);
205 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
206 KNI_MZ_CHECK(mz == NULL);
207 ctx->req_q = mz->addr;
208 kni_fifo_init(ctx->req_q, KNI_FIFO_COUNT_MAX);
209 dev_info.req_phys = mz->phys_addr;
212 rte_snprintf(obj_name, OBJNAMSIZ, "kni_resp_%d", port_id);
213 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
214 KNI_MZ_CHECK(mz == NULL);
215 ctx->resp_q = mz->addr;
216 kni_fifo_init(ctx->resp_q, KNI_FIFO_COUNT_MAX);
217 dev_info.resp_phys = mz->phys_addr;
219 /* Req/Resp sync mem area */
220 rte_snprintf(obj_name, OBJNAMSIZ, "kni_sync_%d", port_id);
221 mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
222 KNI_MZ_CHECK(mz == NULL);
223 ctx->sync_addr = mz->addr;
224 dev_info.sync_va = mz->addr;
225 dev_info.sync_phys = mz->phys_addr;
228 rte_snprintf(mz_name, sizeof(mz_name), "MP_%s", pktmbuf_pool->name);
229 mz = rte_memzone_lookup(mz_name);
230 KNI_MZ_CHECK(mz == NULL);
231 dev_info.mbuf_va = mz->addr;
232 dev_info.mbuf_phys = mz->phys_addr;
233 ctx->pktmbuf_pool = pktmbuf_pool;
234 ctx->port_id = port_id;
235 ctx->mbuf_size = mbuf_size;
237 /* Configure the buffer size which will be checked in kernel module */
238 dev_info.mbuf_size = ctx->mbuf_size;
240 ret = ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info);
241 KNI_MZ_CHECK(ret < 0);
243 ctx->port_in_use = 1;
253 kni_free_fifo(struct rte_kni_fifo *fifo)
256 struct rte_mbuf *pkt;
259 ret = kni_fifo_get(fifo, (void **)&pkt, 1);
261 rte_pktmbuf_free(pkt);
266 rte_kni_release(struct rte_kni *kni)
268 if (!kni || kni->port_in_use == 0)
271 if (ioctl(kni_fd, RTE_KNI_IOCTL_RELEASE, &kni->port_id) < 0) {
272 RTE_LOG(ERR, KNI, "Fail to release kni device\n");
276 /* mbufs in all fifo should be released, except request/response */
277 kni_free_fifo(kni->tx_q);
278 kni_free_fifo(kni->rx_q);
279 kni_free_fifo(kni->alloc_q);
280 kni_free_fifo(kni->free_q);
281 memset(kni, 0, sizeof(struct rte_kni));
287 rte_kni_handle_request(struct rte_kni *kni)
290 struct rte_kni_request *req;
295 /* Get request mbuf */
296 ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
298 return 0; /* It is OK of can not getting the request mbuf */
300 if (req != kni->sync_addr) {
301 rte_panic("Wrong req pointer %p\n", req);
304 /* Analyze the request and call the relevant actions for it */
305 switch (req->req_id) {
306 case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
307 if (kni->ops.change_mtu)
308 req->result = kni->ops.change_mtu(kni->port_id,
311 case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
312 if (kni->ops.config_network_if)
313 req->result = kni->ops.config_network_if(kni->port_id,
317 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
318 req->result = -EINVAL;
322 /* Construct response mbuf and put it back to resp_q */
323 ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
325 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
326 return -1; /* It is an error of can't putting the mbuf back */
333 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
335 unsigned ret = kni_fifo_put(kni->rx_q, (void **)mbufs, num);
337 /* Get mbufs from free_q and then free them */
344 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
346 unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
348 /* Allocate mbufs and then put them into alloc_q */
349 kni_allocate_mbufs(kni);
355 kni_free_mbufs(struct rte_kni *kni)
358 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
360 ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
361 if (likely(ret > 0)) {
362 for (i = 0; i < ret; i++)
363 rte_pktmbuf_free(pkts[i]);
368 kni_allocate_mbufs(struct rte_kni *kni)
371 struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
373 /* Check if pktmbuf pool has been configured */
374 if (kni->pktmbuf_pool == NULL) {
375 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
379 for (i = 0; i < MAX_MBUF_BURST_NUM; i++) {
380 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
381 if (unlikely(pkts[i] == NULL)) {
383 RTE_LOG(ERR, KNI, "Out of memory\n");
388 /* No pkt mbuf alocated */
392 ret = kni_fifo_put(kni->alloc_q, (void **)pkts, i);
394 /* Check if any mbufs not put into alloc_q, and then free them */
395 if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
398 for (j = ret; j < i; j++)
399 rte_pktmbuf_free(pkts[j]);
404 rte_kni_get_port_id(struct rte_kni *kni)
413 rte_kni_info_get(uint8_t port_id)
416 const struct rte_memzone *mz;
417 char mz_name[RTE_MEMZONE_NAMESIZE];
419 if(port_id >= RTE_MAX_ETHPORTS)
422 rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%d", port_id);
423 mz = rte_memzone_lookup(mz_name);
428 if (0 == kni->port_in_use)
434 static enum kni_ops_status
435 kni_check_request_register(struct rte_kni_ops *ops)
437 /* check if KNI request ops has been registered*/
439 return KNI_REQ_NO_REGISTER;
441 if((NULL == ops->change_mtu) && (NULL == ops->config_network_if))
442 return KNI_REQ_NO_REGISTER;
444 return KNI_REQ_REGISTERED;
448 rte_kni_register_handlers(struct rte_kni *kni,struct rte_kni_ops *ops)
450 enum kni_ops_status req_status;
453 RTE_LOG(ERR, KNI, "Invalid KNI request operation.\n");
458 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
462 req_status = kni_check_request_register(&kni->ops);
463 if ( KNI_REQ_REGISTERED == req_status) {
464 RTE_LOG(ERR, KNI, "The KNI request operation"
465 "has already registered.\n");
469 memcpy(&kni->ops, ops, sizeof(struct rte_kni_ops));
474 rte_kni_unregister_handlers(struct rte_kni *kni)
477 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
481 if (NULL == &kni->ops) {
482 RTE_LOG(ERR, KNI, "The invalid KNI unregister operation.\n");
486 kni->ops.change_mtu = NULL;
487 kni->ops.config_network_if = NULL;