@@ -52,12 +52,46 @@ LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);
52
52
#define VLAN_TAG_4 400
53
53
#define VLAN_TAG_5 500
54
54
#define VLAN_TAG_6 600
55
+ #define VLAN_TAG_7 700
55
56
56
57
#define NET_ETH_MAX_COUNT 2
57
58
58
59
#define MY_IPV6_ADDR "2001:db8:200::2"
59
60
#define MY_IPV6_ADDR_SRV "2001:db8:200::1"
60
61
62
+ /* ICMPv6 Echo Request from 2001:db8::2 to 2001:db8::1,
63
+ * src mac 00:00:5e:00:53:ff dst 02:00:5e:00:53:31
64
+ * VLAN tag 0, priority 0
65
+ */
66
+ static unsigned char icmpv6_echo_request [] = {
67
+ /* 0000 */ 0x02 , 0x00 , 0x5e , 0x00 , 0x53 , 0x31 , 0x00 , 0x00 ,
68
+ 0x5e , 0x00 , 0x53 , 0xff , 0x81 , 0x00 , 0x00 , 0x00 ,
69
+ /* 0010 */ 0x86 , 0xdd , 0x60 , 0x00 , 0x00 , 0x00 , 0x00 , 0x08 ,
70
+ 0x3a , 0x40 , 0x20 , 0x01 , 0x0d , 0xb8 , 0x00 , 0x00 ,
71
+ /* 0020 */ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
72
+ 0x00 , 0x02 , 0x20 , 0x01 , 0x0d , 0xb8 , 0x00 , 0x00 ,
73
+ /* 0030 */ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
74
+ 0x00 , 0x01 , 0x80 , 0x00 , 0x24 , 0x48 , 0x00 , 0x00 ,
75
+ /* 0040 */ 0x00 , 0x00 ,
76
+ };
77
+
78
+ /* ICMPv6 Echo Reply from 2001:db8::1 to 2001:db8::2,
79
+ * src mac 02:00:5e:00:53:31 dst 00:00:5e:00:53:08
80
+ * No VLAN tag.
81
+ */
82
+ static unsigned char icmpv6_echo_reply [] = {
83
+ /* 1st fragment with Ethernet header */
84
+ 0x00 , 0x00 , 0x5e , 0x00 , 0x53 , 0x08 , 0x00 , 0x00 ,
85
+ 0x5e , 0x00 , 0x53 , 0x08 , 0x86 , 0xdd ,
86
+ /* 2nd fragment, with IPv6 header and ICMPv6 Echo Reply */
87
+ 0x60 , 0x00 , 0x00 , 0x00 , 0x00 , 0x08 , 0x3a , 0x40 ,
88
+ 0x20 , 0x01 , 0x0d , 0xb8 , 0x00 , 0x00 , 0x00 , 0x00 ,
89
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 ,
90
+ 0x20 , 0x01 , 0x0d , 0xb8 , 0x00 , 0x00 , 0x00 , 0x00 ,
91
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x02 ,
92
+ 0x81 , 0x00 , 0x23 , 0x48 , 0x00 , 0x00 , 0x00 , 0x00 ,
93
+ };
94
+
61
95
/* Interface 1 addresses */
62
96
static struct in6_addr my_addr1 = { { { 0x20 , 0x01 , 0x0d , 0xb8 , 1 , 0 , 0 , 0 ,
63
97
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x1 } } };
@@ -75,14 +109,20 @@ static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
75
109
0 , 0 , 0 , 0xf2 , 0xaa , 0x29 , 0x02 ,
76
110
0x04 } } };
77
111
112
+ /* Peer addresses */
113
+ static struct in6_addr peer_addr = { { { 0x20 , 0x01 , 0x0d , 0xb8 , 0 , 0 , 0 , 0 ,
114
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x2 } } };
115
+
78
116
/* Keep track of all ethernet interfaces */
79
117
static struct net_if * eth_interfaces [NET_ETH_MAX_COUNT ];
80
118
static struct net_if * vlan_interfaces [NET_VLAN_MAX_COUNT ];
81
119
static struct net_if * dummy_interfaces [2 ];
82
120
static struct net_if * embed_ll_interface ;
121
+ static struct net_if * test_iface ;
83
122
84
123
static bool test_failed ;
85
124
static bool test_started ;
125
+ static bool expecting_vlan_tag_0 ;
86
126
87
127
static K_SEM_DEFINE (wait_data , 0 , UINT_MAX ) ;
88
128
@@ -127,6 +167,26 @@ static int eth_tx(const struct device *dev, struct net_pkt *pkt)
127
167
struct eth_context * context = dev -> data ;
128
168
int ret ;
129
169
170
+ if (expecting_vlan_tag_0 ) {
171
+ uint8_t reply [sizeof (icmpv6_echo_reply ) - sizeof (struct net_eth_hdr )];
172
+ uint16_t ptype ;
173
+
174
+ expecting_vlan_tag_0 = false;
175
+ test_iface = net_pkt_iface (pkt );
176
+ net_pkt_cursor_init (pkt );
177
+ net_pkt_set_overwrite (pkt , true);
178
+ net_pkt_skip (pkt , sizeof (struct net_eth_hdr ) - 2 );
179
+ net_pkt_read_be16 (pkt , & ptype );
180
+ zassert_equal (ptype , NET_ETH_PTYPE_IPV6 , "Invalid ptype 0x%04x" , ptype );
181
+ net_pkt_read (pkt , reply , sizeof (reply ));
182
+ zassert_mem_equal (reply , icmpv6_echo_reply + sizeof (struct net_eth_hdr ),
183
+ sizeof (reply ) - sizeof (struct net_eth_hdr ),
184
+ "Invalid ICMPv6 Echo Reply" );
185
+
186
+ k_sem_give (& wait_data );
187
+ return 0 ;
188
+ }
189
+
130
190
if (!IS_ENABLED (CONFIG_NET_L2_ETHERNET_RESERVE_HEADER )) {
131
191
/* There should be at least two net_buf. The first one should contain
132
192
* the link layer header.
@@ -975,4 +1035,88 @@ ZTEST(net_vlan, test_vlan_enable_disable_all)
975
1035
test_vlan_disable_all ();
976
1036
}
977
1037
1038
+ static bool add_peer_neighbor (struct net_if * iface , struct in6_addr * addr ,
1039
+ uint8_t * lladdr )
1040
+ {
1041
+ struct net_linkaddr ll_addr = {
1042
+ .addr = lladdr ,
1043
+ .len = 6 ,
1044
+ .type = NET_LINK_ETHERNET
1045
+ };
1046
+ struct net_nbr * nbr ;
1047
+
1048
+ nbr = net_ipv6_nbr_add (iface , addr , & ll_addr , false,
1049
+ NET_IPV6_NBR_STATE_REACHABLE );
1050
+ if (!nbr ) {
1051
+ DBG ("Cannot add dst %s to neighbor cache\n" ,
1052
+ net_sprint_ipv6_addr (addr ));
1053
+ return false;
1054
+ }
1055
+
1056
+ DBG ("Adding dst %s as [%s] to nbr cache\n" ,
1057
+ net_sprint_ipv6_addr (addr ),
1058
+ net_sprint_ll_addr (ll_addr .addr , 6 ));
1059
+
1060
+ return true;
1061
+ }
1062
+
1063
+ ZTEST (net_vlan , test_vlan_tag_0 )
1064
+ {
1065
+ struct eth_context * context ;
1066
+ const struct device * dev ;
1067
+ struct net_if * iface ;
1068
+ struct net_pkt * pkt ;
1069
+ int ret ;
1070
+
1071
+ iface = vlan_interfaces [0 ];
1072
+
1073
+ dev = net_if_get_device (eth_interfaces [0 ]);
1074
+ context = dev -> data ;
1075
+
1076
+ /* Set the receiving mac address of the example packet */
1077
+ memcpy (& icmpv6_echo_request [0 ], context -> mac_addr , 6 );
1078
+
1079
+ net_if_down (eth_interfaces [0 ]);
1080
+ net_if_down (vlan_interfaces [0 ]);
1081
+
1082
+ /* Setup the interfaces */
1083
+ ret = net_eth_vlan_enable (eth_interfaces [0 ], VLAN_TAG_7 );
1084
+ zassert_equal (ret , 0 , "Cannot enable %d (%d)" , VLAN_TAG_7 , ret );
1085
+
1086
+ net_if_up (eth_interfaces [0 ]);
1087
+ net_if_up (vlan_interfaces [0 ]);
1088
+
1089
+ ret = add_peer_neighbor (iface , & peer_addr , & icmpv6_echo_request [0 ]);
1090
+ zassert_true (ret , "Cannot add neighbor" );
1091
+
1092
+ /* Create ICMPv6 echo request packet, then send it to the Ethernet
1093
+ * interface. Expect the ICMPv6 echo response to be sent back to the
1094
+ * same interface.
1095
+ */
1096
+ pkt = net_pkt_rx_alloc_with_buffer (iface ,
1097
+ sizeof (icmpv6_echo_request ),
1098
+ AF_INET6 ,
1099
+ IPPROTO_ICMPV6 ,
1100
+ K_NO_WAIT );
1101
+ zassert_not_null (pkt , "Cannot allocate pkt" );
1102
+
1103
+ ret = net_pkt_write (pkt , icmpv6_echo_request , sizeof (icmpv6_echo_request ));
1104
+ zassert_equal (ret , 0 , "Cannot write to pkt" );
1105
+
1106
+ expecting_vlan_tag_0 = true;
1107
+ test_iface = eth_interfaces [0 ];
1108
+
1109
+ /* Make sure that the reply packet is sent to the correct interface */
1110
+ ret = net_recv_data (eth_interfaces [0 ], pkt );
1111
+ zassert_false (ret < 0 , "Cannot receive data (%d)" , ret );
1112
+
1113
+ if (k_sem_take (& wait_data , WAIT_TIME )) {
1114
+ DBG ("Timeout while waiting interface data\n" );
1115
+ zassert_false (true, "Timeout" );
1116
+ }
1117
+
1118
+ zassert_false (expecting_vlan_tag_0 , "VLAN tag 0 not received" );
1119
+ zassert_equal (test_iface , eth_interfaces [0 ], "Wrong interface" );
1120
+ }
1121
+
978
1122
ZTEST_SUITE (net_vlan , NULL , setup , NULL , NULL , NULL );
0 commit comments