/
vr_dpdk_netlink.c
301 lines (245 loc) · 7.54 KB
/
vr_dpdk_netlink.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* vr_dpdk_netlink.c -- message handling from agent
*
* Copyright (c) 2014, Juniper Networks Private Inc.,
* All rights reserved
*/
#include "vr_dpdk.h"
#include "vr_dpdk_usocket.h"
#include "vr_message.h"
#include "vr_genetlink.h"
#include "vr_uvhost.h"
#include "vr_uvhost_msg.h"
#include <sys/stat.h>
#include <sys/un.h>
#include <linux/genetlink.h>
#include <rte_errno.h>
#define HDR_LEN (NLMSG_HDRLEN + GENL_HDRLEN + sizeof(struct nlattr))
struct nlmsghdr *dpdk_nl_message_hdr(struct vr_message *);
unsigned int dpdk_nl_message_len(struct vr_message *);
int vr_usocket_message_write(struct vr_usocket *, struct vr_message *);
int vr_nl_uvh_sock;
static void
dpdk_nl_process_response(void *usockp, struct nlmsghdr *nlh)
{
__u32 seq;
unsigned int multi_flag = 0;
bool write = true;
struct vr_message *resp;
struct nlmsghdr *resp_nlh;
struct genlmsghdr *genlh, *resp_genlh;
struct nlattr *resp_nla;
seq = nlh->nlmsg_seq;
genlh = (struct genlmsghdr *)((unsigned char *)nlh + NLMSG_HDRLEN);
/* Process responses */
while ((resp = (struct vr_message *)vr_message_dequeue_response())) {
if (!write) {
vr_message_free(resp);
continue;
}
if (!vr_response_queue_empty()) {
multi_flag = NLM_F_MULTI;
} else {
multi_flag = 0;
}
/* Update Netlink headers */
resp_nlh = dpdk_nl_message_hdr(resp);
resp_nlh->nlmsg_len = dpdk_nl_message_len(resp);
resp_nlh->nlmsg_type = nlh->nlmsg_type;
resp_nlh->nlmsg_flags = multi_flag;
resp_nlh->nlmsg_seq = seq;
resp_nlh->nlmsg_pid = 0;
resp_genlh = (struct genlmsghdr *)((unsigned char *)resp_nlh +
NLMSG_HDRLEN);
memcpy(resp_genlh, genlh, sizeof(*genlh));
resp_nla = (struct nlattr *)((unsigned char *)resp_genlh + GENL_HDRLEN);
resp_nla->nla_len = resp->vr_message_len;
resp_nla->nla_type = NL_ATTR_VR_MESSAGE_PROTOCOL;
if (vr_usocket_message_write(usockp, resp) < 0) {
write = false;
vr_usocket_close(usockp);
}
}
return;
}
int
dpdk_netlink_receive(void *usockp, char *nl_buf,
unsigned int nl_len)
{
int ret;
struct vr_message request;
request.vr_message_buf = nl_buf + HDR_LEN;
request.vr_message_len = nl_len - HDR_LEN;
ret = vr_message_request(&request);
if (ret < 0)
vr_send_response(ret);
dpdk_nl_process_response(usockp, (struct nlmsghdr *)nl_buf);
return 0;
}
unsigned int
dpdk_nl_message_len(struct vr_message *message)
{
return message->vr_message_len + HDR_LEN;
}
struct nlmsghdr *
dpdk_nl_message_hdr(struct vr_message *message)
{
return (struct nlmsghdr *)(message->vr_message_buf - HDR_LEN);
}
static void
dpdk_nl_trans_free(char *buf)
{
buf -= HDR_LEN;
vr_free(buf, VR_MESSAGE_OBJECT);
return;
}
static char *
dpdk_nl_trans_alloc(unsigned int size)
{
char *buf;
buf = vr_malloc(size + HDR_LEN, VR_MESSAGE_OBJECT);
if (!buf)
return NULL;
return buf + HDR_LEN;
}
static struct vr_mtransport dpdk_nl_transport = {
.mtrans_alloc = dpdk_nl_trans_alloc,
.mtrans_free = dpdk_nl_trans_free,
};
/*
* vr_netlink_uvhost_vif_del - sends a message to the user space vhost
* thread when a vif is deleted. vif_idx is the index of the vif.
*
* Returns 0 on success, -1 otherwise.
*/
int
vr_netlink_uvhost_vif_del(unsigned int vif_idx)
{
vrnu_msg_t msg;
msg.vrnum_type = VRNU_MSG_VIF_DEL;
msg.vrnum_vif_del.vrnu_vif_idx = vif_idx;
/*
* This is a blocking send.
*/
if (send(vr_nl_uvh_sock, (void *) &msg, sizeof(msg), 0) !=
sizeof(msg)) {
RTE_LOG(ERR, VROUTER, " error deleting vif %u from user space vhost:"
" %s (%d)\n", vif_idx, rte_strerror(errno), errno);
return -1;
}
return 0;
}
/*
* vr_netlink_uvhost_vif_add - sends a message to the user space vhost
* thread when a new vif is created. The name os the vif is specified in
* the vif_name argument.
*
* Returns 0 on success, -1 otherwise.
*/
int
vr_netlink_uvhost_vif_add(char *vif_name, unsigned int vif_idx,
unsigned int vif_nrxqs, unsigned int vif_ntxqs)
{
vrnu_msg_t msg;
msg.vrnum_type = VRNU_MSG_VIF_ADD;
strncpy(msg.vrnum_vif_add.vrnu_vif_name, vif_name,
sizeof(msg.vrnum_vif_add.vrnu_vif_name) - 1);
msg.vrnum_vif_add.vrnu_vif_idx = vif_idx;
msg.vrnum_vif_add.vrnu_vif_nrxqs = vif_nrxqs;
msg.vrnum_vif_add.vrnu_vif_ntxqs = vif_ntxqs;
/*
* This is a blocking send.
*/
if (send(vr_nl_uvh_sock, (void *) &msg, sizeof(msg), 0) !=
sizeof(msg)) {
RTE_LOG(ERR, VROUTER, " error adding vif %u to user space vhost:"
" %s (%d)\n", vif_idx, rte_strerror(errno), errno);
return -1;
}
return 0;
}
void
dpdk_netlink_exit(void)
{
vr_message_transport_unregister(&dpdk_nl_transport);
vr_usocket_close(vr_dpdk.netlink_sock);
return;
}
/*
* vr_nl_uvhost_connect - connect to the user space vhost server on a UNIX
* domain socket.
*
* Returns 0 on success, error otherwise.
*/
static int
vr_nl_uvhost_connect(void)
{
int s = 0, ret = -1, err;
struct sockaddr_un nl_sun, uvh_sun;
s = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (s == -1) {
RTE_LOG(ERR, VROUTER, " error creating uvhost socket: %s (%d)\n",
rte_strerror(errno), errno);
goto error;
}
RTE_LOG(INFO, VROUTER, " uvhost Unix socket FD is %d\n", s);
memset(&nl_sun, 0, sizeof(nl_sun));
nl_sun.sun_family = AF_UNIX;
strncpy(nl_sun.sun_path, VR_NL_UVH_SOCK, sizeof(nl_sun.sun_path) - 1);
mkdir(VR_SOCKET_DIR, VR_SOCKET_DIR_MODE);
unlink(nl_sun.sun_path);
ret = bind(s, (struct sockaddr *) &nl_sun, sizeof(nl_sun));
if (ret == -1) {
RTE_LOG(ERR, VROUTER, " error binding uvhost FD %d to %s: %s (%d)\n",
s, nl_sun.sun_path, rte_strerror(errno), errno);
goto error;
}
/*
* This will block until the user space vhost thread listens on the
* socket.
*/
memset(&uvh_sun, 0, sizeof(uvh_sun));
uvh_sun.sun_family = AF_UNIX;
strncpy(uvh_sun.sun_path, VR_UVH_NL_SOCK, sizeof(uvh_sun.sun_path) - 1);
ret = vr_dpdk_retry_connect(s, (struct sockaddr *) &uvh_sun, sizeof(uvh_sun));
if (ret == -1) {
RTE_LOG(ERR, VROUTER, " error connecting uvhost socket FD %d to %s:"
" %s (%d)\n", s, uvh_sun.sun_path, rte_strerror(errno), errno);
goto error;
}
vr_nl_uvh_sock = s;
return 0;
error:
err = errno;
if (s > 0) {
close(s);
}
errno = err;
return ret;
}
/* Init NetLink and UVHost sockets */
int
vr_dpdk_netlink_init(void)
{
int ret;
RTE_LOG(INFO, VROUTER, "Starting NetLink...\n");
ret = vr_message_transport_register(&dpdk_nl_transport);
if (ret)
return ret;
vr_dpdk.netlink_sock = vr_usocket(NETLINK, TCP);
if (!vr_dpdk.netlink_sock) {
RTE_LOG(ERR, VROUTER, " error creating NetLink server socket:"
" %s (%d)\n", rte_strerror(errno), errno);
return -1;
}
RTE_LOG(INFO, VROUTER, " NetLink TCP socket FD is %d\n",
((struct vr_usocket *)vr_dpdk.netlink_sock)->usock_fd);
ret = vr_nl_uvhost_connect();
if (ret != 0) {
vr_message_transport_unregister(&dpdk_nl_transport);
vr_usocket_close(vr_dpdk.netlink_sock);
RTE_LOG(ERR, VROUTER, " error creating uvhost connection\n");
return -1;
}
return 0;
}