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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 6cfc98d was 6cfc98d, checked in by Sebastian Huber <sebastian.huber@…>, on 10/01/14 at 12:20:42

nexus: Use a linker set for the devices

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