Repository navigation
net/netforward: Add receiver-aware multicast forwarding (stage 1). #22839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
85e1db4
96ecf03
23aae80
0f671b5
4803dc3
ff60368
fb28e04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| =================================== | ||
| Receiver-Aware Multicast Forwarding | ||
| =================================== | ||
|
|
||
| By default, a multicast packet that is received on a network device is | ||
| forwarded through every other network device when | ||
| ``CONFIG_NET_IPFORWARD_BROADCAST`` is selected, whether or not any receiver | ||
| is reachable through that device. ``CONFIG_NET_IPFORWARD_MULTICAST`` makes | ||
| the forwarding decision depend on the receivers: a multicast packet is | ||
| forwarded only through the network devices that are listed for its group in a | ||
| multicast forwarding table. | ||
|
|
||
| Behavior | ||
| ======== | ||
|
|
||
| With ``CONFIG_NET_IPFORWARD_MULTICAST``: | ||
|
|
||
| * A multicast packet is forwarded through the devices listed in the table | ||
| entry of its destination group, and only through them. | ||
| * A multicast packet whose group has no entry is not forwarded. | ||
| * A packet is never forwarded back out the device that it was received on. | ||
| * Broadcast packets are not affected and are still forwarded through all | ||
| devices. | ||
| * The other forwarding filters still apply: ``IFF_NOSRC_FORWARD`` and | ||
| ``IFF_NODST_FORWARD``, the link-local range 224.0.0.0/24 of IPv4, the | ||
| reserved, interface-local and link-local scopes of IPv6 (ffx0, ffx1 and | ||
| ffx2), and the TTL or hop limit. | ||
| * Delivery of the packet to the local host is not changed. It still depends | ||
| on the groups that were joined with IGMP or MLD on the receiving device. | ||
|
|
||
| Without ``CONFIG_NET_IPFORWARD_MULTICAST``, the behavior does not change and | ||
| no additional code or data is built. | ||
|
|
||
| Only IPv4 and IPv6 groups, without a source, are supported: an entry is a | ||
| ``(*,G)`` entry. | ||
|
|
||
| Forwarding Table | ||
| ================ | ||
|
|
||
| The table has ``CONFIG_NET_IPFORWARD_MULTICAST_NENTRIES`` entries and is | ||
| statically allocated. Each entry holds a group address, a bitmap of egress | ||
| interface indices, and an optional expiry time. The size of an entry is about | ||
| 32 bytes when IPv6 is enabled and about 24 bytes for IPv4 only. | ||
|
|
||
| An entry that has an expiry time is ignored once the time has elapsed, and its | ||
| slot is reused by the next entry that is added. When a network device goes | ||
| down or is unregistered, it is removed from every entry, and an entry without | ||
| any device left is freed. | ||
|
|
||
| Configuration | ||
| ============= | ||
|
|
||
| The table is configured with two ioctl commands on a socket, defined in | ||
| ``nuttx/net/ioctl.h``. The argument is a ``struct netfwd_mcastreq_s`` from | ||
| ``nuttx/net/netfwd_mcast.h``: | ||
|
|
||
| ``SIOCADDMCFWD`` | ||
| Add an egress interface to the entry of a group. The entry is created if | ||
| the group does not have one. ``imr_lifetime`` is the lifetime of the entry | ||
| in seconds, zero for an entry that does not expire. Adding a device | ||
| restarts the lifetime of the entry. | ||
|
|
||
| ``SIOCDELMCFWD`` | ||
| Remove an egress interface from the entry of a group. The entry is freed | ||
| when no interface is left. | ||
|
|
||
| For example, to forward the group 239.1.1.1 through ``eth1``: | ||
|
|
||
| .. code-block:: c | ||
| struct netfwd_mcastreq_s req; | ||
| struct sockaddr_in *group = (struct sockaddr_in *)&req.imr_group; | ||
| int sd = socket(AF_INET, SOCK_DGRAM, 0); | ||
| memset(&req, 0, sizeof(req)); | ||
| group->sin_family = AF_INET; | ||
| group->sin_addr.s_addr = inet_addr("239.1.1.1"); | ||
| strlcpy(req.imr_ifname, "eth1", IFNAMSIZ); | ||
| ioctl(sd, SIOCADDMCFWD, (unsigned long)&req); | ||
| ``EINVAL`` is returned if the group is not a multicast address, ``ENODEV`` if | ||
| the interface does not exist, and ``ENOSPC`` if the table is full. | ||
| ``SIOCDELMCFWD`` returns ``ENOENT`` if there is nothing to remove. | ||
|
|
||
| Locking | ||
| ======= | ||
|
|
||
| The table has its own lock, private to ``netfwd_mcast.c``. It is taken only | ||
| inside the table functions and nothing is called while it is held, so it is | ||
| a leaf lock: the packet path takes it with the lock of the receiving device | ||
| held, and the network lock is not needed to forward a packet. The ioctls | ||
| resolve the interface name with the device list locked, and | ||
| ``netdev_unregister()`` removes a device from the table with the same lock | ||
| held, so an entry cannot be added for a device that is being removed. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /**************************************************************************** | ||
| * include/nuttx/net/netfwd_mcast.h | ||
| * | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. The | ||
| * ASF licenses this file to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance with the | ||
| * License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| * | ||
| ****************************************************************************/ | ||
|
|
||
| #ifndef __INCLUDE_NUTTX_NET_NETFWD_MCAST_H | ||
| #define __INCLUDE_NUTTX_NET_NETFWD_MCAST_H | ||
|
|
||
| /**************************************************************************** | ||
| * Included Files | ||
| ****************************************************************************/ | ||
|
|
||
| #include <nuttx/config.h> | ||
|
|
||
| #include <sys/socket.h> | ||
| #include <stdint.h> | ||
| #include <net/if.h> | ||
|
|
||
| #ifdef CONFIG_NET_IPFORWARD_MULTICAST | ||
|
|
||
| /**************************************************************************** | ||
| * Public Types | ||
| ****************************************************************************/ | ||
|
|
||
| /* The argument of the SIOCADDMCFWD and SIOCDELMCFWD ioctl commands. The | ||
| * forwarding entry of a multicast group lists the egress interfaces that | ||
| * packets for the group are forwarded through. | ||
| */ | ||
|
|
||
| struct netfwd_mcastreq_s | ||
| { | ||
| struct sockaddr_storage imr_group; /* Group, AF_INET or AF_INET6 */ | ||
| char imr_ifname[IFNAMSIZ]; /* Name of the egress interface */ | ||
| uint32_t imr_lifetime; /* Lifetime of the entry in | ||
| * seconds, zero for no expiry. | ||
| * Only used to add */ | ||
| }; | ||
|
|
||
| /**************************************************************************** | ||
| * Public Function Prototypes | ||
| ****************************************************************************/ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" | ||
| { | ||
| #endif | ||
|
|
||
| /**************************************************************************** | ||
| * Name: netfwd_mcast_ioctl | ||
| * | ||
| * Description: | ||
| * Handle the SIOCADDMCFWD and SIOCDELMCFWD ioctl commands. | ||
| * | ||
| * Input Parameters: | ||
| * cmd - The ioctl command | ||
| * arg - A pointer to a struct netfwd_mcastreq_s | ||
| * | ||
| * Returned Value: | ||
| * OK on success, -ENOTTY if cmd is not a multicast forwarding command, | ||
| * or another negated errno value on failure: | ||
| * | ||
| * -EINVAL - The group is not a multicast address, the lifetime is | ||
| * too long, or the interface name is not terminated. | ||
| * -EAFNOSUPPORT - The address family of the group is not supported. | ||
| * -ENODEV - There is no interface with that name. | ||
| * -ENOSPC - The forwarding table is full. | ||
| * -ENOENT - SIOCDELMCFWD found no such entry. | ||
| * | ||
| ****************************************************************************/ | ||
|
|
||
| int netfwd_mcast_ioctl(int cmd, unsigned long arg); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* CONFIG_NET_IPFORWARD_MULTICAST */ | ||
| #endif /* __INCLUDE_NUTTX_NET_NETFWD_MCAST_H */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -360,7 +360,7 @@ static int ipv4_in(FAR struct net_driver_s *dev) | |
| net_ipv4addr_broadcast(destipaddr, dev->d_netmask))) | ||
| { | ||
| #ifdef CONFIG_NET_IPFORWARD_BROADCAST | ||
| ipv4_forward_broadcast(dev, ipv4); | ||
| bool forwarded = ipv4_forward_broadcast(dev, ipv4); | ||
| #endif | ||
|
|
||
| if (isfrag) | ||
|
|
@@ -376,9 +376,15 @@ static int ipv4_in(FAR struct net_driver_s *dev) | |
| } | ||
| #endif | ||
|
|
||
| #ifdef CONFIG_NET_IPFORWARD_BROADCAST | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. i put braces around the counter though: with NET_STATISTICS off (the default) the bare |
||
| if (!forwarded) | ||
| #endif | ||
| { | ||
| #ifdef CONFIG_NET_STATISTICS | ||
| g_netstats.ipv4.drop++; | ||
| g_netstats.ipv4.drop++; | ||
| #endif | ||
| } | ||
|
|
||
| goto drop; | ||
| } | ||
| else if (IN_MULTICAST(NTOHL(destipaddr))) | ||
|
|
@@ -393,9 +399,21 @@ static int ipv4_in(FAR struct net_driver_s *dev) | |
| else | ||
| #endif | ||
| { | ||
| #ifdef CONFIG_NET_IPFORWARD_MULTICAST | ||
| /* The group was not joined on this device, but the packet may | ||
| * still have egress interfaces in the forwarding table. | ||
| */ | ||
|
|
||
| bool forwarded = ipv4_forward_broadcast(dev, ipv4); | ||
|
|
||
| if (!forwarded) | ||
| #endif | ||
| { | ||
| #ifdef CONFIG_NET_STATISTICS | ||
| g_netstats.ipv4.drop++; | ||
| g_netstats.ipv4.drop++; | ||
| #endif | ||
| } | ||
|
|
||
| goto drop; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we use Linux/POSIX compliant interface?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the linux interface for this is the multicast routing api (linux/mroute.h, mroute6.h): MRT_INIT on a raw igmp socket, then MRT_ADD_VIF / MRT_ADD_MFC, i.e. the kernel mfc that smcroute/mrouted/pimd work with. i'd implement a minimal subset: vifs by ifindex, (,G) entries via MRT_ADD_MFC_PROXY with a zero origin, mapped onto the current table, and drop SIOCADDMCFWD/SIOCDELMCFWD. ipv4 needs a raw igmp socket first, since nuttx only has raw icmp/icmpv6 ip sockets today, so that would be a separate prep pr.
or did you mean socket options with struct group_req like MCAST_JOIN_GROUP? that's smaller, but it would be a nuttx-only option.
note that linux (,G) entries also check the interface the packet arrived on, today's table accepts any incoming interface, so the mrt option would add that check.
which one do you prefer? apps#4287 is already merged, so the test app would get a follow-up pr either way.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it's better to align with linux userspace contract, we can implement the minimal feature what we want now and improve the functionality step by step.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great, will do. plan: a small prep pr adding raw IPPROTO_IGMP sockets (needed for MRT_INIT), then this pr switches from SIOCADDMCFWD/SIOCDELMCFWD to a minimal MRT_/MRT6_ subset, then a follow-up apps pr for the test app (also for the kconfig rename). i'll convert this pr to draft until then.