Changeset 3e6bca8 in mainline


Ignore:
Timestamp:
2021-08-08T17:30:29Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a7f7b9c3
Parents:
b4edc96
Message:

Represent Ethernet address as a number instead of an array

Carefully document the design since this breaks the principle of least
surprise. Also add unit tests.

Location:
uspace
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/inet/inet.c

    rb4edc96 r3e6bca8  
    3737#include <errno.h>
    3838#include <inet/addr.h>
     39#include <inet/eth_addr.h>
    3940#include <inet/inetcfg.h>
    4041#include <io/table.h>
     
    340341        inet_link_info_t linfo;
    341342        table_t *table = NULL;
     343        eth_addr_str_t saddr;
    342344
    343345        size_t count;
     
    368370                }
    369371
    370                 table_printf(table, "%02x:%02x:%02x:%02x:%02x:%02x\t"
    371                     "%s\t" "%zu\n",
    372                     linfo.mac_addr.b[0], linfo.mac_addr.b[1],
    373                     linfo.mac_addr.b[2], linfo.mac_addr.b[3],
    374                     linfo.mac_addr.b[4], linfo.mac_addr.b[5],
    375                     linfo.name, linfo.def_mtu);
     372                eth_addr_format(&linfo.mac_addr, &saddr);
     373                table_printf(table, "%s\t %s\t %zu\n", saddr.str, linfo.name,
     374                    linfo.def_mtu);
    376375
    377376                free(linfo.name);
  • uspace/lib/drv/generic/remote_ieee80211.c

    rb4edc96 r3e6bca8  
    11/*
     2 * Copyright (c) 2021 Jiri Svoboda
    23 * Copyright (c) 2015 Jan Kolarik
    34 * All rights reserved.
     
    8485}
    8586
    86 static bool mac_matches(uint8_t *mac1, uint8_t *mac2)
    87 {
    88         for (size_t i = 0; i < ETH_ADDR; i++) {
    89                 if (mac1[i] != mac2[i])
    90                         return false;
    91         }
    92 
    93         return true;
    94 }
    95 
     87// XXX This is wrong. Wifi should not have anything to do with IP links
    9688static sysarg_t get_link_id(uint8_t *mac)
    9789{
    9890        sysarg_t *link_list;
    9991        inet_link_info_t link_info;
     92        eth_addr_t eth_addr;
    10093        size_t count;
     94
     95        eth_addr_decode(mac, &eth_addr);
    10196
    10297        errno_t rc = inetcfg_get_link_list(&link_list, &count);
     
    109104                        return -1;
    110105
    111                 if (mac_matches(mac, link_info.mac_addr.b))
     106                if (eth_addr_compare(&eth_addr, &link_info.mac_addr) == 0)
    112107                        return link_list[i];
    113108        }
     
    170165                return rc;
    171166
     167        // XXX This is wrong. Wifi should not initiate DHCP
     168
    172169        /* Send DHCP discover. */
    173170        nic_address_t wifi_mac;
     
    207204        if (rc != EOK)
    208205                return rc;
     206
     207        eth_addr_t eth_addr;
     208        eth_addr_decode(wifi_mac.address, &eth_addr);
    209209
    210210        inet_link_info_t link_info;
     
    215215        size_t count;
    216216
     217        /// XXX This is wrong. Wifi should do nothing with DHCP
     218
    217219        /* Remove previous DHCP address. */
    218220        rc = inetcfg_get_addr_list(&addr_list, &count);
     
    229231                        return rc;
    230232
    231                 if (mac_matches(wifi_mac.address, link_info.mac_addr.b)) {
     233                if (eth_addr_compare(&eth_addr, &link_info.mac_addr) == 0) {
    232234                        if (str_test_prefix(addr_info.name, "dhcp")) {
    233235                                rc = inetcfg_addr_delete(addr_list[i]);
  • uspace/lib/inet/include/inet/eth_addr.h

    rb4edc96 r3e6bca8  
    4141
    4242#define ETH_ADDR_SIZE 6
     43#define ETH_ADDR_STR_SIZE (6 * 2 + 5)
    4344
     45#define ETH_ADDR_INITIALIZER(aa, bb, cc, dd, ee, ff) \
     46    { .a = ((uint64_t)(aa) << 40) | ((uint64_t)(bb) << 32) | \
     47    ((uint64_t)(cc) << 24) | ((uint64_t)(dd) << 16) | \
     48    ((uint64_t)(ee) << 8) | (ff) }
     49
     50/** Ethernet address.
     51 *
     52 * Defined as a structure. This provides strong type checking.
     53 *
     54 * Since the structure is not opaque, this allows eth_addr_t to be
     55 * allocated statically and copied around using the assignment operator.
     56 *
     57 * It is stored in the lower 48 bits of a 64-bit integer. This is an internal
     58 * representation that allows simple and efficient operation. Most CPUs
     59 * will be much faster (and we will need less instructions) operating
     60 * on a single 64-bit integer than on six individual 8-bit integers.
     61 *
     62 * Kind reader will appreciate the cleverness and elegance of this
     63 * representation.
     64 */
    4465typedef struct {
    45         uint8_t b[ETH_ADDR_SIZE];
     66        uint64_t a;
    4667} eth_addr_t;
     68
     69/** Ethernet address in the form of a string */
     70typedef struct {
     71        char str[ETH_ADDR_STR_SIZE + 1];
     72} eth_addr_str_t;
    4773
    4874extern const eth_addr_t eth_addr_broadcast;
     
    5278
    5379extern int eth_addr_compare(const eth_addr_t *, const eth_addr_t *);
     80extern void eth_addr_format(eth_addr_t *, eth_addr_str_t *);
    5481
    5582#endif
  • uspace/lib/inet/meson.build

    rb4edc96 r3e6bca8  
    4444        'src/udp.c',
    4545)
     46
     47test_src = files(
     48        'test/eth_addr.c',
     49        'test/main.c',
     50)
  • uspace/lib/inet/src/addr.c

    rb4edc96 r3e6bca8  
    5555const addr32_t addr32_broadcast_all_hosts = 0xffffffff;
    5656
    57 static const eth_addr_t inet_eth_addr_solicited_node = {
    58         0x33, 0x33, 0xff, 0, 0, 0
    59 };
     57static eth_addr_t inet_eth_addr_solicited_node =
     58    ETH_ADDR_INITIALIZER(0x33, 0x33, 0xff, 0, 0, 0);
    6059
    6160static const inet_addr_t inet_addr_any_addr = {
     
    9190void eth_addr_solicited_node(const addr128_t ip, eth_addr_t *mac)
    9291{
    93         memcpy(&mac->b[0], &inet_eth_addr_solicited_node.b[0], 3);
    94         memcpy(&mac->b[3], ip + 13, 3);
     92        uint8_t b[6];
     93        mac->a = inet_eth_addr_solicited_node.a;
     94
     95        eth_addr_encode(&inet_eth_addr_solicited_node, b);
     96        memcpy(&b[3], ip + 13, 3);
     97        eth_addr_decode(b, mac);
    9598}
    9699
  • uspace/lib/inet/src/eth_addr.c

    rb4edc96 r3e6bca8  
    3737#include <inet/eth_addr.h>
    3838#include <mem.h>
     39#include <stdio.h>
    3940
    40 const eth_addr_t eth_addr_broadcast = {
    41         0xff, 0xff, 0xff, 0xff, 0xff, 0xff
    42 };
     41const eth_addr_t eth_addr_broadcast =
     42    ETH_ADDR_INITIALIZER(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
    4343
    4444void eth_addr_encode(eth_addr_t *addr, void *buf)
    4545{
    4646        uint8_t *bp = (uint8_t *)buf;
     47        uint64_t a;
     48        int i;
    4749
    48         memcpy(bp, &addr->b[0], ETH_ADDR_SIZE);
     50        a = addr->a;
     51
     52        for (i = 0; i < ETH_ADDR_SIZE; i++)
     53                bp[i] = (a >> (40 - 8 * i)) & 0xff;
    4954}
    5055
     
    5257{
    5358        const uint8_t *bp = (uint8_t *)buf;
     59        uint64_t a;
     60        int i;
    5461
    55         memcpy(&addr->b[0], bp, ETH_ADDR_SIZE);
     62        a = 0;
     63        for (i = 0; i < ETH_ADDR_SIZE; i++)
     64                a |= (uint64_t)bp[i] << (40 - 8 * i);
     65
     66        addr->a = a;
    5667}
    5768
     
    6273int eth_addr_compare(const eth_addr_t *a, const eth_addr_t *b)
    6374{
    64         return memcmp(a->b, b->b, ETH_ADDR_SIZE) == 0;
     75        if (a->a < b->a)
     76                return -1;
     77        else if (a->a == b->a)
     78                return 0;
     79        else
     80                return 1;
     81}
     82
     83void eth_addr_format(eth_addr_t *addr, eth_addr_str_t *saddr)
     84{
     85        int i;
     86
     87        snprintf(saddr->str, 3, "%02x",
     88            (unsigned)((addr->a >> 40) & 0xff));
     89        for (i = 1; i < ETH_ADDR_SIZE; i++) {
     90                snprintf(saddr->str + 2 + 3 * (i - 1), 4, ":%02x",
     91                    (unsigned)((addr->a >> (40 - i * 8)) & 0xff));
     92        }
    6593}
    6694
  • uspace/srv/net/dhcp/dhcp.c

    rb4edc96 r3e6bca8  
    162162        hdr->flags = flag_broadcast;
    163163
    164         memcpy(dlink->link_info.mac_addr.b, hdr->chaddr,
    165             sizeof(dlink->link_info.mac_addr.b));
     164        eth_addr_encode(&dlink->link_info.mac_addr, hdr->chaddr);
    166165        hdr->opt_magic = host2uint32_t_be(dhcp_opt_magic);
    167166
     
    187186        hdr->flags = flag_broadcast;
    188187        hdr->ciaddr = host2uint32_t_be(offer->oaddr.addr);
    189         memcpy(hdr->chaddr, dlink->link_info.mac_addr.b, 6);
     188        eth_addr_encode(&dlink->link_info.mac_addr, hdr->chaddr);
    190189        hdr->opt_magic = host2uint32_t_be(dhcp_opt_magic);
    191190
  • uspace/srv/net/inetsrv/inet_link.c

    rb4edc96 r3e6bca8  
    7474    addr128_t ip_addr)
    7575{
     76        uint8_t b[ETH_ADDR_SIZE];
     77
    7678        memcpy(ip_addr, link_local_node_ip, 16);
    77 
    78         ip_addr[8] = mac_addr->b[0] ^ 0x02;
    79         ip_addr[9] = mac_addr->b[1];
    80         ip_addr[10] = mac_addr->b[2];
    81         ip_addr[13] = mac_addr->b[3];
    82         ip_addr[14] = mac_addr->b[4];
    83         ip_addr[15] = mac_addr->b[5];
     79        eth_addr_encode(mac_addr, b);
     80
     81        ip_addr[8] = b[0] ^ 0x02;
     82        ip_addr[9] = b[1];
     83        ip_addr[10] = b[2];
     84        ip_addr[13] = b[3];
     85        ip_addr[14] = b[4];
     86        ip_addr[15] = b[5];
    8487}
    8588
     
    124127static errno_t inet_iplink_change_addr(iplink_t *iplink, eth_addr_t *mac)
    125128{
     129        eth_addr_str_t saddr;
     130
     131        eth_addr_format(mac, &saddr);
    126132        log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_change_addr(): "
    127             "new addr=%02x:%02x:%02x:%02x:%02x:%02x",
    128             mac->b[0], mac->b[1], mac->b[2], mac->b[3], mac->b[4], mac->b[5]);
     133            "new addr=%s", saddr.str);
    129134
    130135        list_foreach(inet_links, link_list, inet_link_t, ilink) {
Note: See TracChangeset for help on using the changeset viewer.