update Intel copyright years to 2014
[dpdk.git] / lib / librte_kni / rte_kni.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
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
16  *       distribution.
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.
20  * 
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.
32  */
33
34 #ifndef RTE_EXEC_ENV_LINUXAPP
35 #error "KNI is not supported"
36 #endif
37
38 #include <string.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <sys/ioctl.h>
42
43 #include <rte_string_fns.h>
44 #include <rte_ethdev.h>
45 #include <rte_malloc.h>
46 #include <rte_log.h>
47 #include <rte_kni.h>
48 #include <rte_memzone.h>
49 #include <exec-env/rte_kni_common.h>
50 #include "rte_kni_fifo.h"
51
52 #define MAX_MBUF_BURST_NUM            32
53
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))
58
59 #define KNI_REQUEST_MBUF_NUM_MAX      32
60
61 #define KNI_MZ_CHECK(mz) do { if (mz) goto fail; } while (0)
62
63 /**
64  * KNI context
65  */
66 struct rte_kni {
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 */
71
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 */
76
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 */
81
82         struct rte_kni_ops ops;             /**< operations for request */
83         uint8_t in_use : 1;                 /**< kni in use */
84 };
85
86 enum kni_ops_status {
87         KNI_REQ_NO_REGISTER = 0,
88         KNI_REQ_REGISTERED,
89 };
90
91 static void kni_free_mbufs(struct rte_kni *kni);
92 static void kni_allocate_mbufs(struct rte_kni *kni);
93
94 static volatile int kni_fd = -1;
95
96 static const struct rte_memzone *
97 kni_memzone_reserve(const char *name, size_t len, int socket_id,
98                                                 unsigned flags)
99 {
100         const struct rte_memzone *mz = rte_memzone_lookup(name);
101
102         if (mz == NULL)
103                 mz = rte_memzone_reserve(name, len, socket_id, flags);
104
105         return mz;
106 }
107
108 /* It is deprecated and just for backward compatibility */
109 struct rte_kni *
110 rte_kni_create(uint8_t port_id,
111                unsigned mbuf_size,
112                struct rte_mempool *pktmbuf_pool,
113                struct rte_kni_ops *ops)
114 {
115         struct rte_kni_conf conf;
116         struct rte_eth_dev_info info;
117
118         memset(&info, 0, sizeof(info));
119         memset(&conf, 0, sizeof(conf));
120         rte_eth_dev_info_get(port_id, &info);
121
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;
127
128         /* Save the port id for request handling */
129         ops->port_id = port_id;
130
131         return rte_kni_alloc(pktmbuf_pool, &conf, ops);
132 }
133
134 struct rte_kni *
135 rte_kni_alloc(struct rte_mempool *pktmbuf_pool,
136               const struct rte_kni_conf *conf,
137               struct rte_kni_ops *ops)
138 {
139         int ret;
140         struct rte_kni_device_info dev_info;
141         struct rte_kni *ctx;
142         char intf_name[RTE_KNI_NAMESIZE];
143 #define OBJNAMSIZ 32
144         char obj_name[OBJNAMSIZ];
145         char mz_name[RTE_MEMZONE_NAMESIZE];
146         const struct rte_memzone *mz;
147
148         if (!pktmbuf_pool || !conf || !conf->name[0])
149                 return NULL;
150
151         /* Check FD and open once */
152         if (kni_fd < 0) {
153                 kni_fd = open("/dev/" KNI_DEVICE, O_RDWR);
154                 if (kni_fd < 0) {
155                         RTE_LOG(ERR, KNI, "Can not open /dev/%s\n",
156                                                         KNI_DEVICE);
157                         return NULL;
158                 }
159         }
160
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), 
164                                 SOCKET_ID_ANY, 0);
165         KNI_MZ_CHECK(mz == NULL);
166         ctx = mz->addr;
167
168         if (ctx->in_use) {
169                 RTE_LOG(ERR, KNI, "KNI %s is in use\n", ctx->name);
170                 goto fail;
171         }
172         memset(ctx, 0, sizeof(struct rte_kni));
173         if (ops)
174                 memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops));
175
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;
186
187         rte_snprintf(ctx->name, RTE_KNI_NAMESIZE, intf_name);
188         rte_snprintf(dev_info.name, RTE_KNI_NAMESIZE, intf_name);
189
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);
193
194         /* TX RING */
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;
201
202         /* RX RING */
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;
209
210         /* ALLOC RING */
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;
217
218         /* FREE RING */
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;
225
226         /* Request RING */
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;
233
234         /* Response RING */
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;
241
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;
249
250         /* MBUF mempool */
251         rte_snprintf(mz_name, sizeof(mz_name), "MP_%s", pktmbuf_pool->name);
252         mz = rte_memzone_lookup(mz_name);
253         KNI_MZ_CHECK(mz == NULL);
254         dev_info.mbuf_va = mz->addr;
255         dev_info.mbuf_phys = mz->phys_addr;
256         ctx->pktmbuf_pool = pktmbuf_pool;
257         ctx->group_id = conf->group_id;
258         ctx->mbuf_size = conf->mbuf_size;
259
260         ret = ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info);
261         KNI_MZ_CHECK(ret < 0);
262
263         ctx->in_use = 1;
264
265         return ctx;
266
267 fail:
268
269         return NULL;
270 }
271
272 static void
273 kni_free_fifo(struct rte_kni_fifo *fifo)
274 {
275         int ret;
276         struct rte_mbuf *pkt;
277
278         do {
279                 ret = kni_fifo_get(fifo, (void **)&pkt, 1);
280                 if (ret)
281                         rte_pktmbuf_free(pkt);
282         } while (ret);
283 }
284
285 int
286 rte_kni_release(struct rte_kni *kni)
287 {
288         struct rte_kni_device_info dev_info;
289
290         if (!kni || !kni->in_use)
291                 return -1;
292
293         rte_snprintf(dev_info.name, sizeof(dev_info.name), kni->name);
294         if (ioctl(kni_fd, RTE_KNI_IOCTL_RELEASE, &dev_info) < 0) {
295                 RTE_LOG(ERR, KNI, "Fail to release kni device\n");
296                 return -1;
297         }
298
299         /* mbufs in all fifo should be released, except request/response */
300         kni_free_fifo(kni->tx_q);
301         kni_free_fifo(kni->rx_q);
302         kni_free_fifo(kni->alloc_q);
303         kni_free_fifo(kni->free_q);
304         memset(kni, 0, sizeof(struct rte_kni));
305
306         return 0;
307 }
308
309 int
310 rte_kni_handle_request(struct rte_kni *kni)
311 {
312         unsigned ret;
313         struct rte_kni_request *req;
314
315         if (kni == NULL)
316                 return -1;
317
318         /* Get request mbuf */
319         ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
320         if (ret != 1)
321                 return 0; /* It is OK of can not getting the request mbuf */
322
323         if (req != kni->sync_addr) {
324                 rte_panic("Wrong req pointer %p\n", req);
325         }
326
327         /* Analyze the request and call the relevant actions for it */
328         switch (req->req_id) {
329         case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
330                 if (kni->ops.change_mtu)
331                         req->result = kni->ops.change_mtu(kni->ops.port_id,
332                                                         req->new_mtu);
333                 break;
334         case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
335                 if (kni->ops.config_network_if)
336                         req->result = kni->ops.config_network_if(\
337                                         kni->ops.port_id, req->if_up);
338                 break;
339         default:
340                 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
341                 req->result = -EINVAL;
342                 break;
343         }
344
345         /* Construct response mbuf and put it back to resp_q */
346         ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
347         if (ret != 1) {
348                 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
349                 return -1; /* It is an error of can't putting the mbuf back */
350         }
351
352         return 0;
353 }
354
355 unsigned
356 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
357 {
358         unsigned ret = kni_fifo_put(kni->rx_q, (void **)mbufs, num);
359
360         /* Get mbufs from free_q and then free them */
361         kni_free_mbufs(kni);
362
363         return ret;
364 }
365
366 unsigned
367 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
368 {
369         unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
370
371         /* Allocate mbufs and then put them into alloc_q */
372         kni_allocate_mbufs(kni);
373
374         return ret;
375 }
376
377 static void
378 kni_free_mbufs(struct rte_kni *kni)
379 {
380         int i, ret;
381         struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
382
383         ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
384         if (likely(ret > 0)) {
385                 for (i = 0; i < ret; i++)
386                         rte_pktmbuf_free(pkts[i]);
387         }
388 }
389
390 static void
391 kni_allocate_mbufs(struct rte_kni *kni)
392 {
393         int i, ret;
394         struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
395
396         /* Check if pktmbuf pool has been configured */
397         if (kni->pktmbuf_pool == NULL) {
398                 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
399                 return;
400         }
401
402         for (i = 0; i < MAX_MBUF_BURST_NUM; i++) {
403                 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
404                 if (unlikely(pkts[i] == NULL)) {
405                         /* Out of memory */
406                         RTE_LOG(ERR, KNI, "Out of memory\n");
407                         break;
408                 }
409         }
410
411         /* No pkt mbuf alocated */
412         if (i <= 0)
413                 return;
414
415         ret = kni_fifo_put(kni->alloc_q, (void **)pkts, i);
416
417         /* Check if any mbufs not put into alloc_q, and then free them */
418         if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
419                 int j;
420
421                 for (j = ret; j < i; j++)
422                         rte_pktmbuf_free(pkts[j]);
423         }
424 }
425
426 /* It is deprecated and just for backward compatibility */
427 uint8_t
428 rte_kni_get_port_id(struct rte_kni *kni)
429 {
430         if (!kni)
431                 return ~0x0;
432
433         return kni->ops.port_id;
434 }
435
436 struct rte_kni *
437 rte_kni_get(const char *name)
438 {
439         struct rte_kni *kni;
440         const struct rte_memzone *mz;
441         char mz_name[RTE_MEMZONE_NAMESIZE];
442
443         if (!name || !name[0])
444                 return NULL;
445
446         rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%s", name);
447         mz = rte_memzone_lookup(mz_name);
448         if (!mz)
449                 return NULL;
450
451         kni = mz->addr;
452         if (!kni->in_use)
453                 return NULL;
454
455         return kni;
456 }
457
458 /*
459  * It is deprecated and just for backward compatibility.
460  */
461 struct rte_kni *
462 rte_kni_info_get(uint8_t port_id)
463 {
464         char name[RTE_MEMZONE_NAMESIZE];
465
466         if (port_id >= RTE_MAX_ETHPORTS)
467                 return NULL;
468
469         rte_snprintf(name, RTE_MEMZONE_NAMESIZE, "vEth%u", port_id);
470
471         return rte_kni_get(name);
472 }
473
474 static enum kni_ops_status
475 kni_check_request_register(struct rte_kni_ops *ops)
476 {
477         /* check if KNI request ops has been registered*/
478         if( NULL == ops )
479                 return KNI_REQ_NO_REGISTER;
480                  
481         if((NULL == ops->change_mtu) && (NULL == ops->config_network_if))
482                 return KNI_REQ_NO_REGISTER;
483
484         return KNI_REQ_REGISTERED;
485 }
486
487 int
488 rte_kni_register_handlers(struct rte_kni *kni,struct rte_kni_ops *ops)
489 {
490         enum kni_ops_status req_status;
491         
492         if (NULL == ops) {
493                 RTE_LOG(ERR, KNI, "Invalid KNI request operation.\n");
494                 return -1;
495         }
496
497         if (NULL == kni) {
498                 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
499                 return -1;
500         }
501
502         req_status = kni_check_request_register(&kni->ops);
503         if ( KNI_REQ_REGISTERED == req_status) {
504                 RTE_LOG(ERR, KNI, "The KNI request operation"
505                                         "has already registered.\n");
506                 return -1;
507         }
508
509         memcpy(&kni->ops, ops, sizeof(struct rte_kni_ops));     
510         return 0;
511 }
512
513 int
514 rte_kni_unregister_handlers(struct rte_kni *kni)
515 {
516         if (NULL == kni) {
517                 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
518                 return -1;
519         }
520         
521         kni->ops.change_mtu = NULL;
522         kni->ops.config_network_if = NULL;
523         return 0;
524 }