1 /* Minimized/hacked up from openvswitch lib/conntrack.c, which had this license
2 header: */
3 /*
4 * Copyright (c) 2015-2019 Nicira, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 typedef __SIZE_TYPE__ size_t;
20 #define NULL ((void *)0)
21 #define false 0
22
23 #define OBJECT_OFFSETOF(OBJECT, MEMBER)\
24 __builtin_offsetof(typeof(*(OBJECT)), MEMBER)
25
26 #define OBJECT_CONTAINING(POINTER, OBJECT, MEMBER) \
27 ((typeof(OBJECT)) (void *) \
28 ((char *) (POINTER) - OBJECT_OFFSETOF(OBJECT, MEMBER)))
29
30 #define ASSIGN_CONTAINER(OBJECT, POINTER, MEMBER) \
31 ((OBJECT) = OBJECT_CONTAINING(POINTER, OBJECT, MEMBER), (void) 0)
32
33 #define INIT_CONTAINER(OBJECT, POINTER, MEMBER) \
34 ((OBJECT) = NULL, ASSIGN_CONTAINER(OBJECT, POINTER, MEMBER))
35
36 #define HMAP_FOR_EACH_POP(NODE, MEMBER, HMAP) \
37 for (size_t bucket__ = 0; \
38 INIT_CONTAINER(NODE, hmap_pop_helper__(HMAP, &bucket__), MEMBER), \
39 (NODE != OBJECT_CONTAINING(NULL, NODE, MEMBER)) \
40 || ((NODE = NULL), false);)
41
42 struct hmap {
43 struct hmap_node **buckets;
44 struct hmap_node *one;
45 size_t mask;
46 size_t n;
47 };
48
49 struct hmap_node {
50 size_t hash;
51 struct hmap_node *next;
52 };
53
54 static inline void hmap_remove(struct hmap *, struct hmap_node *);
55
56 struct hmap_node *
hmap_pop_helper__(struct hmap * hmap,size_t * bucket)57 hmap_pop_helper__(struct hmap *hmap, size_t *bucket) {
58
59 for (; *bucket <= hmap->mask; (*bucket)++) {
60 struct hmap_node *node = hmap->buckets[*bucket];
61
62 if (node) {
63 hmap_remove(hmap, node);
64 return node;
65 }
66 }
67
68 return NULL;
69 }
70
71 static inline void
hmap_remove(struct hmap * hmap,struct hmap_node * node)72 hmap_remove(struct hmap *hmap, struct hmap_node *node)
73 {
74 struct hmap_node **bucket = &hmap->buckets[node->hash & hmap->mask];
75 while (*bucket != node) {
76 bucket = &(*bucket)->next;
77 }
78 *bucket = node->next;
79 hmap->n--;
80 }
81
82 struct conntrack {
83 struct hmap zone_limits;
84 };
85
86 struct zone_limit {
87 struct hmap_node node;
88 };
89
90 void
conntrack_destroy(struct conntrack * ct)91 conntrack_destroy(struct conntrack *ct)
92 {
93 struct zone_limit *zl;
94 HMAP_FOR_EACH_POP (zl, node, &ct->zone_limits) {
95 __builtin_free(zl);
96 }
97 }
98