Skip to content

Conversation

@zhhyu7
Copy link
Contributor

@zhhyu7 zhhyu7 commented Dec 8, 2025

Summary

Inspired by Linux's way, we also create VLAN devices for managing VLAN,
which will become interfaces like eth0.58.

QinQ is also supported, we can create VLAN devices above another VLAN
devices, like eth0.100.101 (or even eth0.1.2.3.4, also supported on
Linux).

Supporting ADD_VLAN_CMD and DEL_VLAN_CMD of SIOCSIFVLAN
Ref: https://github.com/torvalds/linux/blob/v6.12/net/8021q/vlan.c#L621

We add default PCP because some of our apps may not want to set PCP
manually (e.g. Our user may just ping with pre-set PCP)

The vlan_qos is used as PCP when setting Linux's priority mapping:
https://github.com/torvalds/linux/blob/v6.12/net/8021q/vlan.c#L590
Although vlan_qos is not used when creating VLAN on Linux, we can
use it as PCP on creating VLAN (without changing its meaning), and
keep compatible with Linux's creating (Exactly the same when
vlan_qos is 0).

Impact

Users can create VLAN network device of various priorities through ioctl.

Testing

sim:matter with creates the VLAN network device on the sim eth0 device.

int netlib_add_vlan(FAR const char *ifname, int vlanid, int prio)            
{                                                                            
  int ret = ERROR;                                                           
                                                                             
  if (ifname && vlanid > 0)                                                  
    {                                                                        
      int sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL);
      if (sockfd >= 0)                                                       
        {                                                                    
          struct vlan_ioctl_args ifv;                                        
                                                                             
          strlcpy(ifv.device1, ifname, sizeof(ifv.device1));                 
          ifv.u.VID    = vlanid;                                             
          ifv.vlan_qos = prio;                                               
          ifv.cmd      = ADD_VLAN_CMD;                                       
                                                                             
          ret = ioctl(sockfd, SIOCSIFVLAN, &ifv);                            
          close(sockfd);                                                     
        }                                                                    
    }                                                                        
                                                                             
  return ret;                                                                
}                                                                            

Then test the functionality and performance of the VLAN network card using tools such as ping and iperf2.

NuttX:
nsh> ifconfig
eth0	Link encap:Ethernet HWaddr 42:00:d7:82:13:6b at RUNNING mtu 1500
	inet addr:10.0.1.2 DRaddr:10.0.0.1 Mask:255.255.255.0
	inet6 addr: fe80::4000:d7ff:fe82:136b/64
	inet6 DRaddr: ::

lo	Link encap:Local Loopback at RUNNING mtu 1518
	inet addr:127.0.0.1 DRaddr:127.0.0.1 Mask:255.0.0.0
	inet6 addr: ::1/128
	inet6 DRaddr: ::1

nsh> vconfig -h
nsh: vconfig: missing required argument(s)
nsh> help vconfig
vconfig usage:  vconfig [add iface-name vlan-id [pcp]]|[rem vlan-name]
nsh> vconfig add eth0 2
nsh> ifconfig
eth0	Link encap:Ethernet HWaddr 42:00:d7:82:13:6b at RUNNING mtu 1500
	inet addr:10.0.1.2 DRaddr:10.0.0.1 Mask:255.255.255.0
	inet6 addr: fe80::4000:d7ff:fe82:136b/64
	inet6 DRaddr: ::

lo	Link encap:Local Loopback at RUNNING mtu 1518
	inet addr:127.0.0.1 DRaddr:127.0.0.1 Mask:255.0.0.0
	inet6 addr: ::1/128
	inet6 DRaddr: ::1

eth0.2	Link encap:Ethernet HWaddr 42:00:d7:82:13:6b at DOWN mtu 1504
	inet addr:0.0.0.0 DRaddr:0.0.0.0 Mask:0.0.0.0
	inet6 addr: ::/0
	inet6 DRaddr: ::

nsh> ifconfig eth0.2 10.0.2.2/24
nsh> ifup eth0.2
ifup eth0.2...OK
nsh> ifconfig
eth0	Link encap:Ethernet HWaddr 42:00:d7:82:13:6b at RUNNING mtu 1500
	inet addr:10.0.1.2 DRaddr:10.0.0.1 Mask:255.255.255.0
	inet6 addr: fe80::4000:d7ff:fe82:136b/64
	inet6 DRaddr: ::

lo	Link encap:Local Loopback at RUNNING mtu 1518
	inet addr:127.0.0.1 DRaddr:127.0.0.1 Mask:255.0.0.0
	inet6 addr: ::1/128
	inet6 DRaddr: ::1

eth0.2	Link encap:Ethernet HWaddr 42:00:d7:82:13:6b at RUNNING mtu 1504
	inet addr:10.0.2.2 DRaddr:10.0.2.1 Mask:255.255.255.0
	inet6 addr: ::/0
	inet6 DRaddr: ::

