Skip to content

Commit ea89f1f

Browse files
committed
[nrf fromtree] net: vlan: Fix net_eth_get_vlan_tag() to check correct interface
The network interface parameter for net_eth_get_vlan_tag() should be the VLAN interface so use the search loop properly. Earlier the main interface could be checked. Add also test cases for this so that we can catch that the func works properly. Signed-off-by: Jukka Rissanen <[email protected]> (cherry picked from commit d40abe8) Signed-off-by: Robert Lubos <[email protected]>
1 parent 8e18208 commit ea89f1f

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

subsys/net/l2/ethernet/vlan.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,17 @@ bool net_eth_is_vlan_enabled(struct ethernet_context *ctx,
317317
uint16_t net_eth_get_vlan_tag(struct net_if *iface)
318318
{
319319
uint16_t tag = NET_VLAN_TAG_UNSPEC;
320-
struct vlan_context *ctx;
321320

322321
k_mutex_lock(&lock, K_FOREVER);
323322

324-
ctx = get_vlan_ctx(iface, tag, true);
325-
if (ctx != NULL) {
326-
/* The Ethernet interface does not have a tag so if user
327-
* tried to use the main interface, then do not return
328-
* the tag.
329-
*/
330-
if (ctx->attached_to != iface) {
331-
tag = ctx->tag;
323+
ARRAY_FOR_EACH(vlan_ctx, i) {
324+
if (vlan_ctx[i] == NULL || !vlan_ctx[i]->is_used) {
325+
continue;
326+
}
327+
328+
if (vlan_ctx[i]->iface == iface) {
329+
tag = vlan_ctx[i]->tag;
330+
break;
332331
}
333332
}
334333

tests/net/vlan/src/main.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,23 @@ static void test_vlan_enable(void)
574574
ret = net_eth_vlan_enable(iface, VLAN_TAG_1);
575575
zassert_equal(ret, -EALREADY, "VLAN tag %d enabled for iface 1 (%d)",
576576
VLAN_TAG_1, ret);
577+
578+
for (int i = VLAN_TAG_1; i <= VLAN_TAG_5; i += 100) {
579+
iface = net_eth_get_vlan_iface(NULL, i);
580+
581+
ARRAY_FOR_EACH_PTR(vlan_interfaces, vlan_iface) {
582+
uint16_t tag;
583+
584+
if (*vlan_iface == iface) {
585+
tag = net_eth_get_vlan_tag(*vlan_iface);
586+
587+
zassert_equal(tag, i,
588+
"Could not get the VLAN interface (%d)",
589+
net_if_get_by_iface(*vlan_iface));
590+
break;
591+
}
592+
}
593+
}
577594
}
578595

579596
static void test_vlan_disable(void)
@@ -628,13 +645,13 @@ static void test_vlan_enable_all(void)
628645
int ret;
629646

630647
ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_1);
631-
zassert_equal(ret, 0, "Cannot enable %d", VLAN_TAG_1);
648+
zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_1);
632649
ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_2);
633-
zassert_equal(ret, 0, "Cannot enable %d", VLAN_TAG_2);
650+
zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_2);
634651
ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_3);
635-
zassert_equal(ret, 0, "Cannot enable %d", VLAN_TAG_3);
652+
zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_3);
636653
ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_4);
637-
zassert_equal(ret, 0, "Cannot enable %d", VLAN_TAG_4);
654+
zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_4);
638655

639656
eth_ctx = net_if_l2_data(eth_interfaces[0]);
640657

0 commit comments

Comments
 (0)