mbuf_offload: introduce library to attach offloads to mbuf
[dpdk.git] / lib / librte_mbuf_offload / rte_mbuf_offload.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <string.h>
34 #include <rte_common.h>
35
36 #include "rte_mbuf_offload.h"
37
38 /** Initialize rte_mbuf_offload structure */
39 static void
40 rte_pktmbuf_offload_init(struct rte_mempool *mp,
41                 __rte_unused void *opaque_arg,
42                 void *_op_data,
43                 __rte_unused unsigned i)
44 {
45         struct rte_mbuf_offload *ol = _op_data;
46
47         memset(_op_data, 0, mp->elt_size);
48
49         ol->type = RTE_PKTMBUF_OL_NOT_SPECIFIED;
50         ol->mp = mp;
51 }
52
53
54 struct rte_mempool *
55 rte_pktmbuf_offload_pool_create(const char *name, unsigned size,
56                 unsigned cache_size, uint16_t priv_size, int socket_id)
57 {
58         struct rte_pktmbuf_offload_pool_private *priv;
59         unsigned elt_size = sizeof(struct rte_mbuf_offload) + priv_size;
60
61
62         /* lookup mempool in case already allocated */
63         struct rte_mempool *mp = rte_mempool_lookup(name);
64
65         if (mp != NULL) {
66                 priv = (struct rte_pktmbuf_offload_pool_private *)
67                                 rte_mempool_get_priv(mp);
68
69                 if (priv->offload_priv_size <  priv_size ||
70                                 mp->elt_size != elt_size ||
71                                 mp->cache_size < cache_size ||
72                                 mp->size < size) {
73                         mp = NULL;
74                         return NULL;
75                 }
76                 return mp;
77         }
78
79         mp = rte_mempool_create(
80                         name,
81                         size,
82                         elt_size,
83                         cache_size,
84                         sizeof(struct rte_pktmbuf_offload_pool_private),
85                         NULL,
86                         NULL,
87                         rte_pktmbuf_offload_init,
88                         NULL,
89                         socket_id,
90                         0);
91
92         if (mp == NULL)
93                 return NULL;
94
95         priv = (struct rte_pktmbuf_offload_pool_private *)
96                         rte_mempool_get_priv(mp);
97
98         priv->offload_priv_size = priv_size;
99         return mp;
100 }