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

5-freebsd-12
Last change on this file since b1404f2 was b1404f2, checked in by Sebastian Huber <sebastian.huber@…>, on Apr 30, 2018 at 11:35:25 AM

Add DHCP clientid to test default network init

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