]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/netdev-afxdp-pool.c
2 * Copyright (c) 2018, 2019 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "dp-packet.h"
19 #include "netdev-afxdp-pool.h"
20 #include "openvswitch/util.h"
23 * umem_elem_push* shouldn't overflow because we always pop
24 * elem first, then push back to the stack.
27 umem_elem_push_n__(struct umem_pool
*umemp
, int n
, void **addrs
)
31 ovs_assert(umemp
->index
+ n
<= umemp
->size
);
33 ptr
= &umemp
->array
[umemp
->index
];
34 memcpy(ptr
, addrs
, n
* sizeof(void *));
38 void umem_elem_push_n(struct umem_pool
*umemp
, int n
, void **addrs
)
40 ovs_spin_lock(&umemp
->lock
);
41 umem_elem_push_n__(umemp
, n
, addrs
);
42 ovs_spin_unlock(&umemp
->lock
);
46 umem_elem_push__(struct umem_pool
*umemp
, void *addr
)
48 ovs_assert(umemp
->index
+ 1 <= umemp
->size
);
50 umemp
->array
[umemp
->index
++] = addr
;
54 umem_elem_push(struct umem_pool
*umemp
, void *addr
)
56 ovs_spin_lock(&umemp
->lock
);
57 umem_elem_push__(umemp
, addr
);
58 ovs_spin_unlock(&umemp
->lock
);
62 umem_elem_pop_n__(struct umem_pool
*umemp
, int n
, void **addrs
)
66 if (OVS_UNLIKELY(umemp
->index
- n
< 0)) {
71 ptr
= &umemp
->array
[umemp
->index
];
72 memcpy(addrs
, ptr
, n
* sizeof(void *));
78 umem_elem_pop_n(struct umem_pool
*umemp
, int n
, void **addrs
)
82 ovs_spin_lock(&umemp
->lock
);
83 ret
= umem_elem_pop_n__(umemp
, n
, addrs
);
84 ovs_spin_unlock(&umemp
->lock
);
90 umem_elem_pop__(struct umem_pool
*umemp
)
92 if (OVS_UNLIKELY(umemp
->index
- 1 < 0)) {
96 return umemp
->array
[--umemp
->index
];
100 umem_elem_pop(struct umem_pool
*umemp
)
104 ovs_spin_lock(&umemp
->lock
);
105 ptr
= umem_elem_pop__(umemp
);
106 ovs_spin_unlock(&umemp
->lock
);
112 umem_pool_alloc__(unsigned int size
)
116 bufs
= xmalloc_pagealign(size
* sizeof *bufs
);
117 memset(bufs
, 0, size
* sizeof *bufs
);
123 umem_pool_init(struct umem_pool
*umemp
, unsigned int size
)
125 umemp
->array
= umem_pool_alloc__(size
);
132 ovs_spin_init(&umemp
->lock
);
137 umem_pool_cleanup(struct umem_pool
*umemp
)
139 free_pagealign(umemp
->array
);
141 ovs_spin_destroy(&umemp
->lock
);
145 umem_pool_count(struct umem_pool
*umemp
)
150 /* AF_XDP metadata init/destroy. */
152 xpacket_pool_init(struct xpacket_pool
*xp
, unsigned int size
)
154 xp
->array
= xmalloc_pagealign(size
* sizeof *xp
->array
);
157 memset(xp
->array
, 0, size
* sizeof *xp
->array
);
163 xpacket_pool_cleanup(struct xpacket_pool
*xp
)
165 free_pagealign(xp
->array
);