|
| 1 | +/* main.c - Application main entry point */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2019 Intel Corporation |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +#define NET_LOG_LEVEL CONFIG_NET_HOSTNAME_LOG_LEVEL |
| 10 | + |
| 11 | +#include <logging/log.h> |
| 12 | +LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL); |
| 13 | + |
| 14 | +#include <zephyr/types.h> |
| 15 | +#include <stdbool.h> |
| 16 | +#include <stddef.h> |
| 17 | +#include <string.h> |
| 18 | +#include <errno.h> |
| 19 | +#include <sys/printk.h> |
| 20 | +#include <linker/sections.h> |
| 21 | + |
| 22 | +#include <ztest.h> |
| 23 | + |
| 24 | +#include <net/ethernet.h> |
| 25 | +#include <net/buf.h> |
| 26 | +#include <net/net_ip.h> |
| 27 | +#include <net/net_if.h> |
| 28 | + |
| 29 | +#define NET_LOG_ENABLED 1 |
| 30 | +#include "net_private.h" |
| 31 | + |
| 32 | +#if defined(CONFIG_NET_IF_LOG_LEVEL_DBG) |
| 33 | +#define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__) |
| 34 | +#else |
| 35 | +#define DBG(fmt, ...) |
| 36 | +#endif |
| 37 | + |
| 38 | +/* Interface 1 addresses */ |
| 39 | +static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0, |
| 40 | + 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; |
| 41 | +static struct in_addr my_ipv4_addr1 = { { { 192, 0, 2, 1 } } }; |
| 42 | + |
| 43 | +/* Extra address is assigned to ll_addr */ |
| 44 | +static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0, |
| 45 | + 0, 0, 0, 0xf2, 0xaa, 0x29, 0x02, |
| 46 | + 0x04 } } }; |
| 47 | + |
| 48 | +static struct in6_addr in6addr_mcast = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, |
| 49 | + 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; |
| 50 | + |
| 51 | +static struct net_if *iface1; |
| 52 | + |
| 53 | +static bool test_started; |
| 54 | +static struct k_sem wait_data; |
| 55 | + |
| 56 | +#define WAIT_TIME 250 |
| 57 | + |
| 58 | +struct net_if_test { |
| 59 | + u8_t idx; |
| 60 | + u8_t mac_addr[sizeof(struct net_eth_addr)]; |
| 61 | + struct net_linkaddr ll_addr; |
| 62 | +}; |
| 63 | + |
| 64 | +static int net_iface_dev_init(struct device *dev) |
| 65 | +{ |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +static u8_t *net_iface_get_mac(struct device *dev) |
| 70 | +{ |
| 71 | + struct net_if_test *data = dev->driver_data; |
| 72 | + |
| 73 | + if (data->mac_addr[2] == 0x00) { |
| 74 | + /* 00-00-5E-00-53-xx Documentation RFC 7042 */ |
| 75 | + data->mac_addr[0] = 0x00; |
| 76 | + data->mac_addr[1] = 0x00; |
| 77 | + data->mac_addr[2] = 0x5E; |
| 78 | + data->mac_addr[3] = 0x00; |
| 79 | + data->mac_addr[4] = 0x53; |
| 80 | + data->mac_addr[5] = sys_rand32_get(); |
| 81 | + } |
| 82 | + |
| 83 | + data->ll_addr.addr = data->mac_addr; |
| 84 | + data->ll_addr.len = 6U; |
| 85 | + |
| 86 | + return data->mac_addr; |
| 87 | +} |
| 88 | + |
| 89 | +static void net_iface_init(struct net_if *iface) |
| 90 | +{ |
| 91 | + u8_t *mac = net_iface_get_mac(net_if_get_device(iface)); |
| 92 | + |
| 93 | + net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr), |
| 94 | + NET_LINK_ETHERNET); |
| 95 | +} |
| 96 | + |
| 97 | +static int sender_iface(struct device *dev, struct net_pkt *pkt) |
| 98 | +{ |
| 99 | + if (!pkt->buffer) { |
| 100 | + DBG("No data to send!\n"); |
| 101 | + return -ENODATA; |
| 102 | + } |
| 103 | + |
| 104 | + k_sem_give(&wait_data); |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +struct net_if_test net_iface1_data; |
| 110 | + |
| 111 | +static struct ethernet_api net_iface_api = { |
| 112 | + .iface_api.init = net_iface_init, |
| 113 | + .send = sender_iface, |
| 114 | +}; |
| 115 | + |
| 116 | +#define _ETH_L2_LAYER ETHERNET_L2 |
| 117 | +#define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(ETHERNET_L2) |
| 118 | + |
| 119 | +NET_DEVICE_INIT_INSTANCE(net_iface1_test, |
| 120 | + "iface1", |
| 121 | + iface1, |
| 122 | + net_iface_dev_init, |
| 123 | + &net_iface1_data, |
| 124 | + NULL, |
| 125 | + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, |
| 126 | + &net_iface_api, |
| 127 | + _ETH_L2_LAYER, |
| 128 | + _ETH_L2_CTX_TYPE, |
| 129 | + 127); |
| 130 | + |
| 131 | +struct eth_fake_context { |
| 132 | + struct net_if *iface; |
| 133 | + u8_t mac_address[6]; |
| 134 | + bool promisc_mode; |
| 135 | +}; |
| 136 | + |
| 137 | +static struct eth_fake_context eth_fake_data; |
| 138 | + |
| 139 | +static void eth_fake_iface_init(struct net_if *iface) |
| 140 | +{ |
| 141 | + struct device *dev = net_if_get_device(iface); |
| 142 | + struct eth_fake_context *ctx = dev->driver_data; |
| 143 | + |
| 144 | + ctx->iface = iface; |
| 145 | + |
| 146 | + net_if_set_link_addr(iface, ctx->mac_address, |
| 147 | + sizeof(ctx->mac_address), |
| 148 | + NET_LINK_ETHERNET); |
| 149 | + |
| 150 | + ethernet_init(iface); |
| 151 | +} |
| 152 | + |
| 153 | +static int eth_fake_send(struct device *dev, |
| 154 | + struct net_pkt *pkt) |
| 155 | +{ |
| 156 | + ARG_UNUSED(dev); |
| 157 | + ARG_UNUSED(pkt); |
| 158 | + |
| 159 | + return 0; |
| 160 | +} |
| 161 | + |
| 162 | +static struct ethernet_api eth_fake_api_funcs = { |
| 163 | + .iface_api.init = eth_fake_iface_init, |
| 164 | + .send = eth_fake_send, |
| 165 | +}; |
| 166 | + |
| 167 | +static int eth_fake_init(struct device *dev) |
| 168 | +{ |
| 169 | + struct eth_fake_context *ctx = dev->driver_data; |
| 170 | + |
| 171 | + ctx->promisc_mode = false; |
| 172 | + |
| 173 | + return 0; |
| 174 | +} |
| 175 | + |
| 176 | +ETH_NET_DEVICE_INIT(eth_fake, "eth_fake", eth_fake_init, ð_fake_data, |
| 177 | + NULL, CONFIG_ETH_INIT_PRIORITY, ð_fake_api_funcs, |
| 178 | + NET_ETH_MTU); |
| 179 | + |
| 180 | +#if NET_LOG_LEVEL >= LOG_LEVEL_DBG |
| 181 | +static const char *iface2str(struct net_if *iface) |
| 182 | +{ |
| 183 | + if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) { |
| 184 | + return "Ethernet"; |
| 185 | + } |
| 186 | + |
| 187 | + return "<unknown type>"; |
| 188 | +} |
| 189 | +#endif |
| 190 | + |
| 191 | +static void iface_cb(struct net_if *iface, void *user_data) |
| 192 | +{ |
| 193 | + static int if_count; |
| 194 | + |
| 195 | + DBG("Interface %p (%s) [%d]\n", iface, iface2str(iface), |
| 196 | + net_if_get_by_iface(iface)); |
| 197 | + |
| 198 | + if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) { |
| 199 | + switch (if_count) { |
| 200 | + case 0: |
| 201 | + iface1 = iface; |
| 202 | + break; |
| 203 | + } |
| 204 | + |
| 205 | + if_count++; |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +static void iface_setup(void) |
| 210 | +{ |
| 211 | + struct net_if_mcast_addr *maddr; |
| 212 | + struct net_if_addr *ifaddr; |
| 213 | + int idx; |
| 214 | + |
| 215 | + /* The semaphore is there to wait the data to be received. */ |
| 216 | + k_sem_init(&wait_data, 0, UINT_MAX); |
| 217 | + |
| 218 | + net_if_foreach(iface_cb, NULL); |
| 219 | + |
| 220 | + idx = net_if_get_by_iface(iface1); |
| 221 | + ((struct net_if_test *) |
| 222 | + net_if_get_device(iface1)->driver_data)->idx = idx; |
| 223 | + |
| 224 | + DBG("Interfaces: [%d] iface1 %p\n", |
| 225 | + net_if_get_by_iface(iface1), iface1); |
| 226 | + |
| 227 | + zassert_not_null(iface1, "Interface 1"); |
| 228 | + |
| 229 | + ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1, |
| 230 | + NET_ADDR_MANUAL, 0); |
| 231 | + if (!ifaddr) { |
| 232 | + DBG("Cannot add IPv6 address %s\n", |
| 233 | + net_sprint_ipv6_addr(&my_addr1)); |
| 234 | + zassert_not_null(ifaddr, "addr1"); |
| 235 | + } |
| 236 | + |
| 237 | + ifaddr = net_if_ipv4_addr_add(iface1, &my_ipv4_addr1, |
| 238 | + NET_ADDR_MANUAL, 0); |
| 239 | + if (!ifaddr) { |
| 240 | + DBG("Cannot add IPv4 address %s\n", |
| 241 | + net_sprint_ipv4_addr(&my_ipv4_addr1)); |
| 242 | + zassert_not_null(ifaddr, "ipv4 addr1"); |
| 243 | + } |
| 244 | + |
| 245 | + /* For testing purposes we need to set the adddresses preferred */ |
| 246 | + ifaddr->addr_state = NET_ADDR_PREFERRED; |
| 247 | + |
| 248 | + ifaddr = net_if_ipv6_addr_add(iface1, &ll_addr, |
| 249 | + NET_ADDR_MANUAL, 0); |
| 250 | + if (!ifaddr) { |
| 251 | + DBG("Cannot add IPv6 address %s\n", |
| 252 | + net_sprint_ipv6_addr(&ll_addr)); |
| 253 | + zassert_not_null(ifaddr, "ll_addr"); |
| 254 | + } |
| 255 | + |
| 256 | + ifaddr->addr_state = NET_ADDR_PREFERRED; |
| 257 | + |
| 258 | + net_ipv6_addr_create(&in6addr_mcast, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001); |
| 259 | + |
| 260 | + maddr = net_if_ipv6_maddr_add(iface1, &in6addr_mcast); |
| 261 | + if (!maddr) { |
| 262 | + DBG("Cannot add multicast IPv6 address %s\n", |
| 263 | + net_sprint_ipv6_addr(&in6addr_mcast)); |
| 264 | + zassert_not_null(maddr, "mcast"); |
| 265 | + } |
| 266 | + |
| 267 | + net_if_up(iface1); |
| 268 | + |
| 269 | + test_started = true; |
| 270 | +} |
| 271 | + |
| 272 | +static void hostname_get(void) |
| 273 | +{ |
| 274 | + const char *hostname; |
| 275 | + const char *config_hostname = CONFIG_NET_HOSTNAME; |
| 276 | + |
| 277 | + hostname = net_hostname_get(); |
| 278 | + |
| 279 | + zassert_mem_equal(hostname, config_hostname, |
| 280 | + sizeof(CONFIG_NET_HOSTNAME) - 1, ""); |
| 281 | + |
| 282 | + if (IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE)) { |
| 283 | + char mac[8]; |
| 284 | + int ret; |
| 285 | + |
| 286 | + ret = net_bytes_from_str(mac, sizeof(mac), |
| 287 | + hostname + sizeof(CONFIG_NET_HOSTNAME) - 1); |
| 288 | + zassert_equal(ret, 0, ""); |
| 289 | + |
| 290 | + zassert_mem_equal(mac, net_if_get_link_addr(iface1)->addr, |
| 291 | + net_if_get_link_addr(iface1)->len, ""); |
| 292 | + } |
| 293 | +} |
| 294 | + |
| 295 | +static void hostname_set(void) |
| 296 | +{ |
| 297 | + if (IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE)) { |
| 298 | + int ret; |
| 299 | + |
| 300 | + ret = net_hostname_set_postfix("foobar", sizeof("foobar") - 1); |
| 301 | + zassert_equal(ret, -EALREADY, |
| 302 | + "Could set hostname postfix (%d)", ret); |
| 303 | + } |
| 304 | +} |
| 305 | + |
| 306 | +void test_main(void) |
| 307 | +{ |
| 308 | + ztest_test_suite(net_hostname_test, |
| 309 | + ztest_unit_test(iface_setup), |
| 310 | + ztest_unit_test(hostname_get), |
| 311 | + ztest_unit_test(hostname_set) |
| 312 | + ); |
| 313 | + |
| 314 | + ztest_run_test_suite(net_hostname_test); |
| 315 | +} |
0 commit comments