Preparation for packet mis-ordering stats
[samplevnf.git] / VNFs / DPPD-PROX / local_mbuf.h
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #ifndef _LOCAL_MBUF_H_
18 #define _LOCAL_MBUF_H_
19 #define LOCAL_MBUF_COUNT 64
20
21 struct local_mbuf {
22         struct rte_mempool *mempool;
23         uint32_t           n_new_pkts;
24         struct rte_mbuf    *new_pkts[LOCAL_MBUF_COUNT];
25 };
26
27 static struct rte_mbuf **local_mbuf_take(struct local_mbuf *local_mbuf, uint32_t count)
28 {
29         PROX_ASSERT(local_mbuf->n_new_pkts >= count);
30
31         const uint32_t start_pos = local_mbuf->n_new_pkts - count;
32         struct rte_mbuf **ret = &local_mbuf->new_pkts[start_pos];
33
34         local_mbuf->n_new_pkts -= count;
35         return ret;
36 }
37
38 static int local_mbuf_refill(struct local_mbuf *local_mbuf)
39 {
40         const uint32_t fill = LOCAL_MBUF_COUNT - local_mbuf->n_new_pkts;
41         struct rte_mbuf **fill_mbuf = &local_mbuf->new_pkts[local_mbuf->n_new_pkts];
42
43         if (rte_mempool_get_bulk(local_mbuf->mempool, (void **)fill_mbuf, fill) < 0)
44                 return -1;
45         local_mbuf->n_new_pkts += fill;
46         return 0;
47 }
48
49 /* Ensures that count or more mbufs are available. Returns pointer to
50    count allocated mbufs or NULL if not enough mbufs are available. */
51 static struct rte_mbuf **local_mbuf_refill_and_take(struct local_mbuf *local_mbuf, uint32_t count)
52 {
53         PROX_ASSERT(count <= LOCAL_MBUF_COUNT);
54         if (local_mbuf->n_new_pkts >= count)
55                 return local_mbuf_take(local_mbuf, count);
56
57         if (local_mbuf_refill(local_mbuf) == 0)
58                 return local_mbuf_take(local_mbuf, count);
59         return NULL;
60 }
61
62 #endif /* _LOCAL_MBUF_H_ */