Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | /* main.c - Application main entry point */ /* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <logging/log.h> LOG_MODULE_REGISTER(net_test, CONFIG_NET_IPV6_NBR_CACHE_LOG_LEVEL); #include <ztest.h> #include <zephyr/types.h> #include <stddef.h> #include <string.h> #include <stdio.h> #include <errno.h> #include <device.h> #include <init.h> #include <sys/printk.h> #include <net/net_core.h> #include <net/net_ip.h> #include <net/ethernet.h> #include <linker/sections.h> #include "nbr.h" #define NET_LOG_ENABLED 1 #include "net_private.h" static int remove_count, add_count, clear_called; static void net_neighbor_data_remove(struct net_nbr *nbr) { printk("Neighbor %p removed\n", nbr); remove_count++; } static void net_neighbor_table_clear(struct net_nbr_table *table) { printk("Neighbor table %p cleared\n", table); clear_called = true; } NET_NBR_POOL_INIT(net_test_neighbor_pool, CONFIG_NET_IPV6_MAX_NEIGHBORS, 0, net_neighbor_data_remove, 0); NET_NBR_TABLE_INIT(NET_NBR_LOCAL, test_neighbor, net_test_neighbor_pool, net_neighbor_table_clear); static struct net_eth_addr hwaddr1 = { { 0x42, 0x11, 0x69, 0xde, 0xfa, 0x01 } }; static struct net_eth_addr hwaddr2 = { { 0x5f, 0x1c, 0x04, 0xae, 0x99, 0x02 } }; static struct net_eth_addr hwaddr3 = { { 0xee, 0xe1, 0x55, 0xfe, 0x44, 0x03 } }; static struct net_eth_addr hwaddr4 = { { 0x61, 0xf2, 0xfe, 0x4e, 0x8e, 0x04 } }; static struct net_eth_addr hwaddr5 = { { 0x8a, 0x52, 0x01, 0x21, 0x11, 0x05 } }; static void test_neighbor(void) { struct net_eth_addr *addrs[] = { &hwaddr1, &hwaddr2, &hwaddr3, &hwaddr4, &hwaddr5, NULL }; struct net_nbr *nbrs[sizeof(addrs) - 1] = { 0 }; struct net_eth_addr *eth_addr; struct net_nbr *nbr; struct net_linkaddr lladdr; struct net_linkaddr_storage *lladdr_ptr; struct net_if *iface1 = INT_TO_POINTER(1); struct net_if *iface2 = INT_TO_POINTER(2); int ret, i; zassert_true(CONFIG_NET_IPV6_MAX_NEIGHBORS == (ARRAY_SIZE(addrs) - 2), "There should be exactly %d valid entries in addrs array\n", CONFIG_NET_IPV6_MAX_NEIGHBORS + 1); /* First adding a neighbor and trying to add multiple hw addresses * to it. Only the first one should succeed. */ nbr = net_nbr_get(&net_test_neighbor.table); zassert_not_null(nbr, "Cannot get neighbor from table %p\n", &net_test_neighbor.table); zassert_false(nbr->ref != 1, "Invalid ref count %d\n", nbr->ref); lladdr.len = sizeof(struct net_eth_addr); for (i = 0; i < 2; i++) { eth_addr = addrs[i]; lladdr.addr = eth_addr->addr; ret = net_nbr_link(nbr, iface1, &lladdr); /* One neighbor can have only one lladdr */ zassert_false(i == 0 && ret < 0, "Cannot add %s to nbr cache (%d)\n", net_sprint_ll_addr(lladdr.addr, lladdr.len), ret); if (!ret) { printk("Adding %s\n", net_sprint_ll_addr(eth_addr->addr, sizeof(struct net_eth_addr))); } } lladdr.addr = addrs[0]->addr; nbr = net_nbr_lookup(&net_test_neighbor.table, iface1, &lladdr); zassert_true(nbr->idx == 0, "Wrong index %d should be %d\n", nbr->idx, 0); for (i = 0; i < 2; i++) { eth_addr = addrs[i]; lladdr.addr = eth_addr->addr; ret = net_nbr_unlink(nbr, &lladdr); zassert_false(i == 0 && ret < 0, "Cannot add %s to nbr cache (%d)\n", net_sprint_ll_addr(lladdr.addr, lladdr.len), ret); if (!ret) { printk("Deleting %s\n", net_sprint_ll_addr(eth_addr->addr, sizeof(struct net_eth_addr))); } } net_nbr_unref(nbr); zassert_false(nbr->ref != 0, "nbr still referenced, ref %d\n", nbr->ref); /* Then adding multiple neighbors. */ lladdr.len = sizeof(struct net_eth_addr); for (i = 0; i < ARRAY_SIZE(addrs); i++) { nbr = net_nbr_get(&net_test_neighbor.table); if (!nbr) { if (i >= CONFIG_NET_IPV6_MAX_NEIGHBORS) { /* Ok, last entry will not fit cache */ break; } zassert_true(1, "[%d] Cannot get neighbor from table %p\n", i, &net_test_neighbor.table); } zassert_true(nbr->ref == 1, "[%d] Invalid ref count %d\n", i, nbr->ref); nbrs[i] = nbr; eth_addr = addrs[i]; lladdr.addr = eth_addr->addr; ret = net_nbr_link(nbr, iface1, &lladdr); zassert_false(ret < 0, "Cannot add %s to nbr cache (%d)\n", net_sprint_ll_addr(lladdr.addr, lladdr.len), ret); printk("Adding %s\n", net_sprint_ll_addr(eth_addr->addr, sizeof(struct net_eth_addr))); } for (i = 0; i < ARRAY_SIZE(addrs) - 2; i++) { lladdr.addr = addrs[i]->addr; nbr = net_nbr_lookup(&net_test_neighbor.table, iface1, &lladdr); zassert_false(nbr->idx != i, "Wrong index %d should be %d\n", nbr->idx, i); } for (i = 0; i < ARRAY_SIZE(addrs); i++) { eth_addr = addrs[i]; nbr = nbrs[i]; lladdr.addr = eth_addr->addr; if (!nbr || i >= CONFIG_NET_IPV6_MAX_NEIGHBORS) { break; } ret = net_nbr_unlink(nbr, &lladdr); zassert_false(ret < 0, "Cannot del %s from nbr cache (%d)\n", net_sprint_ll_addr(lladdr.addr, lladdr.len), ret); printk("Deleting %s\n", net_sprint_ll_addr(eth_addr->addr, sizeof(struct net_eth_addr))); net_nbr_unref(nbr); zassert_false(nbr->ref != 0, "nbr still referenced, ref %d\n", nbr->ref); } /* Then adding multiple neighbors but some of them in different * interface. */ lladdr.len = sizeof(struct net_eth_addr); remove_count = add_count = 0; for (i = 0; i < ARRAY_SIZE(addrs); i++) { nbr = net_nbr_get(&net_test_neighbor.table); if (!nbr) { if (i >= CONFIG_NET_IPV6_MAX_NEIGHBORS) { /* Ok, last entry will not fit cache */ break; } zassert_true(1, "[%d] Cannot get neighbor from table %p\n", i, &net_test_neighbor.table); } zassert_false(nbr->ref != 1, "[%d] Invalid ref count %d\n", i, nbr->ref); nbrs[i] = nbr; eth_addr = addrs[i]; lladdr.addr = eth_addr->addr; if (i % 2) { ret = net_nbr_link(nbr, iface1, &lladdr); } else { ret = net_nbr_link(nbr, iface2, &lladdr); } zassert_false(ret < 0, "Cannot add %s to nbr cache (%d)\n", net_sprint_ll_addr(lladdr.addr, lladdr.len), ret); printk("Adding %s iface %p\n", net_sprint_ll_addr(eth_addr->addr, sizeof(struct net_eth_addr)), nbr->iface); add_count++; } for (i = 0; i < ARRAY_SIZE(addrs) - 2; i++) { lladdr.addr = addrs[i]->addr; if (i % 2) { nbr = net_nbr_lookup(&net_test_neighbor.table, iface1, &lladdr); } else { nbr = net_nbr_lookup(&net_test_neighbor.table, iface2, &lladdr); } zassert_false(nbr->idx != i, "Wrong index %d should be %d\n", nbr->idx, i); lladdr_ptr = net_nbr_get_lladdr(i); if (memcmp(lladdr_ptr->addr, addrs[i]->addr, sizeof(struct net_eth_addr))) { zassert_true(1, "Wrong lladdr %s in index %d\n", net_sprint_ll_addr(lladdr_ptr->addr, lladdr_ptr->len), i); } } for (i = 0; i < ARRAY_SIZE(addrs); i++) { struct net_if *iface; eth_addr = addrs[i]; nbr = nbrs[i]; lladdr.addr = eth_addr->addr; if (!nbr || i >= CONFIG_NET_IPV6_MAX_NEIGHBORS) { break; } iface = nbr->iface; ret = net_nbr_unlink(nbr, &lladdr); zassert_false(ret < 0, "Cannot del %s from nbr cache (%d)\n", net_sprint_ll_addr(lladdr.addr, lladdr.len), ret); printk("Deleting %s iface %p\n", net_sprint_ll_addr(eth_addr->addr, sizeof(struct net_eth_addr)), iface); net_nbr_unref(nbr); zassert_false(nbr->ref != 0, "nbr still referenced, ref %d\n", nbr->ref); } zassert_false(add_count != remove_count, "Remove count %d does not match add count %d\n", remove_count, add_count); net_nbr_clear_table(&net_test_neighbor.table); zassert_true(clear_called, "Table clear check failed"); /* The table should be empty now */ lladdr.addr = addrs[0]->addr; nbr = net_nbr_lookup(&net_test_neighbor.table, iface1, &lladdr); zassert_is_null(nbr, "Some entries still found in nbr cache"); return; } /*test case main entry*/ void test_main(void) { k_thread_priority_set(k_current_get(), K_PRIO_COOP(7)); ztest_test_suite(neighbor, ztest_unit_test(test_neighbor)); ztest_run_test_suite(neighbor); } |