VIDEOS 》 FreeBSD Networking Sub-system

GitHub main FreeBSD src tree (read-only mirror) http://www.FreeBSD.org/ - https://github.com/freebsd/freebsd
struct mbuf (and struct pkthdr) - packet buffer - /sys/sys/mbuf.h - https://github.com/freebsd/freebsd/blob/master/sys/sys/mbuf.h

FreeBSD Networking Subsystem vs Linux Kernel Networking The FreeBSD Channel

GitHub main FreeBSD src tree (read-only mirror) http://www.FreeBSD.org/ - https://github.com/freebsd/freebsd
struct mbuf (and struct pkthdr) - packet buffer - /sys/sys/mbuf.h - https://github.com/freebsd/freebsd/blob/master/sys/sys/mbuf.h
struct mbuf - packet buffer APIs - /sys/kern/kern_mbuf.c - https://github.com/freebsd/freebsd/blob/master/sys/kern/kern_mbuf.c
struct mbuf - packet buffer APIs - /sys/kern/uipc_mbuf.c - https://github.com/freebsd/freebsd/blob/master/sys/kern/uipc_mbuf.c
struct mbuf - packet buffer APIs - /sys/kern/uipc_mbuf2.c - https://github.com/freebsd/freebsd/blob/master/sys/kern/uipc_mbuf2.c

FreeBSD IPv4 stack source - https://github.com/freebsd/freebsd/tree/master/sys/netinet
FreeBSD IPv6 stack source - https://github.com/freebsd/freebsd/tree/master/sys/netinet6
Structure defining a network interface - struct ifnet - https://github.com/freebsd/freebsd/blob/master/sys/net/if_var.h
--- vs ---
Linux Kernel Source:
Free Electrons online Kernel Source - http://elixir.free-electrons.com/linux/latest/source
struct sk_buff - packet buffer - /include/linux/skbuff.h - http://elixir.free-electrons.com/linux/latest/source/include/linux/skbuff.h
struct net_device - The DEVICE structure - /include/linux/netdevice.h - http://elixir.free-electrons.com/linux/latest/source/include/linux/netdevice.h

Linux Kernel IPv4-stack source - http://elixir.free-electrons.com/linux/latest/source/net/ipv4
Linux Kernel IPv6-stack source - http://elixir.free-electrons.com/linux/latest/source/net/ipv6

The source code of struct mbuf data-structure (/sys/sys/mbuf.h) for quick reference:

 * The core of the mbuf object along with some shortcut defines for practical
 * purposes.
struct mbuf {
	 * Header present at the beginning of every mbuf.
	 * Size ILP32: 24
	 *      LP64: 32
	 * Compile-time assertions in uipc_mbuf.c test these values to ensure
	 * that they are correct.
	union {	/* next buffer in chain */
		struct mbuf		*m_next;
		SLIST_ENTRY(mbuf)	m_slist;
		STAILQ_ENTRY(mbuf)	m_stailq;
	union {	/* next chain in queue/record */
		struct mbuf		*m_nextpkt;
		SLIST_ENTRY(mbuf)	m_slistpkt;
		STAILQ_ENTRY(mbuf)	m_stailqpkt;
	caddr_t		 m_data;	/* location of data */
	int32_t		 m_len;		/* amount of data in this mbuf */
	uint32_t	 m_type:8,	/* type of data in this mbuf */
			 m_flags:24;	/* flags; see below */
#if !defined(__LP64__)
	uint32_t	 m_pad;		/* pad for 64bit alignment */

	 * A set of optional headers (packet header, external storage header)
	 * and internal data storage.  Historically, these arrays were sized
	 * to MHLEN (space left after a packet header) and MLEN (space left
	 * after only a regular mbuf header); they are now variable size in
	 * order to support future work on variable-size mbufs.
	union {
		struct {
			struct pkthdr	m_pkthdr;	/* M_PKTHDR set */
			union {
				struct m_ext	m_ext;	/* M_EXT set */
				char		m_pktdat[0];
		char	m_dat[0];			/* !M_PKTHDR, !M_EXT */

The source code of struct pkthdr data-structure (/sys/sys/mbuf.h) for quick reference:

 * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set.
 * Size ILP32: 48
 *	 LP64: 56
 * Compile-time assertions in uipc_mbuf.c test these values to ensure that
 * they are correct.
struct pkthdr {
	union {
		struct m_snd_tag *snd_tag;	/* send tag, if any */
		struct ifnet	*rcvif;		/* rcv interface */
	SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */
	int32_t		 len;		/* total packet length */