nsh> ping -c 3 10.0.2.1
PING 10.0.2.1 56 bytes of data
ERROR: sendto failed at seqno 0: 101
ERROR: sendto failed at seqno 1: 101
ERROR: sendto failed at seqno 2: 101
3 packets transmitted, 0 received, 100% packet loss, time 3030 ms
Linux:
sudo vconfig add nuttx0 2
sudo ifconfig nuttx0.2 10.0.2.1/24 up
ifconfig nuttx0.2
nuttx0.2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.1  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::a096:6ff:fe65:382a  prefixlen 64  scopeid 0x20<link>
        ether a2:96:06:65:38:2a  txqueuelen 1000  (以太网)
        RX packets 351108  bytes 493407693 (493.4 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 175443  bytes 9482376 (9.4 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

NuttX:
nsh> ping -c 3 10.0.2.1
PING 10.0.2.1 56 bytes of data
56 bytes from 10.0.2.1: icmp_seq=0 time=0.0 ms
56 bytes from 10.0.2.1: icmp_seq=1 time=0.0 ms
56 bytes from 10.0.2.1: icmp_seq=2 time=0.0 ms
3 packets transmitted, 3 received, 0% packet loss, time 3030 ms
rtt min/avg/max/mdev = 0.000/0.000/0.000/0.000 ms
nsh> ipe
  iperf
  iperf2
nsh> iperf2 -c 10.0.2.1 -i 1
------------------------------------------------------------
Client connecting to 10.0.2.1, TCP port 5001
TCP window size: 16.0 KByte
------------------------------------------------------------
[  1] local 10.0.2.2 port 17205 connected with 10.0.2.1 port 5001
[ ID] Interval       Transfer     Bandwidth
[  1] 0.00-1.00 sec  45.6 MBytes   382 Mbits/sec
[  1] 1.00-2.00 sec  46.5 MBytes   390 Mbits/sec
[  1] 2.00-3.00 sec  46.5 MBytes   390 Mbits/sec
[  1] 3.00-4.00 sec  45.8 MBytes   384 Mbits/sec
[  1] 4.00-5.00 sec  46.5 MBytes   390 Mbits/sec
[  1] 5.00-6.00 sec  46.2 MBytes   387 Mbits/sec
[  1] 6.00-7.00 sec  45.4 MBytes   381 Mbits/sec
[  1] 7.00-8.00 sec  44.7 MBytes   375 Mbits/sec
[  1] 8.00-9.00 sec  45.4 MBytes   381 Mbits/sec
[  1] 9.00-10.00 sec  44.6 MBytes   374 Mbits/sec
[  1] 0.00-10.02 sec   457 MBytes   383 Mbits/sec
nsh> 

@github-actions github-actions bot added Area: Networking Effects networking subsystem Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. labels Dec 8, 2025
wengzhe and others added 3 commits December 8, 2025 21:15
And fix wrong `d_len` for IOBs from `upper->txq` in TX.

Signed-off-by: Zhe Weng <[email protected]>
avoid continuing to access the released memory after the netdev_register
error occurs.

Signed-off-by: daichuan <[email protected]>
To prepare for VLAN device support, a VLAN device will share quota with
its real device.

Signed-off-by: Zhe Weng <[email protected]>
Copy link
Contributor

@acassis acassis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhhyu7 Amazing!!! Please don't forget to include the Documentation/ about VLAN here

@acassis acassis requested a review from linguini1 December 8, 2025 14:30
@linguini1
Copy link
Contributor

This seems like a really great addition to NuttX! If you could share the results of your iperf/ping tests that would be appreciated! Otherwise I agree that the only thing to add here would be additional documentation about this new feature.

xiaoxiang781216
xiaoxiang781216 previously approved these changes Dec 8, 2025
@zhhyu7
Copy link
Contributor Author

zhhyu7 commented Dec 9, 2025

This seems like a really great addition to NuttX! If you could share the results of your iperf/ping tests that would be appreciated! Otherwise I agree that the only thing to add here would be additional documentation about this new feature.

I have added complete test logs.

wengzhe and others added 6 commits December 9, 2025 11:04
Inspired by Linux's way, we also create VLAN devices for managing VLAN,
which will become interfaces like `eth0.58`.

QinQ is also supported, we can create VLAN devices above another VLAN
devices, like `eth0.100.101` (or even `eth0.1.2.3.4`, also supported on
Linux).

Signed-off-by: Zhe Weng <[email protected]>
Supporting ADD_VLAN_CMD and DEL_VLAN_CMD of SIOCSIFVLAN
Ref: https://github.com/torvalds/linux/blob/v6.12/net/8021q/vlan.c#L621

Signed-off-by: Zhe Weng <[email protected]>
1. We add default PCP because some of our apps may not want to set PCP
   manually (e.g. Our user may just ping with pre-set PCP)
2. The `vlan_qos` is used as PCP when setting Linux's priority mapping:
   https://github.com/torvalds/linux/blob/v6.12/net/8021q/vlan.c#L590
   Although `vlan_qos` is not used when creating VLAN on Linux, we can
   use it as PCP on creating VLAN (without changing its meaning), and
   keep compatible with Linux's creating (Exactly the same when
   `vlan_qos` is 0).

Signed-off-by: Zhe Weng <[email protected]>
Release iob when dropping ethernet packets

Signed-off-by: gaohedong <[email protected]>
avoid illegal address access caused by removing vlan
network cards during the traversal of vlan network cards

Signed-off-by: zhanghongyu <[email protected]>
add log output for error flow

Signed-off-by: zhangkai25 <[email protected]>
@zhhyu7
Copy link
Contributor Author

zhhyu7 commented Dec 9, 2025

@zhhyu7 Amazing!!! Please don't forget to include the Documentation/ about VLAN here

A brief document description has been added.

@github-actions github-actions bot added the Area: Documentation Improvements or additions to documentation label Dec 9, 2025
@acassis acassis merged commit ad42268 into apache:master Dec 9, 2025
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Area: Networking Effects networking subsystem Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants