-
Notifications
You must be signed in to change notification settings - Fork 171
/
vrouter_host_mod.c
347 lines (272 loc) · 6.45 KB
/
vrouter_host_mod.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* vrouter_host_mod.c -- 'vrouter' library init
*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <stdarg.h>
#include "vr_os.h"
#include "vr_packet.h"
#include "vr_proto.h"
#include "vrouter.h"
#include <sys/time.h>
#include "vr_message.h"
#include "vr_sandesh.h"
#include "host/vr_host_packet.h"
#include "ulinux.h"
#define PAGE_SIZE 4096
unsigned int vr_num_cpus = 1;
static bool vr_host_inited = false;
static unsigned int vr_message_proto;
extern void vr_diet_message_proto_exit(void);
extern int vr_diet_message_proto_init(void);
static int
vr_lib_create_timer(struct vr_timer *vtimer)
{
struct dummy_timer_list *timer;
timer = vr_zalloc(sizeof(*timer), VR_TIMER_OBJECT);
if (!timer)
return -1;
init_timer(timer);
vtimer->vt_os_arg = (void *)timer;
timer->data = (unsigned long)vtimer;
timer->function = ulinux_timer;
timer->expires = get_time() + vtimer->vt_msecs;
add_timer(timer);
return 0;
}
static void
vr_lib_delete_timer(struct vr_timer *vtimer)
{
vr_free(vtimer->vt_os_arg, VR_TIMER_OBJECT);
}
static void *
vr_lib_page_alloc(unsigned int size)
{
int pages;
pages = size / PAGE_SIZE;
if (size % PAGE_SIZE)
pages++;
return calloc(pages, PAGE_SIZE);
}
static void
vr_lib_page_free(void *address, unsigned int size)
{
if (address)
free(address);
}
static int
vr_lib_printf(const char *format, ...)
{
int printed;
va_list args;
va_start(args, format);
printed = printf(format, args);
va_end(args);
return printed;
}
static void *
vr_lib_malloc(unsigned int size, unsigned int object)
{
return malloc(size);
}
static void *
vr_lib_zalloc(unsigned int size, unsigned int object)
{
return calloc(size, 1);
}
static void
vr_lib_free(void *mem, unsigned int object)
{
if (mem)
free(mem);
return;
}
static struct vr_packet *
vr_lib_get_packet(struct vr_hpacket *hpkt, struct vr_interface *vif)
{
struct vr_packet *pkt;
pkt = &hpkt->hp_packet;
pkt->vp_head = hpkt->hp_head;
pkt->vp_data = hpkt->hp_data;
pkt->vp_tail = hpkt->hp_tail;
pkt->vp_end = hpkt->hp_end;
pkt->vp_len = hpkt_head_len(hpkt);
pkt->vp_if = vif;
return pkt;
}
static struct vr_packet *
vr_lib_palloc(unsigned int size)
{
struct vr_hpacket *hpkt;
hpkt = vr_hpacket_alloc(size);
if (!hpkt)
return NULL;
return vr_lib_get_packet(hpkt, NULL);
}
static struct vr_packet *
vr_lib_palloc_head(struct vr_packet *pkt, unsigned int size)
{
struct vr_hpacket *hpkt_head, *hpkt;
hpkt_head = vr_hpacket_alloc(size);
if (!hpkt_head)
return NULL;
hpkt = VR_PACKET_TO_HPACKET(pkt);
hpkt_head->hp_len = hpkt->hp_len;
hpkt_head->hp_next = hpkt;
return &hpkt_head->hp_packet;
}
static struct vr_packet *
vr_lib_pclone(struct vr_packet *pkt)
{
struct vr_hpacket *hpkt, *hpkt_c;
hpkt = VR_PACKET_TO_HPACKET(pkt);
hpkt_c = vr_hpacket_clone(hpkt);
if (!hpkt_c)
return NULL;
return &hpkt_c->hp_packet;
}
static void
vr_lib_preset(struct vr_packet *pkt)
{
struct vr_hpacket *hpkt;
hpkt = VR_PACKET_TO_HPACKET(pkt);
pkt->vp_data = hpkt->hp_data;
pkt->vp_tail = hpkt->hp_tail;
pkt->vp_len = pkt->vp_tail - pkt->vp_data + 1;
return;
}
static void
vr_lib_pfree(struct vr_packet *pkt, unsigned short reason)
{
struct vr_hpacket *hpkt;
hpkt = VR_PACKET_TO_HPACKET(pkt);
vr_hpacket_free(hpkt);
return;
}
static int
vr_lib_pcopy(unsigned char *dst, struct vr_packet *p_src,
unsigned int offset, unsigned int len)
{
struct vr_hpacket *src_hpkt = VR_PACKET_TO_HPACKET(p_src);
return vr_hpacket_copy(dst, src_hpkt, offset, len);
}
static unsigned short
vr_lib_pfrag_len(struct vr_packet *pkt)
{
struct vr_hpacket *hpkt;
hpkt = VR_PACKET_TO_HPACKET(pkt);
if (!hpkt->hp_next)
return 0;
return hpkt->hp_next->hp_len;
}
static void
vr_lib_get_time(unsigned long *sec, unsigned long *usec)
{
struct timeval tv;
*sec = *usec = 0;
if (gettimeofday(&tv, NULL) < 0)
return;
*sec = tv.tv_sec;
*usec = tv.tv_usec;
return;
}
static unsigned int
vr_lib_get_cpu(void)
{
return 0;
}
static int
vr_lib_schedule_work(unsigned int cpu, void (*fn)(void *), void *arg)
{
return -EOPNOTSUPP;
}
static void
vr_lib_delay_op(void)
{
return;
}
struct host_os vr_lib_host = {
.hos_printf = vr_lib_printf,
.hos_malloc = vr_lib_malloc,
.hos_zalloc = vr_lib_zalloc,
.hos_free = vr_lib_free,
.hos_palloc = vr_lib_palloc,
.hos_palloc_head = vr_lib_palloc_head,
.hos_pfree = vr_lib_pfree,
.hos_preset = vr_lib_preset,
.hos_pclone = vr_lib_pclone,
.hos_pcopy = vr_lib_pcopy,
.hos_pfrag_len = vr_lib_pfrag_len,
.hos_get_cpu = vr_lib_get_cpu,
.hos_schedule_work = vr_lib_schedule_work,
.hos_delay_op = vr_lib_delay_op,
.hos_get_time = vr_lib_get_time,
.hos_page_alloc = vr_lib_page_alloc,
.hos_page_free = vr_lib_page_free,
.hos_create_timer = vr_lib_create_timer,
.hos_delete_timer = vr_lib_delete_timer,
};
struct host_os *
vrouter_get_host(void)
{
return &vr_lib_host;
}
static void
vr_message_exit(void)
{
switch (vr_message_proto) {
case VR_MPROTO_DIET:
vr_diet_message_proto_exit();
break;
case VR_MPROTO_SANDESH:
vr_sandesh_exit();
break;
default:
break;
}
vr_message_proto = 0;
return;
}
static int
vr_message_init(unsigned int message_proto)
{
int ret = 0;
switch (message_proto) {
case VR_MPROTO_DIET:
ret = vr_diet_message_proto_init();
break;
case VR_MPROTO_SANDESH:
ret = vr_sandesh_init();
break;
default:
ret = -EINVAL;
}
if (!ret)
vr_message_proto = message_proto;
return ret;
}
void
vrouter_host_exit(void)
{
vr_message_exit();
vrouter_exit(false);
return;
}
int
vrouter_host_init(unsigned int message_proto)
{
int ret;
if (vr_host_inited)
return 0;
ret = vrouter_init();
if (ret)
return ret;
ret = vr_message_init(message_proto);
if (ret)
goto init_fail;
vr_host_inited = true;
return 0;
init_fail:
vrouter_host_exit();
return ret;
}