	/* Layer crossing persistent information. */
	uint32_t	 flowid;	/* packet's 4-tuple system */
	uint32_t	 csum_flags;	/* checksum and offload features */
	uint16_t	 fibnum;	/* this packet should use this fib */
	uint8_t		 cosqos;	/* class/quality of service */
	uint8_t		 rsstype;	/* hash type */
	union {
		uint64_t	rcv_tstmp;	/* timestamp in ns */
		struct {
			uint8_t		 l2hlen;	/* layer 2 hdr len */
			uint8_t		 l3hlen;	/* layer 3 hdr len */
			uint8_t		 l4hlen;	/* layer 4 hdr len */
			uint8_t		 l5hlen;	/* layer 5 hdr len */
			uint32_t	 spare;
	union {
		uint8_t  eight[8];
		uint16_t sixteen[4];
		uint32_t thirtytwo[2];
		uint64_t sixtyfour[1];
		uintptr_t unintptr[1];
		void	*ptr;
	} PH_per;

	/* Layer specific non-persistent local storage for reassembly, etc. */
	union {
		uint8_t  eight[8];
		uint16_t sixteen[4];
		uint32_t thirtytwo[2];
		uint64_t sixtyfour[1];
		uintptr_t unintptr[1];
		void 	*ptr;
	} PH_loc;

The source code of struct ifnet data-structure (/sys/net/if_var.h) for quick reference:

 * Structure defining a network interface.
struct ifnet {
	/* General book keeping of interface lists. */
	TAILQ_ENTRY(ifnet) if_link; 	/* all struct ifnets are chained */
	LIST_ENTRY(ifnet) if_clones;	/* interfaces of a cloner */
	TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if */
					/* protected by if_addr_lock */
	u_char	if_alloctype;		/* if_type at time of allocation */

	/* Driver and protocol specific information that remains stable. */
	void	*if_softc;		/* pointer to driver state */
	void	*if_llsoftc;		/* link layer softc */
	void	*if_l2com;		/* pointer to protocol bits */
	const char *if_dname;		/* driver name */
	int	if_dunit;		/* unit or IF_DUNIT_NONE */
	u_short	if_index;		/* numeric abbreviation for this if  */
	short	if_index_reserved;	/* spare space to grow if_index */
	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
	char	*if_description;	/* interface description */

	/* Variable fields that are touched by the stack and drivers. */
	int	if_flags;		/* up/down, broadcast, etc. */
	int	if_drv_flags;		/* driver-managed status flags */
	int	if_capabilities;	/* interface features & capabilities */
	int	if_capenable;		/* enabled features & capabilities */
	void	*if_linkmib;		/* link-type-specific MIB data */
	size_t	if_linkmiblen;		/* length of above data */
	u_int	if_refcount;		/* reference count */

	/* These fields are shared with struct if_data. */
	uint8_t		if_type;	/* ethernet, tokenring, etc */
	uint8_t		if_addrlen;	/* media address length */
	uint8_t		if_hdrlen;	/* media header length */
	uint8_t		if_link_state;	/* current link state */
	uint32_t	if_mtu;		/* maximum transmission unit */
	uint32_t	if_metric;	/* routing metric (external only) */
	uint64_t	if_baudrate;	/* linespeed */
	uint64_t	if_hwassist;	/* HW offload capabilities, see IFCAP */
	time_t		if_epoch;	/* uptime at attach or stat reset */
	struct timeval	if_lastchange;	/* time of last administrative change */

	struct  ifaltq if_snd;		/* output queue (includes altq) */
	struct	task if_linktask;	/* task for link change events */

	/* Addresses of different protocol families assigned to this if. */
	struct	rwlock if_addr_lock;	/* lock to protect address lists */
		 * if_addrhead is the list of all addresses associated to
		 * an interface.
		 * Some code in the kernel assumes that first element
		 * of the list has type AF_LINK, and contains sockaddr_dl
		 * addresses which store the link-level address and the name
		 * of the interface.
		 * However, access to the AF_LINK address through this
		 * field is deprecated. Use if_addr or ifaddr_byindex() instead.
	struct	ifaddrhead if_addrhead;	/* linked list of addresses per if */
	struct	ifmultihead if_multiaddrs; /* multicast addresses configured */
	int	if_amcount;		/* number of all-multicast requests */
	struct	ifaddr	*if_addr;	/* pointer to link-level address */
	void	*if_hw_addr;		/* hardware link-level address */
	const u_int8_t *if_broadcastaddr; /* linklevel broadcast bytestring */
	struct	rwlock if_afdata_lock;
	void	*if_afdata[AF_MAX];
	int	if_afdata_initialized;

