forked from DPDK/grout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.c
More file actions
164 lines (132 loc) · 4.08 KB
/
control.c
File metadata and controls
164 lines (132 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024 Robin Jarry
#include "ipip_priv.h"
#include <gr_event.h>
#include <gr_fib4.h>
#include <gr_iface.h>
#include <gr_infra.h>
#include <gr_ipip.h>
#include <gr_log.h>
#include <gr_module.h>
#include <gr_port.h>
#include <event2/event.h>
#include <rte_ethdev.h>
#include <rte_ether.h>
#include <rte_hash.h>
#include <string.h>
struct ipip_key {
ip4_addr_t local;
ip4_addr_t remote;
// XXX: Using uint16_t causes the compiler to add 2 bytes padding at the
// end of the structure. When the structure is initialized on the stack,
// the padding bytes have undetermined contents.
//
// This structure is used to compute a hash key. In order to get
// deterministic results, use uint32_t to store the vrf_id so that the
// compiler does not insert any padding.
uint32_t vrf_id;
};
static struct rte_hash *ipip_hash;
struct iface *ipip_get_iface(ip4_addr_t local, ip4_addr_t remote, uint16_t vrf_id) {
struct ipip_key key = {local, remote, vrf_id};
void *data;
if (rte_hash_lookup_data(ipip_hash, &key, &data) < 0)
return NULL;
return data;
}
static int iface_ipip_reconfig(
struct iface *iface,
uint64_t set_attrs,
const struct gr_iface *conf,
const void *api_info
) {
struct iface_info_ipip *cur = (struct iface_info_ipip *)iface->info;
const struct gr_iface_info_ipip *next = api_info;
struct ipip_key cur_key = {cur->local, cur->remote, iface->vrf_id};
struct ipip_key next_key = {next->local, next->remote, conf->vrf_id};
int ret;
if (set_attrs & (GR_IFACE_SET_VRF | GR_IPIP_SET_LOCAL | GR_IPIP_SET_REMOTE)) {
if (conf->vrf_id >= MAX_VRFS)
return errno_set(EOVERFLOW);
if (rte_hash_lookup(ipip_hash, &next_key) >= 0)
return errno_set(EADDRINUSE);
if (fib4_lookup(conf->vrf_id, next->local) == NULL)
return -errno;
if (fib4_lookup(conf->vrf_id, next->remote) == NULL)
return -errno;
if (memcmp(&cur_key, &next_key, sizeof(cur_key)) != 0)
rte_hash_del_key(ipip_hash, &cur_key);
if ((ret = rte_hash_add_key_data(ipip_hash, &next_key, iface)) < 0)
return errno_log(-ret, "rte_hash_add_key_data");
cur->local = next->local;
cur->remote = next->remote;
iface->vrf_id = conf->vrf_id;
}
if (set_attrs & GR_IFACE_SET_FLAGS)
iface->flags = conf->flags;
if (set_attrs & GR_IFACE_SET_MTU)
iface->mtu = conf->mtu;
gr_event_push(GR_EVENT_IFACE_POST_RECONFIG, iface);
return 0;
}
static int iface_ipip_fini(struct iface *iface) {
struct iface_info_ipip *ipip = (struct iface_info_ipip *)iface->info;
struct ipip_key key = {ipip->local, ipip->remote, iface->vrf_id};
rte_hash_del_key(ipip_hash, &key);
return 0;
}
static int iface_ipip_init(struct iface *iface, const void *api_info) {
const struct gr_iface conf = {
.flags = iface->flags,
.mtu = iface->mtu,
.mode = iface->mode,
.vrf_id = iface->vrf_id
};
int ret;
ret = iface_ipip_reconfig(iface, IFACE_SET_ALL, &conf, api_info);
if (ret < 0) {
iface_ipip_fini(iface);
errno = -ret;
}
return ret;
}
static void ipip_to_api(void *info, const struct iface *iface) {
const struct iface_info_ipip *ipip = (const struct iface_info_ipip *)iface->info;
struct gr_iface_info_ipip *api = info;
*api = ipip->base;
}
static struct iface_type iface_type_ipip = {
.id = GR_IFACE_TYPE_IPIP,
.name = "ipip",
.info_size = sizeof(struct iface_info_ipip),
.init = iface_ipip_init,
.reconfig = iface_ipip_reconfig,
.fini = iface_ipip_fini,
.to_api = ipip_to_api,
};
static void ipip_init(struct event_base *) {
struct rte_hash_parameters params = {
.name = "ipip",
.entries = MAX_IFACES,
.key_len = sizeof(struct ipip_key),
.socket_id = SOCKET_ID_ANY,
.extra_flag = RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF
| RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT,
};
ipip_hash = rte_hash_create(¶ms);
if (ipip_hash == NULL)
ABORT("rte_hash_create(ipip)");
}
static void ipip_fini(struct event_base *) {
rte_hash_free(ipip_hash);
ipip_hash = NULL;
}
static struct gr_module ipip_module = {
.name = "ipip",
.init = ipip_init,
.fini = ipip_fini,
};
RTE_INIT(ipip_constructor) {
gr_register_module(&ipip_module);
iface_type_register(&iface_type_ipip);
}