Skip to content

Commit

Permalink
net_if: add IFF_LOOPBACK/POINTOPOINT/MULTICAST/BROADCAST
Browse files Browse the repository at this point in the history
Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
  • Loading branch information
zhhyu7 committed Jul 1, 2022
1 parent 31cddc9 commit acff87c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
66 changes: 41 additions & 25 deletions include/net/if.h
Expand Up @@ -46,32 +46,48 @@

/* Interface flag bits */

#define IFF_DOWN (1 << 0) /* Interface is down */
#define IFF_UP (1 << 1) /* Interface is up */
#define IFF_RUNNING (1 << 2) /* Carrier is available */
#define IFF_IPv6 (1 << 3) /* Configured for IPv6 packet (vs ARP or IPv4) */
#define IFF_BOUND (1 << 4) /* Bound to a socket */
#define IFF_NOARP (1 << 7) /* ARP is not required for this packet */
#define IFF_DOWN (1 << 0) /* Interface is down */
#define IFF_UP (1 << 1) /* Interface is up */
#define IFF_RUNNING (1 << 2) /* Carrier is available */
#define IFF_IPv6 (1 << 3) /* Configured for IPv6 packet (vs ARP or IPv4) */
#define IFF_BOUND (1 << 4) /* Bound to a socket */
#define IFF_LOOPBACK (1 << 5) /* Is a loopback net */
#define IFF_POINTOPOINT (1 << 6) /* Is point-to-point link */
#define IFF_NOARP (1 << 7) /* ARP is not required for this packet */
#define IFF_MULTICAST (1 << 12) /* Supports multicast. */
#define IFF_BROADCAST (1 << 13) /* Broadcast address valid. */

/* Interface flag helpers */

#define IFF_SET_DOWN(f) do { (f) |= IFF_DOWN; } while (0)
#define IFF_SET_UP(f) do { (f) |= IFF_UP; } while (0)
#define IFF_SET_RUNNING(f) do { (f) |= IFF_RUNNING; } while (0)
#define IFF_SET_BOUND(f) do { (f) |= IFF_BOUND; } while (0)
#define IFF_SET_NOARP(f) do { (f) |= IFF_NOARP; } while (0)

#define IFF_CLR_DOWN(f) do { (f) &= ~IFF_DOWN; } while (0)
#define IFF_CLR_UP(f) do { (f) &= ~IFF_UP; } while (0)
#define IFF_CLR_RUNNING(f) do { (f) &= ~IFF_RUNNING; } while (0)
#define IFF_CLR_BOUND(f) do { (f) &= ~IFF_BOUND; } while (0)
#define IFF_CLR_NOARP(f) do { (f) &= ~IFF_NOARP; } while (0)

#define IFF_IS_DOWN(f) (((f) & IFF_DOWN) != 0)
#define IFF_IS_UP(f) (((f) & IFF_UP) != 0)
#define IFF_IS_RUNNING(f) (((f) & IFF_RUNNING) != 0)
#define IFF_IS_BOUND(f) (((f) & IFF_BOUND) != 0)
#define IFF_IS_NOARP(f) (((f) & IFF_NOARP) != 0)
#define IFF_SET_DOWN(f) do { (f) |= IFF_DOWN; } while (0)
#define IFF_SET_UP(f) do { (f) |= IFF_UP; } while (0)
#define IFF_SET_RUNNING(f) do { (f) |= IFF_RUNNING; } while (0)
#define IFF_SET_BOUND(f) do { (f) |= IFF_BOUND; } while (0)
#define IFF_SET_NOARP(f) do { (f) |= IFF_NOARP; } while (0)
#define IFF_SET_LOOPBACK(f) do { (f) |= IFF_LOOPBACK; } while (0)
#define IFF_SET_POINTOPOINT(f) do { (f) |= IFF_POINTOPOINT; } while (0)
#define IFF_SET_MULTICAST(f) do { (f) |= IFF_MULTICAST; } while (0)
#define IFF_SET_BROADCAST(f) do { (f) |= IFF_BROADCAST; } while (0)

#define IFF_CLR_DOWN(f) do { (f) &= ~IFF_DOWN; } while (0)
#define IFF_CLR_UP(f) do { (f) &= ~IFF_UP; } while (0)
#define IFF_CLR_RUNNING(f) do { (f) &= ~IFF_RUNNING; } while (0)
#define IFF_CLR_BOUND(f) do { (f) &= ~IFF_BOUND; } while (0)
#define IFF_CLR_NOARP(f) do { (f) &= ~IFF_NOARP; } while (0)
#define IFF_CLR_LOOPBACK(f) do { (f) &= ~IFF_LOOPBACK; } while (0)
#define IFF_CLR_POINTOPOINT(f) do { (f) &= ~IFF_POINTOPOINT; } while (0)
#define IFF_CLR_MULTICAST(f) do { (f) &= ~IFF_MULTICAST; } while (0)
#define IFF_CLR_BROADCAST(f) do { (f) &= ~IFF_BROADCAST; } while (0)

