1*a5061eceSAdrien DestuguesHaiku Network Stack Architecture 2*a5061eceSAdrien Destugues================================ 3*a5061eceSAdrien Destugues 4*a5061eceSAdrien DestuguesThe Haiku Network Stack is a modular and layered networking stack, very 5*a5061eceSAdrien Destuguessimilar to what you may know as BONE. 6*a5061eceSAdrien Destugues 7*a5061eceSAdrien DestuguesThe entry point when talking to the stack is through a dedicated device 8*a5061eceSAdrien Destuguesdriver that publish itself in /dev/net. The userland library 9*a5061eceSAdrien Destugueslibnetwork.so (which combines libsocket.so, and libbind.so) directly 10*a5061eceSAdrien Destuguestalks to this driver, mostly via ioctl()\ `1 <#foot1>`__. 11*a5061eceSAdrien Destugues 12*a5061eceSAdrien DestuguesThe driver either creates sockets, or passes on every command to the 13*a5061eceSAdrien Destuguessocket module\ `2 <#foot2>`__. Depending on the address family and type 14*a5061eceSAdrien Destuguesof the sockets, the lower layers will be loaded and connected. 15*a5061eceSAdrien Destugues 16*a5061eceSAdrien DestuguesFor example, with a TCP/IP socket, the stack could look like this: 17*a5061eceSAdrien Destugues 18*a5061eceSAdrien Destugues+------------------+--------------------------------------------------------+ 19*a5061eceSAdrien Destugues| **Socket** | 20*a5061eceSAdrien Destugues+------------------+--------------------------------------------------------+ 21*a5061eceSAdrien Destugues| TCP | Protocols defined by the socket (address family, type) | 22*a5061eceSAdrien Destugues+------------------+ | 23*a5061eceSAdrien Destugues| IPv4 | (session, transport, network layers) | 24*a5061eceSAdrien Destugues+------------------+--------------------------------------------------------+ 25*a5061eceSAdrien Destugues| **Datalink** | 26*a5061eceSAdrien Destugues+------------------+--------------------------------------------------------+ 27*a5061eceSAdrien Destugues| ARP | Datalink Protocols defined by the interface | 28*a5061eceSAdrien Destugues| | (IP address, device) | 29*a5061eceSAdrien Destugues+------------------+ | 30*a5061eceSAdrien Destugues| Ethernet framing | (datalink layer) | 31*a5061eceSAdrien Destugues+------------------+--------------------------------------------------------+ 32*a5061eceSAdrien Destugues| Ethernet device | (physical layer) | 33*a5061eceSAdrien Destugues+------------------+--------------------------------------------------------+ 34*a5061eceSAdrien Destugues 35*a5061eceSAdrien DestuguesWhere TCP, and IPv4 are net_protocol modules, and ARP, and the Ethernet 36*a5061eceSAdrien Destuguesframing are net_datalink_protocol modules. All modules are connected in 37*a5061eceSAdrien Destuguesa chain, even though the datalink layer introduces more than one path 38*a5061eceSAdrien Destugues(one for each interface). 39*a5061eceSAdrien Destugues 40*a5061eceSAdrien DestuguesWhen sending data through a socket, a net_buffer is created in the 41*a5061eceSAdrien Destuguessocket module, and passed on to the lower levels where each protocol 42*a5061eceSAdrien Destuguesprocesses it, before passing it on to the next protocol in the chain. 43*a5061eceSAdrien DestuguesThe last protocol in the chain is always a domain protocol - it will 44*a5061eceSAdrien Destuguesdirectly forward the buffers to the datalink module. When the buffer 45*a5061eceSAdrien Destuguesreaches the datalink level, an accompanied net_route object will 46*a5061eceSAdrien Destuguesdetermine for which interface (which determines the datalink protocols 47*a5061eceSAdrien Destuguesin the chain) the buffer is destined. The route has to be specified by 48*a5061eceSAdrien Destuguesthe upper protocols before the buffer gets into the datalink level - if 49*a5061eceSAdrien Destuguesa buffer comes in without a valid route, it is discarded. 50*a5061eceSAdrien Destugues 51*a5061eceSAdrien DestuguesThe protocol modules are loaded and unloaded as needed. The stack itself 52*a5061eceSAdrien Destuguesstays loaded as long as there are interfaces defined - as soon as the 53*a5061eceSAdrien Destugueslast interface is removed, the stack gets unloaded (which is, of course, 54*a5061eceSAdrien Destuguesnot yet implemented). 55*a5061eceSAdrien Destugues 56*a5061eceSAdrien DestuguesThe Structures and Classes 57*a5061eceSAdrien Destugues~~~~~~~~~~~~~~~~~~~~~~~~~~ 58*a5061eceSAdrien Destugues 59*a5061eceSAdrien Destuguesnet_domain 60*a5061eceSAdrien Destugues^^^^^^^^^^ 61*a5061eceSAdrien Destugues 62*a5061eceSAdrien DestuguesEvery supported address family gets its own domain. A domain comprises 63*a5061eceSAdrien Destuguessuch a family, a net_protocol module that handles this domain, and a 64*a5061eceSAdrien Destugueslist of interfaces and routes. It also gets a name: for example, the 65*a5061eceSAdrien DestuguesIPv4 module registers the "internet" domain (AF_INET). 66*a5061eceSAdrien Destugues 67*a5061eceSAdrien DestuguesThe domain protocol module is responsible for managing the domain; it 68*a5061eceSAdrien Destugueshas to register it when it's loaded, and it has to unregister it when it 69*a5061eceSAdrien Destuguesis unloaded by the networking stack. 70*a5061eceSAdrien Destugues 71*a5061eceSAdrien Destuguesnet_interface 72*a5061eceSAdrien Destugues^^^^^^^^^^^^^ 73*a5061eceSAdrien Destugues 74*a5061eceSAdrien DestuguesAn interface makes an underlying net_device accessible by the stack. 75*a5061eceSAdrien DestuguesWhen creating a new interface, you have to specify a domain, and a 76*a5061eceSAdrien Destuguesdevice to be used. The stack will then look through the registered 77*a5061eceSAdrien Destuguesdatalink protocols, and builds a chain of them for that interface. 78*a5061eceSAdrien Destugues 79*a5061eceSAdrien DestuguesThe interface usually gets a network address, and a route that directs 80*a5061eceSAdrien Destuguesbuffers to be sent to it. If there is no route to an interface, it will 81*a5061eceSAdrien Destuguesnever be used for outgoing data, but may well receive data from other 82*a5061eceSAdrien Destugueshosts. 83*a5061eceSAdrien Destugues 84*a5061eceSAdrien DestuguesAn interface can be "up" (when ``IFF_UP`` is set in its ``flags`` 85*a5061eceSAdrien Destuguesmember) in which case it accepts data - when that flag is not set, it 86*a5061eceSAdrien Destugueswill discard all data it gets. The interface also specifies the maximum 87*a5061eceSAdrien Destuguesbuffer size that can be sent over this interface (the ``mtu`` member, 88*a5061eceSAdrien Destuguesa.k.a. maximum transmission unit). 89*a5061eceSAdrien Destugues 90*a5061eceSAdrien DestuguesInterfaces are configured via ioctl()s (SIOCAIFADDR, ...). You can use 91*a5061eceSAdrien Destuguesthe command line tool "ifconfig" to do this for you. 92*a5061eceSAdrien Destugues 93*a5061eceSAdrien Destuguesnet_device 94*a5061eceSAdrien Destugues^^^^^^^^^^ 95*a5061eceSAdrien Destugues 96*a5061eceSAdrien DestuguesA networking device is used to actually send and receive the buffers. It 97*a5061eceSAdrien Destugueseither points to an actual hardware device (in case of ethernet), or to 98*a5061eceSAdrien Destuguesa virtual device (in case of loopback). Every device has a unique name 99*a5061eceSAdrien Destuguesthat identifies it. When creating a device, the name also decides which 100*a5061eceSAdrien Destuguesnet_device module will be chosen; for example, everything that starts 101*a5061eceSAdrien Destugueswith "loop" will end up in the loopback device, while the ethernet 102*a5061eceSAdrien Destuguesdevice accepts names that start with "/dev/net/". 103*a5061eceSAdrien Destugues 104*a5061eceSAdrien DestuguesA device can be shared by many interfaces at the same time. The device 105*a5061eceSAdrien Destuguesto be used by an interface is specified at the time an interface is 106*a5061eceSAdrien Destuguescreated. It also has an ``mtu`` member that determines the upper limit 107*a5061eceSAdrien Destuguesof an interface's ``mtu`` as well. 108*a5061eceSAdrien Destugues 109*a5061eceSAdrien Destuguesnet_buffer 110*a5061eceSAdrien Destugues^^^^^^^^^^ 111*a5061eceSAdrien Destugues 112*a5061eceSAdrien DestuguesA buffer holds exactly one packet, and has a source as well as a 113*a5061eceSAdrien Destuguesdestination address. The addresses may be changed in every layer the 114*a5061eceSAdrien Destuguesbuffer passes through. For example, the datalink protocols usually use 115*a5061eceSAdrien Destuguessockaddr_dl structures with family AF_DLI, while the upper levels may 116*a5061eceSAdrien Destuguesuse sockaddr_in structures with family AF_INET. Every protocol only 117*a5061eceSAdrien Destuguessupports a small number of address types, and it's the requirement of 118*a5061eceSAdrien Destuguesthe upper protocols to prepare the address for use in the lower 119*a5061eceSAdrien Destuguesprotocols (and that's also a reason why it wouldn't work to arbitrarily 120*a5061eceSAdrien Destuguesstack protocols onto each other). 121*a5061eceSAdrien Destugues 122*a5061eceSAdrien DestuguesThe net_buffer module can be used to access the data within the buffer, 123*a5061eceSAdrien Destuguesappend new data to the buffer, or remove chunks of data from it. 124*a5061eceSAdrien DestuguesInternally, the buffer consists of usually fixed size (2048 byte) 125*a5061eceSAdrien Destuguesbuffers that can be shared or connected as needed. 126*a5061eceSAdrien Destugues 127*a5061eceSAdrien Destuguesnet_socket 128*a5061eceSAdrien Destugues^^^^^^^^^^ 129*a5061eceSAdrien Destugues 130*a5061eceSAdrien DestuguesThe socket is only of interest for the net_protocol modules, as it 131*a5061eceSAdrien Destuguesstores options that may have an effect on the protocol's performance. 132*a5061eceSAdrien DestuguesIt's the direct counterpart to a socket file descriptor in userland, but 133*a5061eceSAdrien Destuguesit has only little logic bound to it. 134*a5061eceSAdrien Destugues 135*a5061eceSAdrien DestuguesWhen a socket is created, the networking stack creates a chain of 136*a5061eceSAdrien Destuguesnet_protocol modules for the socket that will then do the real work. 137*a5061eceSAdrien DestuguesWhen the socket is closed, the net_protocol chain is freed, and the 138*a5061eceSAdrien Destuguesmodules are eventually unloaded (if they are no longer in use). 139*a5061eceSAdrien Destugues 140*a5061eceSAdrien Destuguesnet_protocol 141*a5061eceSAdrien Destugues^^^^^^^^^^^^ 142*a5061eceSAdrien Destugues 143*a5061eceSAdrien DestuguesThe protocols are bound to a specific socket, process the outgoing 144*a5061eceSAdrien Destuguesbuffers as needed (ie. add or remove headers, compute checksums, ...), 145*a5061eceSAdrien Destuguesand pass it on to the next protocol. The last protocol in the chain is 146*a5061eceSAdrien Destuguesalways a domain protocol that will forward the calls to the datalink 147*a5061eceSAdrien Destuguesmodule directly, if needed. 148*a5061eceSAdrien Destugues 149*a5061eceSAdrien DestuguesA domain protocol is a net_protocol that registered a domain, ie. IPv4. 150*a5061eceSAdrien DestuguesOther than usual protocols, domain protocols have some special 151*a5061eceSAdrien Destuguesrequirements: 152*a5061eceSAdrien Destugues 153*a5061eceSAdrien Destugues- they need to be able to execute send_data(), and get_domain() without 154*a5061eceSAdrien Destugues a pointer to its net_protocol object, as those may be called outside 155*a5061eceSAdrien Destugues of the socket context. 156*a5061eceSAdrien Destugues- as mentioned, they also don't talk to the next protocol in the chain 157*a5061eceSAdrien Destugues (as they are always the last one), but to the datalink module 158*a5061eceSAdrien Destugues directly. 159*a5061eceSAdrien Destugues 160*a5061eceSAdrien DestuguesSimilar to the need to perform send_data() outside of the socket 161*a5061eceSAdrien Destuguescontext, all protocols that can receive data need to handle incoming 162*a5061eceSAdrien Destuguesdata without the socket context: incoming data is always handled outside 163*a5061eceSAdrien Destuguesof the socket context, as the actual target socket is unknown during 164*a5061eceSAdrien Destuguesprocessing. 165*a5061eceSAdrien Destugues 166*a5061eceSAdrien DestuguesOnly the top-most protocol will be able to forward the packet to the 167*a5061eceSAdrien Destuguestarget socket(s). To receive incoming data, a protocol must register 168*a5061eceSAdrien Destuguesitself as receiving protocol with the networking stack. The domain 169*a5061eceSAdrien Destuguesprotocol is usually registered automatically by a net_datalink_protocol 170*a5061eceSAdrien Destuguesmodule that knows about both ends (for example, the ARP module is both 171*a5061eceSAdrien DestuguesIPv4 and ethernet specific, and therefore registers the AF_INET domain 172*a5061eceSAdrien Destuguesto receive ethernet packets of type IP). 173*a5061eceSAdrien Destugues 174*a5061eceSAdrien Destuguesnet_datalink_protocol 175*a5061eceSAdrien Destugues^^^^^^^^^^^^^^^^^^^^^ 176*a5061eceSAdrien Destugues 177*a5061eceSAdrien DestuguesThe datalink protocols are bound to a specific net_interface, and 178*a5061eceSAdrien Destuguestherefore to a specific net_device as well. Outgoing data is processed 179*a5061eceSAdrien Destuguesso that it can be sent via the net_device. For example, the ARP protocol 180*a5061eceSAdrien Destugueswill replace sockaddr_in structures in the buffer with sockaddr_dl 181*a5061eceSAdrien Destuguesstructures describing the ethernet MAC address of the source and 182*a5061eceSAdrien Destuguesdestination hosts, the ethernet_frame protocol will add the usual 183*a5061eceSAdrien Destuguesethernet header, etc. 184*a5061eceSAdrien Destugues 185*a5061eceSAdrien DestuguesThe last protocol in the chain is also a special device interface bridge 186*a5061eceSAdrien Destuguesprotocol, that redirects the calls to the underlying net_device. 187*a5061eceSAdrien Destugues 188*a5061eceSAdrien DestuguesIncoming data is handled differently again; when you want to receive 189*a5061eceSAdrien Destuguesdata directly coming from a device, you can either register a deframing 190*a5061eceSAdrien Destuguesfunction for it, or a handler that will be called depending on what data 191*a5061eceSAdrien Destuguestype the deframing module reported. For example, the ethernet_frame 192*a5061eceSAdrien Destuguesmodule registers an ethernet deframing function, while the ARP module 193*a5061eceSAdrien Destuguesregisters a handler for ethernet ARP packets with the device. When the 194*a5061eceSAdrien Destuguesdeframing function reports a ``ETHER_TYPE_ARP`` packet, the ARP 195*a5061eceSAdrien Destuguesreceiving function will be called. 196*a5061eceSAdrien Destugues 197*a5061eceSAdrien Destuguesnet_route 198*a5061eceSAdrien Destugues^^^^^^^^^ 199*a5061eceSAdrien Destugues 200*a5061eceSAdrien DestuguesA route determines the target interface of an outgoing packet. A route 201*a5061eceSAdrien Destuguesis always owned by a specific domain, and the route is chosen by 202*a5061eceSAdrien Destuguescomparing the networking address of the outgoing buffer with the mask 203*a5061eceSAdrien Destuguesand address of the route. 204*a5061eceSAdrien Destugues 205*a5061eceSAdrien DestuguesA protocol will usually not use the routes directly, but use a 206*a5061eceSAdrien Destuguesnet_route_info object (see below), that will make sure that the route is 207*a5061eceSAdrien Destuguesupdated automatically whenever the routing table is changed. 208*a5061eceSAdrien Destugues 209*a5061eceSAdrien Destuguesnet_route_info 210*a5061eceSAdrien Destugues^^^^^^^^^^^^^^ 211*a5061eceSAdrien Destugues 212*a5061eceSAdrien DestuguesA routing helper for protocol usage: it stores the target address as 213*a5061eceSAdrien Destugueswell as the route to be used, and has to be registered with the 214*a5061eceSAdrien Destuguesnetworking stack via ``register_route_info()``. 215*a5061eceSAdrien Destugues 216*a5061eceSAdrien DestuguesThen, the stack will automatically update the route as needed, whenever 217*a5061eceSAdrien Destuguesthe routing table of the domain changes; it will always matches the 218*a5061eceSAdrien Destuguesaddress specified there. When the routing is no longer needed, you must 219*a5061eceSAdrien Destuguesunregister the net_route_info again. 220*a5061eceSAdrien Destugues 221*a5061eceSAdrien Destugues-------------- 222*a5061eceSAdrien Destugues 223*a5061eceSAdrien Destugues| 1 You can find the definition of the driver interface in 224*a5061eceSAdrien Destugues `headers/private/net/net_stack_interface.h <https://git.haiku-os.org/haiku/tree/headers/private/net>`__, 225*a5061eceSAdrien Destugues as well as the driver itself at 226*a5061eceSAdrien Destugues `src/add-ons/kernel/drivers/network <https://git.haiku-os.org/haiku/tree/src/add-ons/kernel/drivers/network>`__ 227*a5061eceSAdrien Destugues| 2\ `src/add-ons/kernel/network/stack/ <https://git.haiku-os.org/haiku/tree/src/add-ons/kernel/network/stack>`__ 228