source: rtems-libbsd/testsuite/include/rtems/bsd/test/default-network-init.h @ 7a79eec

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 7a79eec was 27aedffc, checked in by Sebastian Huber <sebastian.huber@…>, on 05/04/16 at 09:45:19

default-network-init.h: Fix warnings

  • Property mode set to 100644
File size: 8.3 KB
Line 
1/*
2 * Copyright (c) 2013-2014 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/stat.h>
33#include <sys/socket.h>
34
35#include <net/if.h>
36
37#include <assert.h>
38#include <fcntl.h>
39#include <ifaddrs.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <sysexits.h>
43
44#include <machine/rtems-bsd-commands.h>
45
46#include <rtems.h>
47#include <rtems/stackchk.h>
48#include <rtems/bsd/bsd.h>
49
50#if defined(DEFAULT_NETWORK_DHCPCD_ENABLE) && \
51    !defined(DEFAULT_NETWORK_NO_STATIC_IFCONFIG)
52#define DEFAULT_NETWORK_NO_STATIC_IFCONFIG
53#endif
54
55#ifndef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
56#include "network-config.h"
57#endif
58
59#ifdef DEFAULT_NETWORK_SHELL
60#include <rtems/console.h>
61#include <rtems/shell.h>
62#endif
63
64static void
65default_network_set_self_prio(rtems_task_priority prio)
66{
67        rtems_status_code sc;
68
69        sc = rtems_task_set_priority(RTEMS_SELF, prio, &prio);
70        assert(sc == RTEMS_SUCCESSFUL);
71}
72
73static void
74default_network_ifconfig_lo0(void)
75{
76        int exit_code;
77        char *lo0[] = {
78                "ifconfig",
79                "lo0",
80                "inet",
81                "127.0.0.1",
82                "netmask",
83                "255.255.255.0",
84                NULL
85        };
86        char *lo0_inet6[] = {
87                "ifconfig",
88                "lo0",
89                "inet6",
90                "::1",
91                "prefixlen",
92                "128",
93                "alias",
94                NULL
95        };
96
97        exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(lo0), lo0);
98        assert(exit_code == EX_OK);
99
100        exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(lo0_inet6), lo0_inet6);
101        assert(exit_code == EX_OK);
102}
103
104#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
105static void
106default_network_ifconfig_hwif0(char *ifname)
107{
108        int exit_code;
109        char *ifcfg[] = {
110                "ifconfig",
111                ifname,
112#ifdef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
113                "up",
114#else
115                "inet",
116                NET_CFG_SELF_IP,
117                "netmask",
118                NET_CFG_NETMASK,
119#endif
120                NULL
121        };
122
123        exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(ifcfg), ifcfg);
124        assert(exit_code == EX_OK);
125}
126
127static void
128default_network_route_hwif0(char *ifname)
129{
130#ifndef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
131        int exit_code;
132        char *dflt_route[] = {
133                "route",
134                "add",
135                "-host",
136                NET_CFG_GATEWAY_IP,
137                "-iface",
138                ifname,
139                NULL
140        };
141        char *dflt_route2[] = {
142                "route",
143                "add",
144                "default",
145                NET_CFG_GATEWAY_IP,
146                NULL
147        };
148
149        exit_code = rtems_bsd_command_route(RTEMS_BSD_ARGC(dflt_route), dflt_route);
150        assert(exit_code == EXIT_SUCCESS);
151
152        exit_code = rtems_bsd_command_route(RTEMS_BSD_ARGC(dflt_route2), dflt_route2);
153        assert(exit_code == EXIT_SUCCESS);
154#endif
155}
156#endif
157
158#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
159static void
160default_network_dhcpcd_task(rtems_task_argument arg)
161{
162        int exit_code;
163        char *dhcpcd[] = {
164                "dhcpcd",
165                NULL
166        };
167
168        (void)arg;
169
170#ifdef DEFAULT_NETWORK_DHCPCD_NO_DHCP_DISCOVERY
171        static const char cfg[] = "nodhcp\nnodhcp6\n";
172        int fd;
173        int rv;
174        ssize_t n;
175
176        fd = open("/etc/dhcpcd.conf", O_CREAT | O_WRONLY,
177            S_IRWXU | S_IRWXG | S_IRWXO);
178        assert(fd >= 0);
179
180        n = write(fd, cfg, sizeof(cfg));
181        assert(n == (ssize_t) sizeof(cfg));
182
183        rv = close(fd);
184        assert(rv == 0);
185#endif
186
187        exit_code = rtems_bsd_command_dhcpcd(RTEMS_BSD_ARGC(dhcpcd), dhcpcd);
188        assert(exit_code == EXIT_SUCCESS);
189}
190#endif
191
192static void
193default_network_dhcpcd(void)
194{
195#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
196        rtems_status_code sc;
197        rtems_id id;
198
199        sc = rtems_task_create(
200                rtems_build_name('D', 'H', 'C', 'P'),
201                RTEMS_MAXIMUM_PRIORITY - 1,
202                2 * RTEMS_MINIMUM_STACK_SIZE,
203                RTEMS_DEFAULT_MODES,
204                RTEMS_FLOATING_POINT,
205                &id
206        );
207        assert(sc == RTEMS_SUCCESSFUL);
208
209        sc = rtems_task_start(id, default_network_dhcpcd_task, 0);
210        assert(sc == RTEMS_SUCCESSFUL);
211#endif
212}
213
214static void
215default_network_on_exit(int exit_code, void *arg)
216{
217        (void)arg;
218        rtems_stack_checker_report_usage_with_plugin(NULL,
219            rtems_printf_plugin);
220
221        if (exit_code == 0) {
222                puts("*** END OF TEST " TEST_NAME " ***");
223        }
224}
225
226static void
227Init(rtems_task_argument arg)
228{
229        rtems_status_code sc;
230#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
231#ifdef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
232        char ifnamebuf[IF_NAMESIZE];
233#endif
234        char *ifname;
235#endif
236
237        (void)arg;
238        puts("*** " TEST_NAME " TEST ***");
239
240        on_exit(default_network_on_exit, NULL);
241
242#ifdef DEFAULT_EARLY_INITIALIZATION
243        early_initialization();
244#endif
245
246        /* Let other tasks run to complete background work */
247        default_network_set_self_prio(RTEMS_MAXIMUM_PRIORITY - 1U);
248
249#ifdef DEFAULT_NETWORK_SHELL
250        sc = rtems_shell_init(
251                "SHLL",
252                32 * 1024,
253                1,
254                CONSOLE_DEVICE_NAME,
255                false,
256                false,
257                NULL
258        );
259        assert(sc == RTEMS_SUCCESSFUL);
260#endif
261
262        rtems_bsd_initialize();
263
264#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
265#ifdef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
266        ifname = if_indextoname(1, &ifnamebuf[0]);
267        assert(ifname != NULL);
268#else
269        ifname = NET_CFG_INTERFACE_0;
270#endif
271#endif
272
273        /* Let the callout timer allocate its resources */
274        sc = rtems_task_wake_after(2);
275        assert(sc == RTEMS_SUCCESSFUL);
276
277        default_network_ifconfig_lo0();
278#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
279        default_network_ifconfig_hwif0(ifname);
280        default_network_route_hwif0(ifname);
281#endif
282        default_network_dhcpcd();
283
284        test_main();
285
286        assert(0);
287}
288
289#include <machine/rtems-bsd-sysinit.h>
290
291SYSINIT_NEED_NET_PF_UNIX;
292SYSINIT_NEED_NET_IF_LAGG;
293SYSINIT_NEED_NET_IF_VLAN;
294
295#include <bsp/nexus-devices.h>
296
297#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
298#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
299#define CONFIGURE_APPLICATION_NEEDS_STUB_DRIVER
300#define CONFIGURE_APPLICATION_NEEDS_ZERO_DRIVER
301#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
302
303#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
304
305#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 32
306
307#define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1
308
309#define CONFIGURE_UNLIMITED_ALLOCATION_SIZE 32
310#define CONFIGURE_UNLIMITED_OBJECTS
311#define CONFIGURE_UNIFIED_WORK_AREAS
312
313#define CONFIGURE_STACK_CHECKER_ENABLED
314
315#define CONFIGURE_BDBUF_BUFFER_MAX_SIZE (64 * 1024)
316#define CONFIGURE_BDBUF_MAX_READ_AHEAD_BLOCKS 4
317#define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE (1 * 1024 * 1024)
318
319#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
320
321#define CONFIGURE_INIT_TASK_STACK_SIZE (32 * 1024)
322#define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
323#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
324
325#define CONFIGURE_INIT
326
327#include <rtems/confdefs.h>
328
329#ifdef DEFAULT_NETWORK_SHELL
330
331#define CONFIGURE_SHELL_COMMANDS_INIT
332
333#include <bsp/irq-info.h>
334
335#include <rtems/netcmds-config.h>
336
337#define CONFIGURE_SHELL_USER_COMMANDS \
338  &bsp_interrupt_shell_command, \
339  &rtems_shell_BSD_Command, \
340  &rtems_shell_HOSTNAME_Command, \
341  &rtems_shell_PING_Command, \
342  &rtems_shell_ROUTE_Command, \
343  &rtems_shell_NETSTAT_Command, \
344  &rtems_shell_IFCONFIG_Command, \
345  &rtems_shell_TCPDUMP_Command
346
347#define CONFIGURE_SHELL_COMMAND_CPUUSE
348#define CONFIGURE_SHELL_COMMAND_PERIODUSE
349#define CONFIGURE_SHELL_COMMAND_STACKUSE
350#define CONFIGURE_SHELL_COMMAND_PROFREPORT
351
352#define CONFIGURE_SHELL_COMMAND_CP
353#define CONFIGURE_SHELL_COMMAND_PWD
354#define CONFIGURE_SHELL_COMMAND_LS
355#define CONFIGURE_SHELL_COMMAND_LN
356#define CONFIGURE_SHELL_COMMAND_LSOF
357#define CONFIGURE_SHELL_COMMAND_CHDIR
358#define CONFIGURE_SHELL_COMMAND_CD
359#define CONFIGURE_SHELL_COMMAND_MKDIR
360#define CONFIGURE_SHELL_COMMAND_RMDIR
361#define CONFIGURE_SHELL_COMMAND_CAT
362#define CONFIGURE_SHELL_COMMAND_MV
363#define CONFIGURE_SHELL_COMMAND_RM
364#define CONFIGURE_SHELL_COMMAND_MALLOC_INFO
365
366#include <rtems/shellconfig.h>
367
368#endif /* DEFAULT_NETWORK_SHELL */
Note: See TracBrowser for help on using the repository browser.