forked from unikraft/lib-lwip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
264 lines (238 loc) · 8.12 KB
/
init.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
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Authors: Simon Kuenzer <[email protected]>
*
* Copyright (c) 2019, NEC Laboratories Europe GmbH, NEC Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
*/
#include <uk/config.h>
#include "lwip/opt.h"
#include "lwip/tcpip.h"
#include "lwip/init.h"
#include "lwip/dhcp.h"
#include "lwip/inet.h"
#if CONFIG_LWIP_NOTHREADS
#include "lwip/timeouts.h"
#else /* CONFIG_LWIP_NOTHREADS */
#include <uk/semaphore.h>
#endif /* CONFIG_LWIP_NOTHREADS */
#include <uk/netdev_core.h>
#include "netif/uknetdev.h"
#include <uk/init.h>
#if LWIP_NETIF_EXT_STATUS_CALLBACK && CONFIG_LWIP_NETIF_STATUS_PRINT
#include <stdio.h>
static void _netif_status_print(struct netif *nf, netif_nsc_reason_t reason,
const netif_ext_callback_args_t *args)
{
if (reason & LWIP_NSC_NETIF_ADDED)
printf("%c%c%u: Added\n",
nf->name[0], nf->name[1], nf->num);
if (reason & LWIP_NSC_NETIF_REMOVED)
printf("%c%c%u: Removed\n",
nf->name[0], nf->name[1], nf->num);
if (reason & LWIP_NSC_LINK_CHANGED)
printf("%c%c%u: Link is %s\n",
nf->name[0], nf->name[1], nf->num,
args->link_changed.state ? "up" : "down");
if (reason & LWIP_NSC_STATUS_CHANGED)
printf("%c%c%u: Interface is %s\n",
nf->name[0], nf->name[1], nf->num,
args->status_changed.state ? "up" : "down");
#if LWIP_IPV4
if ((reason & LWIP_NSC_IPV4_SETTINGS_CHANGED)
|| (reason & LWIP_NSC_IPV4_ADDRESS_CHANGED)
|| (reason & LWIP_NSC_IPV4_NETMASK_CHANGED)
|| (reason & LWIP_NSC_IPV4_GATEWAY_CHANGED)) {
char str_ip4_addr[17];
char str_ip4_mask[17];
char str_ip4_gw[17];
ipaddr_ntoa_r(&nf->ip_addr, str_ip4_addr, sizeof(str_ip4_addr));
ipaddr_ntoa_r(&nf->netmask, str_ip4_mask, sizeof(str_ip4_mask));
ipaddr_ntoa_r(&nf->gw, str_ip4_gw, sizeof(str_ip4_gw));
printf("%c%c%u: Set IPv4 address %s mask %s gw %s\n",
nf->name[0], nf->name[1], nf->num,
str_ip4_addr, str_ip4_mask, str_ip4_gw);
}
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
if (reason & LWIP_NSC_IPV6_SET)
printf("%c%c%u: Set IPv6 address %"__PRIs8": %s (state %"__PRIu8")\n",
nf->name[0], nf->name[1], nf->num,
args->ipv6_set.addr_index,
ipaddr_ntoa(&nf->ip6_addr[args->ipv6_set.addr_index]),
nf->ip6_addr_state[args->ipv6_set.addr_index]);
if (reason & LWIP_NSC_IPV6_ADDR_STATE_CHANGED)
printf("%c%c%u: Set IPv6 address %"__PRIs8": %s (state %"__PRIu8")\n",
nf->name[0], nf->name[1], nf->num,
args->ipv6_set.addr_index,
ipaddr_ntoa(&nf->ip6_addr[
args->ipv6_addr_state_changed.addr_index]),
nf->ip6_addr_state[
args->ipv6_addr_state_changed.addr_index]);
#endif /* LWIP_IPV6 */
}
NETIF_DECLARE_EXT_CALLBACK(netif_status_print)
#endif /* LWIP_NETIF_EXT_STATUS_CALLBACK && CONFIG_LWIP_NETIF_STATUS_PRINT */
void sys_init(void)
{
/*
* This function is called before the any other sys_arch-function is
* called and is meant to be used to initialize anything that has to
* be up and running for the rest of the functions to work. for
* example to set up a pool of semaphores.
*/
}
#if !CONFIG_LWIP_NOTHREADS
static struct uk_semaphore _lwip_init_sem;
static void _lwip_init_done(void *arg __unused)
{
uk_semaphore_up(&_lwip_init_sem);
}
#endif /* !CONFIG_LWIP_NOTHREADS */
/*
* This function initializing the lwip network stack
*/
static int liblwip_init(void)
{
#if CONFIG_LWIP_UKNETDEV && CONFIG_LWIP_AUTOIFACE
unsigned int devid;
struct uk_netdev *dev;
struct netif *nf;
const char __maybe_unused *strcfg;
uint16_t __maybe_unused int16cfg;
int is_first_nf;
#if LWIP_IPV4
ip4_addr_t ip4;
ip4_addr_t *ip4_arg;
ip4_addr_t mask4;
ip4_addr_t *mask4_arg;
ip4_addr_t gw4;
ip4_addr_t *gw4_arg;
#endif /* LWIP_IPV4 */
#endif /* CONFIG_LWIP_UKNETDEV && CONFIG_LWIP_AUTOIFACE */
uk_pr_info("Initializing lwip\n");
#if !CONFIG_LWIP_NOTHREADS
uk_semaphore_init(&_lwip_init_sem, 0);
#endif /* !CONFIG_LWIP_NOTHREADS */
#if CONFIG_LWIP_NOTHREADS
lwip_init();
#else /* CONFIG_LWIP_NOTHREADS */
tcpip_init(_lwip_init_done, NULL);
/* Wait until stack is booted */
uk_semaphore_down(&_lwip_init_sem);
#endif /* CONFIG_LWIP_NOTHREADS */
#if LWIP_NETIF_EXT_STATUS_CALLBACK && CONFIG_LWIP_NETIF_STATUS_PRINT
/* Add print callback for netif state changes */
netif_add_ext_callback(&netif_status_print, _netif_status_print);
#endif /* LWIP_NETIF_EXT_STATUS_CALLBACK && CONFIG_LWIP_NETIF_STATUS_PRINT */
#if CONFIG_LWIP_UKNETDEV && CONFIG_LWIP_AUTOIFACE
is_first_nf = 1;
for (devid = 0; devid < uk_netdev_count(); ++devid) {
dev = uk_netdev_get(devid);
if (!dev)
continue;
if (uk_netdev_state_get(dev) != UK_NETDEV_UNCONFIGURED) {
uk_pr_info("Skipping to add network device %u to lwIP: Not in unconfigured state\n",
devid);
continue;
}
uk_pr_info("Attach network device %u to lwIP...\n",
devid);
#if LWIP_IPV4
ip4_arg = NULL;
mask4_arg = NULL;
gw4_arg = NULL;
/* IP */
strcfg = uk_netdev_einfo_get(dev, UK_NETDEV_IPV4_ADDR_STR);
if (strcfg) {
if (ip4addr_aton(strcfg, &ip4) != 1) {
uk_pr_err("Error converting IP address: %s\n",
strcfg);
goto no_conf;
}
} else
goto no_conf;
ip4_arg = &ip4;
/* mask */
strcfg = uk_netdev_einfo_get(dev, UK_NETDEV_IPV4_MASK_STR);
if (strcfg) {
if (ip4addr_aton(strcfg, &mask4) != 1) {
uk_pr_err("Error converting net mask: %s\n",
strcfg);
goto no_conf;
}
} else
/* default mask */
ip4_addr_set_u32(&mask4, lwip_htonl(IP_CLASSC_NET));
mask4_arg = &mask4;
/* gateway */
strcfg = uk_netdev_einfo_get(dev, UK_NETDEV_IPV4_GW_STR);
if (strcfg) {
if (ip4addr_aton(strcfg, &gw4) != 1) {
uk_pr_err("Error converting gateway: %s\n",
strcfg);
goto no_conf;
}
gw4_arg = &gw4;
}
no_conf:
nf = uknetdev_addif(dev, ip4_arg, mask4_arg, gw4_arg);
#else /* LWIP_IPV4 */
/*
* TODO: Add support for IPv6 device configuration from
* netdev's econf interface
*/
nf = uknetdev_addif(dev);
#endif /* LWIP_IPV4 */
if (!nf) {
uk_pr_err("Failed to attach network device %u to lwIP\n",
devid);
continue;
}
/* Declare the first network device as default interface */
if (is_first_nf) {
uk_pr_info("%c%c%u: Set as default interface\n",
nf->name[0], nf->name[1], nf->num);
netif_set_default(nf);
is_first_nf = 0;
}
netif_set_up(nf);
#if LWIP_IPV4 && LWIP_DHCP
if (!ip4_arg) {
uk_pr_info("%c%c%u: DHCP configuration (background)...\n",
nf->name[0], nf->name[1], nf->num);
dhcp_start(nf);
}
#endif /* LWIP_IPV4 && LWIP_DHCP */
}
#endif /* CONFIG_LWIP_UKNETDEV && CONFIG_LWIP_AUTOIFACE */
return 0;
}
uk_lib_initcall(liblwip_init);