Merge "[UDP_Replay] fixing of compiler warning"
[samplevnf.git] / VNFs / DPPD-PROX / fqueue.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 _FQUEUE_H_
18 #define _FQUEUE_H_
19
20 #include <rte_mbuf.h>
21
22 #include <inttypes.h>
23
24 struct fqueue {
25         uint32_t prod;
26         uint32_t cons;
27         uint32_t mask;
28         struct rte_mbuf *entries[0];
29 };
30
31 static uint32_t fqueue_put(struct fqueue *q, struct rte_mbuf **mbufs, uint32_t count)
32 {
33         uint32_t free_entries = q->mask + q->cons - q->prod;
34         uint32_t beg = q->prod & q->mask;
35
36         count = count > free_entries? free_entries : count;
37
38         if ((q->prod & q->mask) + count <= q->mask) {
39                 rte_memcpy(&q->entries[q->prod & q->mask], mbufs, sizeof(mbufs[0]) * count);
40                 q->prod += count;
41         }
42         else {
43                 for (uint32_t i = 0; i < count; ++i) {
44                         q->entries[q->prod & q->mask] = mbufs[i];
45                         q->prod++;
46                 }
47         }
48         return count;
49 }
50
51 static uint32_t fqueue_get(struct fqueue *q, struct rte_mbuf **mbufs, uint32_t count)
52 {
53         uint32_t entries = q->prod - q->cons;
54
55         count = count > entries? entries : count;
56
57         if ((q->cons & q->mask) + count <= q->mask) {
58                 rte_memcpy(mbufs, &q->entries[q->cons & q->mask], sizeof(mbufs[0]) * count);
59                 q->cons += count;
60         }
61         else {
62                 for (uint32_t i = 0; i < count; ++i) {
63                         mbufs[i] = q->entries[q->cons & q->mask];
64                         q->cons++;
65                 }
66         }
67         return count;
68 }
69
70 static struct fqueue *fqueue_create(uint32_t size, int socket)
71 {
72         size_t mem_size = 0;
73
74         mem_size += sizeof(struct fqueue);
75         mem_size += sizeof(((struct fqueue *)(0))->entries[0]) * size;
76
77         struct fqueue *ret = prox_zmalloc(mem_size, socket);
78
79         if (!ret)
80                 return NULL;
81
82         ret->mask = size - 1;
83         return ret;
84 }
85
86 #endif /* _FQUEUE_H_ */