#define IFF_IS_DOWN(f) (((f) & IFF_DOWN) != 0)
#define IFF_IS_UP(f) (((f) & IFF_UP) != 0)
#define IFF_IS_RUNNING(f) (((f) & IFF_RUNNING) != 0)
#define IFF_IS_BOUND(f) (((f) & IFF_BOUND) != 0)
#define IFF_IS_NOARP(f) (((f) & IFF_NOARP) != 0)
#define IFF_IS_LOOPBACK(f) (((f) & IFF_LOOPBACK) != 0)
#define IFF_IS_POINTOPOINT(f) (((f) & IFF_POINTOPOINT) != 0)
#define IFF_IS_MULTICAST(f) (((f) & IFF_MULTICAST) != 0)
#define IFF_IS_BROADCAST(f) (((f) & IFF_BROADCAST) != 0)

/* We only need to manage the IPv6 bit if both IPv6 and IPv4 are supported.
* Otherwise, we can save a few bytes by ignoring it.
Expand Down Expand Up @@ -184,7 +200,7 @@ struct lifreq
struct sockaddr lifru_hwaddr; /* MAC address */
int lifru_count; /* Number of devices */
int lifru_mtu; /* MTU size */
uint8_t lifru_flags; /* Interface flags */
uint32_t lifru_flags; /* Interface flags */
struct mii_ioctl_notify_s llfru_mii_notify; /* PHY event notification */
struct mii_ioctl_data_s lifru_mii_data; /* MII request data */
struct can_ioctl_data_s lifru_can_data; /* CAN bitrate request data */
Expand Down Expand Up @@ -237,7 +253,7 @@ struct ifreq
struct sockaddr ifru_hwaddr; /* MAC address */
int ifru_count; /* Number of devices */
int ifru_mtu; /* MTU size */
uint8_t ifru_flags; /* Interface flags */
uint32_t ifru_flags; /* Interface flags */
struct mii_ioctl_notify_s ifru_mii_notify; /* PHY event notification */
struct mii_ioctl_data_s ifru_mii_data; /* MII request data */
struct can_ioctl_data_s ifru_can_data; /* CAN bitrate request data */
Expand Down
2 changes: 1 addition & 1 deletion include/nuttx/net/netdev.h
Expand Up @@ -242,7 +242,7 @@ struct net_driver_s

/* Drivers interface flags. See IFF_* definitions in include/net/if.h */

uint8_t d_flags;
uint32_t d_flags;

/* Multi network devices using multiple link layer protocols are
* supported
Expand Down
12 changes: 12 additions & 0 deletions net/netdev/netdev_register.c
Expand Up @@ -243,6 +243,7 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
FAR struct net_driver_s **last;
FAR char devfmt_str[IFNAMSIZ];
FAR const char *devfmt;
uint32_t flags = 0;
uint16_t pktsize = 0;
uint8_t llhdrlen = 0;
int devnum;
Expand All @@ -263,6 +264,7 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
llhdrlen = 0;
pktsize = NET_LO_PKTSIZE;
devfmt = NETDEV_LO_FORMAT;
flags = IFF_LOOPBACK;
break;
#endif

Expand All @@ -271,6 +273,7 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
llhdrlen = ETH_HDRLEN;
pktsize = CONFIG_NET_ETH_PKTSIZE;
devfmt = NETDEV_ETH_FORMAT;
flags = IFF_BROADCAST | IFF_MULTICAST;
break;
#endif

Expand All @@ -279,6 +282,7 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
llhdrlen = ETH_HDRLEN;
pktsize = CONFIG_NET_ETH_PKTSIZE;
devfmt = NETDEV_WLAN_FORMAT;
flags = IFF_BROADCAST | IFF_MULTICAST;
break;
#endif

Expand All @@ -287,6 +291,7 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
dev->d_llhdrlen = 0;
dev->d_pktsize = NET_CAN_PKTSIZE;
devfmt = NETDEV_CAN_FORMAT;
flags = IFF_NOARP;
break;
#endif

Expand All @@ -297,6 +302,7 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
pktsize = CONFIG_NET_6LOWPAN_PKTSIZE;
#endif
devfmt = NETDEV_BNEP_FORMAT;
flags = IFF_BROADCAST | IFF_MULTICAST;
break;
#endif

Expand All @@ -308,6 +314,7 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
pktsize = CONFIG_NET_6LOWPAN_PKTSIZE;
#endif
devfmt = NETDEV_WPAN_FORMAT;
flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
break;
#endif

Expand All @@ -316,6 +323,7 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
llhdrlen = 0;
pktsize = CONFIG_NET_SLIP_PKTSIZE;
devfmt = NETDEV_SLIP_FORMAT;
flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
break;
#endif

Expand All @@ -325,6 +333,7 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
* if used as a TAP (layer 2) device */
pktsize = CONFIG_NET_TUN_PKTSIZE;
devfmt = NETDEV_TUN_FORMAT;
flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
break;
#endif

Expand All @@ -333,6 +342,7 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
llhdrlen = 0;
pktsize = 1200;
devfmt = NETDEV_WWAN_FORMAT;
flags = IFF_BROADCAST | IFF_NOARP | IFF_MULTICAST;
break;
#endif

Expand All @@ -357,6 +367,8 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
dev->d_pktsize = pktsize;
}

dev->d_flags |= flags;

/* Remember the verified link type */

dev->d_lltype = (uint8_t)lltype;
Expand Down

0 comments on commit acff87c

Please sign in to comment.