	/* Additional features hung off the interface. */
	u_int	if_fib;			/* interface FIB */
	struct	vnet *if_vnet;		/* pointer to network stack instance */
	struct	vnet *if_home_vnet;	/* where this ifnet originates from */
	struct  ifvlantrunk *if_vlantrunk; /* pointer to 802.1q data */
	struct	bpf_if *if_bpf;		/* packet filter structure */
	int	if_pcount;		/* number of promiscuous listeners */
	void	*if_bridge;		/* bridge glue */
	void	*if_lagg;		/* lagg glue */
	void	*if_pf_kif;		/* pf glue */
	struct	carp_if *if_carp;	/* carp interface structure */
	struct	label *if_label;	/* interface MAC label */
	struct	netmap_adapter *if_netmap; /* netmap(4) softc */

	/* Various procedures of the layer2 encapsulation and drivers. */
	int	(*if_output)		/* output routine (enqueue) */
		(struct ifnet *, struct mbuf *, const struct sockaddr *,
		     struct route *);
	void	(*if_input)		/* input routine (from h/w driver) */
		(struct ifnet *, struct mbuf *);
	if_start_fn_t	if_start;	/* initiate output routine */
	if_ioctl_fn_t	if_ioctl;	/* ioctl routine */
	if_init_fn_t	if_init;	/* Init routine */
	int	(*if_resolvemulti)	/* validate/resolve multicast */
		(struct ifnet *, struct sockaddr **, struct sockaddr *);
	if_qflush_fn_t	if_qflush;	/* flush any queue */	
	if_transmit_fn_t if_transmit;   /* initiate output routine */

	void	(*if_reassign)		/* reassign to vnet routine */
		(struct ifnet *, struct vnet *, char *);
	if_get_counter_t if_get_counter; /* get counter values */
	int	(*if_requestencap)	/* make link header from request */
		(struct ifnet *, struct if_encap_req *);

	/* Statistics. */
	counter_u64_t	if_counters[IFCOUNTERS];

	/* Stuff that's only temporary and doesn't belong here. */

	 * Network adapter TSO limits:
	 * ===========================
	 * If the "if_hw_tsomax" field is zero the maximum segment
	 * length limit does not apply. If the "if_hw_tsomaxsegcount"
	 * or the "if_hw_tsomaxsegsize" field is zero the TSO segment
	 * count limit does not apply. If all three fields are zero,
	 * there is no TSO limit.
	 * NOTE: The TSO limits should reflect the values used in the
	 * BUSDMA tag a network adapter is using to load a mbuf chain
	 * for transmission. The TCP/IP network stack will subtract
	 * space for all linklevel and protocol level headers and
	 * ensure that the full mbuf chain passed to the network
	 * adapter fits within the given limits.
	u_int	if_hw_tsomax;		/* TSO maximum size in bytes */
	u_int	if_hw_tsomaxsegcount;	/* TSO maximum segment count */
	u_int	if_hw_tsomaxsegsize;	/* TSO maximum segment size in bytes */

	 * Network adapter send tag support:
	if_snd_tag_alloc_t *if_snd_tag_alloc;
	if_snd_tag_modify_t *if_snd_tag_modify;
	if_snd_tag_query_t *if_snd_tag_query;
	if_snd_tag_free_t *if_snd_tag_free;

