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

55-freebsd-126-freebsd-12
Last change on this file since b2eb48c was 8bd38d6, checked in by Sebastian Huber <sebastian.huber@…>, on 05/02/18 at 06:58:48

dhcpcd: Add rtems_dhcpcd_start()

Use it throughout to start the DHCP client (dhcpcd).

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