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

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 32fd702 was 32fd702, checked in by Sebastian Huber <sebastian.huber@…>, on 06/23/16 at 12:06:21

Update due to RTEMS printer API changes

  • Property mode set to 100644
File size: 8.5 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/printer.h>
48#include <rtems/stackchk.h>
49#include <rtems/bsd/bsd.h>
50
51#if defined(DEFAULT_NETWORK_DHCPCD_ENABLE) && \
52    !defined(DEFAULT_NETWORK_NO_STATIC_IFCONFIG)
53#define DEFAULT_NETWORK_NO_STATIC_IFCONFIG
54#endif
55
56#ifndef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
57#include <rtems/bsd/test/network-config.h>
58#endif
59
60#ifdef DEFAULT_NETWORK_SHELL
61#include <rtems/console.h>
62#include <rtems/shell.h>
63#endif
64
65static void
66default_network_set_self_prio(rtems_task_priority prio)
67{
68        rtems_status_code sc;
69
70        sc = rtems_task_set_priority(RTEMS_SELF, prio, &prio);
71        assert(sc == RTEMS_SUCCESSFUL);
72}
73
74static void
75default_network_ifconfig_lo0(void)
76{
77        int exit_code;
78        char *lo0[] = {
79                "ifconfig",
80                "lo0",
81                "inet",
82                "127.0.0.1",
83                "netmask",
84                "255.255.255.0",
85                NULL
86        };
87        char *lo0_inet6[] = {
88                "ifconfig",
89                "lo0",
90                "inet6",
91                "::1",
92                "prefixlen",
93                "128",
94                "alias",
95                NULL
96        };
97
98        exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(lo0), lo0);
99        assert(exit_code == EX_OK);
100
101        exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(lo0_inet6), lo0_inet6);
102        assert(exit_code == EX_OK);
103}
104
105#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
106static void
107default_network_ifconfig_hwif0(char *ifname)
108{
109        int exit_code;
110        char *ifcfg[] = {
111                "ifconfig",
112                ifname,
113#ifdef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
114                "up",
115#else
116                "inet",
117                NET_CFG_SELF_IP,
118                "netmask",
119                NET_CFG_NETMASK,
120#endif
121                NULL
122        };
123
124        exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(ifcfg), ifcfg);
125        assert(exit_code == EX_OK);
126}
127
128static void
129default_network_route_hwif0(char *ifname)
130{
131#ifndef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
132        int exit_code;
133        char *dflt_route[] = {
134                "route",
135                "add",
136                "-host",
137                NET_CFG_GATEWAY_IP,
138                "-iface",
139                ifname,
140                NULL
141        };
142        char *dflt_route2[] = {
143                "route",
144                "add",
145                "default",
146                NET_CFG_GATEWAY_IP,
147                NULL
148        };
149
150        exit_code = rtems_bsd_command_route(RTEMS_BSD_ARGC(dflt_route), dflt_route);
151        assert(exit_code == EXIT_SUCCESS);
152
153        exit_code = rtems_bsd_command_route(RTEMS_BSD_ARGC(dflt_route2), dflt_route2);
154        assert(exit_code == EXIT_SUCCESS);
155#endif
156}
157#endif
158
159#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
160static void
161default_network_dhcpcd_task(rtems_task_argument arg)
162{
163        int exit_code;
164        char *dhcpcd[] = {
165                "dhcpcd",
166                NULL
167        };
168
169        (void)arg;
170
171#ifdef DEFAULT_NETWORK_DHCPCD_NO_DHCP_DISCOVERY
172        static const char cfg[] = "nodhcp\nnodhcp6\n";
173        int fd;
174        int rv;
175        ssize_t n;
176
177        fd = open("/etc/dhcpcd.conf", O_CREAT | O_WRONLY,
178            S_IRWXU | S_IRWXG | S_IRWXO);
179        assert(fd >= 0);
180
181        n = write(fd, cfg, sizeof(cfg));
182        assert(n == (ssize_t) sizeof(cfg));
183
184        rv = close(fd);
185        assert(rv == 0);
186#endif
187
188        exit_code = rtems_bsd_command_dhcpcd(RTEMS_BSD_ARGC(dhcpcd), dhcpcd);
189        assert(exit_code == EXIT_SUCCESS);
190}
191#endif
192
193static void
194default_network_dhcpcd(void)
195{
196#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
197        rtems_status_code sc;
198        rtems_id id;
199
200        sc = rtems_task_create(
201                rtems_build_name('D', 'H', 'C', 'P'),
202                RTEMS_MAXIMUM_PRIORITY - 1,
203                2 * RTEMS_MINIMUM_STACK_SIZE,
204                RTEMS_DEFAULT_MODES,
205                RTEMS_FLOATING_POINT,
206                &id
207        );
208        assert(sc == RTEMS_SUCCESSFUL);
209
210        sc = rtems_task_start(id, default_network_dhcpcd_task, 0);
211        assert(sc == RTEMS_SUCCESSFUL);
212#endif
213}
214
215static void
216default_network_on_exit(int exit_code, void *arg)
217{
218        rtems_printer printer;
219
220        (void)arg;
221
222        rtems_print_printer_printf(&printer);
223        rtems_stack_checker_report_usage_with_plugin(&printer);
224
225        if (exit_code == 0) {
226                puts("*** END OF TEST " TEST_NAME " ***");
227        }
228}
229
230static void
231Init(rtems_task_argument arg)
232{
233        rtems_status_code sc;
234#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
235#ifdef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
236        char ifnamebuf[IF_NAMESIZE];
237#endif
238        char *ifname;
239#endif
240
241        (void)arg;
242        puts("*** " TEST_NAME " TEST ***");
243
244        on_exit(default_network_on_exit, NULL);
245
246#ifdef DEFAULT_EARLY_INITIALIZATION
247        early_initialization();
248#endif
249
250        /* Let other tasks run to complete background work */
251        default_network_set_self_prio(RTEMS_MAXIMUM_PRIORITY - 1U);
252
253#ifdef DEFAULT_NETWORK_SHELL
254        sc = rtems_shell_init(
255                "SHLL",
256                32 * 1024,
257                1,
258                CONSOLE_DEVICE_NAME,
259                false,
260                false,
261                NULL
262        );
263        assert(sc == RTEMS_SUCCESSFUL);
264#endif
265
266        rtems_bsd_initialize();
267
268#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
269#ifdef DEFAULT_NETWORK_NO_STATIC_IFCONFIG
270        ifname = if_indextoname(1, &ifnamebuf[0]);
271        assert(ifname != NULL);
272#else
273        ifname = NET_CFG_INTERFACE_0;
274#endif
275#endif
276
277        /* Let the callout timer allocate its resources */
278        sc = rtems_task_wake_after(2);
279        assert(sc == RTEMS_SUCCESSFUL);
280
281        default_network_ifconfig_lo0();
282#ifndef DEFAULT_NETWORK_NO_INTERFACE_0
283        default_network_ifconfig_hwif0(ifname);
284        default_network_route_hwif0(ifname);
285#endif
286        default_network_dhcpcd();
287
288        test_main();
289
290        assert(0);
291}
292
293#include <machine/rtems-bsd-sysinit.h>
294
295SYSINIT_NEED_NET_PF_UNIX;
296SYSINIT_NEED_NET_IF_LAGG;
297SYSINIT_NEED_NET_IF_VLAN;
298
299#include <bsp/nexus-devices.h>
300
301#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
302#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
303#define CONFIGURE_APPLICATION_NEEDS_STUB_DRIVER
304#define CONFIGURE_APPLICATION_NEEDS_ZERO_DRIVER
305#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK
306
307#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
308
309#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 32
310
311#define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1
312
313#define CONFIGURE_UNLIMITED_ALLOCATION_SIZE 32
314#define CONFIGURE_UNLIMITED_OBJECTS
315#define CONFIGURE_UNIFIED_WORK_AREAS
316
317#define CONFIGURE_STACK_CHECKER_ENABLED
318
319#define CONFIGURE_BDBUF_BUFFER_MAX_SIZE (64 * 1024)
320#define CONFIGURE_BDBUF_MAX_READ_AHEAD_BLOCKS 4
321#define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE (1 * 1024 * 1024)
322
323#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
324
325#define CONFIGURE_INIT_TASK_STACK_SIZE (32 * 1024)
326#define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
327#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
328
329#define CONFIGURE_INIT
330
331#include <rtems/confdefs.h>
332
333#ifdef DEFAULT_NETWORK_SHELL
334
335#define CONFIGURE_SHELL_COMMANDS_INIT
336
337#include <bsp/irq-info.h>
338
339#include <rtems/netcmds-config.h>
340
341#define CONFIGURE_SHELL_USER_COMMANDS \
342  &bsp_interrupt_shell_command, \
343  &rtems_shell_BSD_Command, \
344  &rtems_shell_HOSTNAME_Command, \
345  &rtems_shell_PING_Command, \
346  &rtems_shell_ROUTE_Command, \
347  &rtems_shell_NETSTAT_Command, \
348  &rtems_shell_IFCONFIG_Command, \
349  &rtems_shell_TCPDUMP_Command, \
350  &rtems_shell_SYSCTL_Command
351
352#define CONFIGURE_SHELL_COMMAND_CPUUSE
353#define CONFIGURE_SHELL_COMMAND_PERIODUSE
354#define CONFIGURE_SHELL_COMMAND_STACKUSE
355#define CONFIGURE_SHELL_COMMAND_PROFREPORT
356
357#define CONFIGURE_SHELL_COMMAND_CP
358#define CONFIGURE_SHELL_COMMAND_PWD
359#define CONFIGURE_SHELL_COMMAND_LS
360#define CONFIGURE_SHELL_COMMAND_LN
361#define CONFIGURE_SHELL_COMMAND_LSOF
362#define CONFIGURE_SHELL_COMMAND_CHDIR
363#define CONFIGURE_SHELL_COMMAND_CD
364#define CONFIGURE_SHELL_COMMAND_MKDIR
365#define CONFIGURE_SHELL_COMMAND_RMDIR
366#define CONFIGURE_SHELL_COMMAND_CAT
367#define CONFIGURE_SHELL_COMMAND_MV
368#define CONFIGURE_SHELL_COMMAND_RM
369#define CONFIGURE_SHELL_COMMAND_MALLOC_INFO
370
371#include <rtems/shellconfig.h>
372
373#endif /* DEFAULT_NETWORK_SHELL */
Note: See TracBrowser for help on using the repository browser.