	 * Spare fields to be added before branching a stable branch, so
	 * that structure can be enhanced without changing the kernel
	 * binary interface.
	int	if_ispare[4];		/* general use */

Suggested Topics:

Video Episodes :: FreeBSD

pfSense - free and open source firewall and router based on FreeBSD that also features unified threat management, load balancing, multi WAN, and more ↗
Saturday' 13-Mar-2021
Watch detailed videos and read topics on pfSense a FreeBSD based Firewall

Generic FreeBSD ↗
Saturday' 13-Mar-2021
FreeBSD is an Open-Source operating system used to power networking appliances, modern servers, desktops, and embedded platforms. A large community has continually developed it for more than three decades. Its advanced networking, security, and storage features have made FreeBSD the platform of choice for many of the busiest web sites, embedded networking solutions and storage devices.

FreeBSD Networking Sub-system ↗
Saturday' 13-Mar-2021

TrueNAS (formerly known as FreeNAS) - free and open-source network-attached storage software based on FreeBSD ↗
Tuesday' 03-May-2022
TrueNAS (formerly known as FreeNAS) - is a free and open-source network-attached storage (NAS) operating systems produced by iXsystems based on FreeBSD and Linux, using the OpenZFS file system. It is licensed under the terms of the BSD License and runs on commodity x86-64 hardware. The TrueNAS range includes free public versions TrueNAS CORE, commercial versions (TrueNAS Enterprise), Linux versions (TrueNAS SCALE). iXsystems also offers hardware, from small home systems to large 10 petabyte arrays, based on the above versions.

Join The Linux Channel :: Facebook Group ↗

Visit The Linux Channel :: on Youtube ↗

💗 Help shape the future: Sponsor/Donate

Recommended Topics:
Featured Video:
Watch on Youtube - [387//0] x263 Inline Programming | Assembly | Scripts | php, python, shell, etc | Rust in Linux Kernel ↗

libpcap Library | Linux User-space Network Stack Development ↗
Sunday' 06-Aug-2023
libpcap is a very popular user-space networking library, with which you can capture and or generate packets. libpcap is the underlying framework for many popular packet capture tools such as tcpdump, Wireshark and so on. In fact libpcap is a part of tcpdump project. But besides just using it as a packet capture tool, you can use libpcap in various applications, such as user-space based networking stack development, etc. In some cases libpcap is yet another alternative to raw-sockets and tun/tap interfaces.

The Linux Channel :: Sponsors ↗
Monday' 30-May-2022
Here is a list of all The Linux Channel sponsors/donors (individual/companies).

Inline Programming | Assembly | Scripts | php, python, shell, etc | Rust in Linux Kernel ↗
Friday' 12-May-2023
Inline programming is a technique where code statements are included directly in the text of a program, instead of being contained in separate files or modules. Inline programming can be useful for small or simple tasks, as it can eliminate the need for a separate script or function. One common example of inline programming is using JavaScripts, Php, etc in HTML documents to create dynamic content. Similarly in Linux Kernel we can find lot of instances where we can find inline programming such as inline assembly and now Rust within the Kernel source.

Linux Kernel /sysfs Interface ↗
Saturday' 14-May-2022
/sysfs is one of the most popular kernel to user-space interface which you can leverage to add an interface to your Kernel code such as Kernel modules, Kernel Device Drivers, etc. Although personally I prefer /proc interface than other alternatives such as /sysfs, ioctl() and so on for my personal Kernel modules/stack. So here is my detailed multi-episode Youtube video series on /sysfs Interface.

Rockchip ROC-RK3566-PC from Firefly | OpenWRT ↗
Thursday' 19-Oct-2023
Here is my multi-episode video series on evaluation of Rockchip ROC-RK3566-PC from Firefly with stock OpenWRT firmware.

What is purpose of Kernel Development - Example SMOAD Networks SDWAN Orchestrator Firewall Kernel Engine ↗
Monday' 18-Jul-2022
Often aspiring students may have this question, that what is the purpose of Linux Kernel Development. Since Linux Kernel is very mature and it has almost everything one would need. Usually, we need custom kernel development in the case of any new driver development for new upcoming hardware. And this happens on and on. But at times we may also come across few features/modules/components which are already provided by the Linux Kernel which are not adequate or atleast not the way we exactly intended to use. So, this is the real-world example, sometimes no matter what Linux Kernel provides as a part of stock Kernel/OS features, sometimes we have to write our own custom kernel stack or module(s) which can specifically cater our exact needs.

Linux Kernel Driver Device Trees ↗
Tuesday' 17-Jan-2023
The Linux kernel is the backbone of the Linux operating system. A device tree is a hierarchical tree structure that describes the various devices that are present in a system, including their properties and relationships to one another. The device tree is used by the Linux kernel to identify and initialize the different devices on a system, and to provide a consistent interface for interacting with them.

Linux Kernel vs User-space - Library APIs - Linux Kernel Programming ↗
Friday' 27-Oct-2023
One of the important aspects a beginner who is into Linux Kernel space systems software development has to understand is that unlike user-space C/C++ programming, where you can freely include any library APIs via respective #include files (which are dynamically linked during run-time via those /lib .so files), in the case of Kernel space programming, these library APIs are written within the Kernel source itself. These are the fundamental APIs which we commonly use, such as memcpy(), memcmp(), strlen(), strcpy(), strcpy() and so on. So here is my detailed Youtube video episode on the same with live demo, walk-through and examples.

Porting Sample libpcap C code to Raw Sockets | User-space Network Stack Framework ↗
Monday' 04-Sep-2023
Here is my multi-episode video series where I demonstrate how you can port the my libpcap sample code, discussed in the earlier episode to raw-socket. This code should further help you to design and architect your own user-space Network stack on top of this fundamental framework.

Roadmap - How to become Systems Software Developer ↗
Friday' 13-May-2022
When you are at the beginning of your career or a student, and aspire to become a software developer, one of the avenues to choose is to become a hard-core Systems Software Developer. However it is easier said than done, since there are many aspects to it as you explore further. As a part of systems developer, you can get into core kernel space developer, kernel device drivers developer, embedded developer and get into things like board bring-up, porting, etc, or can become a user-space systems programmer, and so on. So here is my detailed multi-episode Youtube video series on Roadmap - How to become Systems Software Developer.

Trending Video:
Watch on Youtube - [85//0] 278 The Linux Channel - Managing the Youtube channel and video content ↗

Synchronization in Linux user-space - Architecting multi-process and multi-threads ↗
Saturday' 13-Mar-2021

Recommended Video:
Watch on Youtube - [4822//0] How To clean and maintain your Computer cooling fan? ↗