FreeBSD TCP/IP Stack port for eCos TCP/IP Networking for eCos now provides a complete TCP/IP networking stack, based on a snapshot of the FreeBSD code, released by the KAME project. The networking support is fully featured and well tested within the eCos environment. Networking Stack Features Since this networking package is based on BSD code, it is very complete and robust. The eCos implementation includes support for the following protocols: IPv4 UDP TCP ICMP raw packet interface Multi-cast addressing IPv6 (including UDP, ICP, ICMP) These additional features are also present in the package, but are not supported: Berkeley Packet Filter Uni-cast support Multi-cast routing Freebsd TCP/IP stack port This document describes how to get started with the Freebsd TCP/IP network stack. Targets A number of ethernet devices may be supported. The default configuration supports two instances of the interface by default, and you will need to write your own driver instantiation code, and supplemental startup and initialization code, if you should add additional ones. The target for your board will normally be supplied with an ethernet driver, in which case including the network stack and generic ethernet driver package to your build will automatically enable usage of the ethernet device driver. If your target is not supplied with an ethernet driver, you will need to use loopback (see ). <!--<conditionaltext>-->Building the Network Stack Using the Build->Packages dialog, add the packages “Networking”, “Freebsd TCP/IP Stack” and “Common Ethernet Support” to your configuration. Their package names are CYGPKG_NET, CYGPKG_NET_FREEBSD_STACK and CYGPKG_NET_ETH_DRIVERS respectively. A short-cut way to do this is by using the “net” template if it is available for your platform. The platform-specific ethernet device driver for your platform will be added as part of the target selection (in the Build->Templates “Hardware” item), along with the PCI I/O subsystem (if relevent) and the appropriate serial device driver. For example, the PowerPC MBX target selection adds the package PKG_NET_QUICC_ETH_DRIVERS, and the Cirrus Logic EDB7xxx target selection adds the package CYGPKG_NET_EDB7XXX_ETH_DRIVERS. After this, eCos and its tests can be built exactly as usual. By default, most of the network tests are not built. This is because some of them require manual intervention, i.e. they are to be run “by hand”, and are not suitable for automated testing. To build the full set of network tests, set the configuration option CYGPKG_NET_BUILD_TESTS “Build networking tests (demo programs)” within “Networking support build options”. APIs Standard networking The APIs for the standard networking calls such as socket(), recv() and so on, are in header files relative to the top-level include directory, within the standard subdirectories as conventionally found in /usr/include. For example: install/include/arpa/tftp.h install/include/netinet/tcpip.h install/include/sys/socket.h install/include/sys/socketvar.h install/include/sys/sockio.h network.h at the top level defines various extensions, for example the API init_all_network_interfaces(void) described above. We advise including network.h whether you use these features or not. In general, using the networking code may require definition of two symbols: _KERNEL and __ECOS. _KERNEL is not normally required; __ECOS is normally required. So add this to your compile lines for files which use the network stack: -D__ECOS To expand a little, it’s like this because this is a port of a standard distribution external to eCos. One goal is to perturb the sources as little as possible, so that upgrading and maintenance from the external distribution is simplified. The __ECOS symbol marks out the eCos additions in making the port. The _KERNEL symbol is traditional UNIX practice: it distinguishes a compilation which is to be linked into the kernel from one which is part of an application. eCos applications are fully linked, so this distinction does not apply. _KERNEL can however be used to control the visibility of the internals of the stack, so depending on what features your application uses, it may or may not be necessary. The include file network.h undefines _KERNEL unconditionally, to provide an application-like compilation environment. If you were writing code which, for example, enumerates the stack’s internal structures, that is a kernel-like compilation environment, so you would need to define _KERNEL (in addition to __ECOS) and avoid including network.h. Enhanced Select() The network stack supports an extension to the standard select semantics which allows all threads that are waiting to be restarted even if the select conditions are not satisfied. The standard select() API: int select(int nfd, fd_set *in, fd_set *out, fd_set *ex, struct timeval *tv); does not support the restart. The additional API: int cyg_select_with_abort(int nfd, fd_set *in, fd_set *out, fd_set *ex, struct timeval *tv) behaves exactly as select() with the additional feature that a call to void cyg_select_abort(void) will cause all threads waiting in any cyg_select_with_abort() call to cease waiting and continue execution. Disabling IPv6 Support at Runtime The FreeBSD stack starts up with IPv4 support in an essentially "disabled" state. It requires the application to take positive actions to assign IPv4 address and routes before the interfaces will respond to packets from the outside world. This is not true of IPv6 support. If IPv6 support was enabled in the eCos build, the stack will start up with link-local addresses assigned and interfaces listening to certain multicast addresses. The stack will not only respond to IPv6 packets, it will spontaneously send IPv6 packets without actions from application code. If an eCos application wishes to disable IPv6 support at runtime, the decision has to be made very early in the boot process and actions taken to prevent the invocation of IPv6 startup code in the network stack. This involves two rather obscure "tricks": Using a dummy device driver's "init" hook as a place to execute some code early in the startup process Monkey-patching the network stack's table of initializers by looping through it and checking the function pointers against certain globally visible symbols and replacing the two IPv6-specific function pointers with pointers to a "noop" function. The code listing below shows an example of disabling IPv6 support at runtime if the global variable we_want_to_disable_ipv6 is non-zero. The assumption is that some code in the HAL initialization or somewhere else has made the decision whether or not to disable IPv6 support and set that variable accordingly. // Sample code showing a method of disabling IPv6 support at runtime // Do-nothing function we can patch into the network stack's init table // when we want to disable one of the entries static void init_noop(void *dummy) { } // Function that loops through the network stack's init table and // disables the two IPv6 entries static void disable_ipv6(void) { extern struct init_tab_entry __NET_INIT_TAB__[], __NET_INIT_TAB_END__; struct init_tab_entry *init_entry; extern void cyg_net_add_domain(void *); extern void ip6_init2(void *); extern char inet6domain[]; for (init_entry = __NET_INIT_TAB__; init_entry != &__NET_INIT_TAB_END__; init_entry++) if ((init_entry->fun == ip6_init2) || (init_entry->fun == cyg_net_add_domain && init_entry->data == (void *)inet6domain)) init_entry->fun = init_noop; } // Function called early in the inintialization process via a dummy // device table entry static bool early_init(struct cyg_devtab_entry *tab) { if (we_want_to_disable_ipv6) disable_ipv6(); return 0; } // Dummy device driver table entry that will call the above early_init() // function during the startup process DEVTAB_ENTRY(device_master_early_init, "neveruse", NULL, NULL, early_init, NULL, NULL );