-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathnuse-config.c
250 lines (201 loc) · 5.62 KB
/
nuse-config.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
/*
* config file interface for NUSE
* Copyright (c) 2015 Ryo Nakamura
*
* Author: Ryo Nakamura <upa@wide.ad.jp>
* Hajime Tazaki <tazaki@sfc.wide.ad.jp>
*/
#include <sys/socket.h>
#include <linux/socket.h>
#include <linux/in.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "nuse-vif.h"
#include "nuse-config.h"
#include "nuse-libc.h"
static int
strsplit(char *str, char **args, int max)
{
int argc;
char *c;
for (argc = 0, c = str; *c == ' ' || *c == '\t' || *c == '\n'; c++)
;
while (*c && argc < max) {
args[argc++] = c;
while (*c && *c > ' ')
c++;
while (*c && *c <= ' ')
*c++ = '\0';
}
return argc;
}
static int
nuse_config_parse_interface(char *line, FILE *fp, struct nuse_config *cf)
{
int ret;
char buf[1024], *p, *args[2];
struct nuse_vif_config *vifcf;
struct sockaddr_in *sin;
memset(buf, 0, sizeof(buf));
vifcf = (struct nuse_vif_config *)
malloc(sizeof(struct nuse_vif_config));
memset(vifcf, 0, sizeof(struct nuse_vif_config));
vifcf->type = NUSE_VIF_RAWSOCK; /* default */
strcpy(vifcf->macaddr, "00:00:00:00:00:00"); /* means random */
strsplit(line, args, sizeof(args));
strncpy(vifcf->ifname, args[1], IFNAMSIZ);
while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
ret = strsplit(buf, args, sizeof(args));
if (ret == 0)
/* no item in the line */
break;
if (strncmp(args[0], "address", 7) == 0)
strncpy(vifcf->address, args[1], NUSE_ADDR_STRLEN);
if (strncmp(args[0], "netmask", 7) == 0)
strncpy(vifcf->netmask, args[1], NUSE_ADDR_STRLEN);
if (strncmp(args[0], "macaddr", 7) == 0)
strncpy(vifcf->macaddr, args[1], NUSE_MACADDR_STRLEN);
if (strncmp(args[0], "viftype", 7) == 0) {
if (strncmp(args[1], "RAW", 3) == 0)
vifcf->type = NUSE_VIF_RAWSOCK;
else if (strncmp(args[1], "NETMAP", 6) == 0)
vifcf->type = NUSE_VIF_NETMAP;
else if (strncmp(args[1], "TAP", 3) == 0)
vifcf->type = NUSE_VIF_TAP;
else if (strncmp(args[1], "DPDK", 4) == 0)
vifcf->type = NUSE_VIF_DPDK;
else if (strncmp(args[1], "PIPE", 4) == 0)
vifcf->type = NUSE_VIF_PIPE;
else {
printf("invalid vif type %s\n", args[1]);
free(vifcf);
return 0;
}
}
/* mkfifo path for VIF_PIPE */
if (strncmp(args[0], "pipepath", 4) == 0) {
strncpy(vifcf->pipepath, args[1], PATH_MAX);
}
}
/* setup ifreq */
sin = (struct sockaddr_in *)&vifcf->ifr_vif_addr.ifr_addr;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = inet_addr(vifcf->address);
sin = (struct sockaddr_in *)&vifcf->ifr_vif_mask.ifr_netmask;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = inet_addr(vifcf->netmask);
/* XXX: ifname attached to nuse process is same as host stck */
strncpy(vifcf->ifr_vif_addr.ifr_name, vifcf->ifname, IFNAMSIZ);
strncpy(vifcf->ifr_vif_mask.ifr_name, vifcf->ifname, IFNAMSIZ);
/* reassemble mac address */
if (sscanf(vifcf->macaddr, "%u:%u:%u:%u:%u:%u",
(unsigned int *)&vifcf->mac[0],
(unsigned int *)&vifcf->mac[1],
(unsigned int *)&vifcf->mac[2],
(unsigned int *)&vifcf->mac[3],
(unsigned int *)&vifcf->mac[4],
(unsigned int *)&vifcf->mac[5]) < 1) {
printf("failed to parse mac address %s\n", vifcf->macaddr);
free(vifcf);
return 0;
}
cf->vifs[cf->vif_cnt++] = vifcf;
return 1;
}
static int
nuse_config_parse_route(char *line, FILE *fp, struct nuse_config *cf)
{
int ret, net, mask, gate;
char buf[1024], *p, *args[2];
struct nuse_route_config *rtcf;
struct sockaddr_in *sin;
net = 0;
mask = 0;
gate = 0;
memset(buf, 0, sizeof(buf));
rtcf = (struct nuse_route_config *)
malloc(sizeof(struct nuse_route_config));
if (!rtcf)
assert(0);
memset(rtcf, 0, sizeof(struct nuse_route_config));
while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
ret = strsplit(buf, args, sizeof(args));
if (ret == 0)
/* no item in the line */
break;
if (strncmp(args[0], "network", 7) == 0) {
strncpy(rtcf->network, args[1], NUSE_ADDR_STRLEN);
net = 1;
}
if (strncmp(args[0], "netmask", 7) == 0) {
strncpy(rtcf->netmask, args[1], NUSE_ADDR_STRLEN);
mask = 1;
}
if (strncmp(args[0], "gateway", 7) == 0) {
strncpy(rtcf->gateway, args[1], NUSE_ADDR_STRLEN);
gate = 1;
}
if (net && mask && gate)
break;
}
if (!net)
printf("network is not configured !\n");
if (!mask)
printf("netmask is not configured !\n");
if (!gate)
printf("netmask is not configured !\n");
if (!net || !mask || !gate) {
free (rtcf);
return 0;
}
/* setup rtentry */
sin = (struct sockaddr_in *)&rtcf->route.rt_dst;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = inet_addr(rtcf->network);
sin = (struct sockaddr_in *)&rtcf->route.rt_genmask;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = inet_addr(rtcf->netmask);
sin = (struct sockaddr_in *)&rtcf->route.rt_gateway;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = inet_addr(rtcf->gateway);
rtcf->route.rt_flags = RTF_UP | RTF_GATEWAY;
rtcf->route.rt_metric = 0;
cf->routes[cf->route_cnt++] = rtcf;
return 1;
}
int
nuse_config_parse(struct nuse_config *cf, char *cfname)
{
FILE *fp;
int ret = 0;
char buf[1024];
memset(cf, 0, sizeof(struct nuse_config));
fp = fopen(cfname, "r");
if (fp == NULL) {
perror("fopen");
return 0;
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (strncmp(buf, "interface ", 10) == 0)
ret = nuse_config_parse_interface(buf, fp, cf);
else if (strncmp(buf, "route", 5) == 0)
ret = nuse_config_parse_route(buf, fp, cf);
else
continue;
if (!ret)
break;
}
fclose(fp);
return ret;
}
void
nuse_config_free(struct nuse_config *cf)
{
int n;
for (n = 0; n < cf->vif_cnt; n++)
free(cf->vifs[n]);
for (n = 0; n < cf->route_cnt; n++)
free(cf->routes[n]);
}