chiark / gitweb /
networkd: add FDB support
authorAlin Rauta <alin.rauta@intel.com>
Wed, 17 Dec 2014 15:35:36 +0000 (07:35 -0800)
committerTom Gundersen <teg@jklm.no>
Thu, 18 Dec 2014 14:28:16 +0000 (15:28 +0100)
Makefile.am
man/systemd.network.xml
src/libsystemd/sd-rtnl/rtnl-message.c
src/libsystemd/sd-rtnl/rtnl-types.c
src/network/networkd-fdb.c [new file with mode: 0644]
src/network/networkd-link.c
src/network/networkd-network-gperf.gperf
src/network/networkd-network.c
src/network/networkd.h
src/systemd/sd-rtnl.h

index ab07d3b..6896c4b 100644 (file)
@@ -5281,6 +5281,7 @@ libsystemd_networkd_core_la_SOURCES = \
        src/network/networkd-address.c \
        src/network/networkd-route.c \
        src/network/networkd-manager.c \
+       src/network/networkd-fdb.c \
        src/network/networkd-address-pool.c
 
 nodist_libsystemd_networkd_core_la_SOURCES = \
index 79c7a23..360c57c 100644 (file)
         </refsect1>
 
         <refsect1>
+                <title>[BridgeFDB] Section Options</title>
+                        <para>The <literal>[BridgeFDB]</literal> section manages the forwarding database table of a port and accepts the following keys. Specify
+                        several <literal>[BridgeFDB]</literal> sections to configure several static MAC table entries.</para>
+
+                        <variablelist class='network-directives'>
+                                <varlistentry>
+                                        <term><varname>MACAddress=</varname></term>
+                                        <listitem>
+                                                <para>As in the <literal>[Network]</literal> section. This key is mandatory.</para>
+                                        </listitem>
+                                </varlistentry>
+                                <varlistentry>
+                                        <term><varname>VLANId=</varname></term>
+                                        <listitem>
+                                                <para>The VLAN Id for the new static MAC table entry.
+                                                If omitted, no VLAN Id info is appended to the new static MAC table entry.</para>
+                                        </listitem>
+                                </varlistentry>
+                        </variablelist>
+        </refsect1>
+
+        <refsect1>
                 <title>Example</title>
                 <example>
                         <title>/etc/systemd/network/50-static.network</title>
index 165e84d..9099440 100644 (file)
@@ -220,6 +220,58 @@ int sd_rtnl_message_new_route(sd_rtnl *rtnl, sd_rtnl_message **ret,
         return 0;
 }
 
+int sd_rtnl_message_neigh_set_flags(sd_rtnl_message *m, uint8_t flags) {
+        struct ndmsg *ndm;
+
+        assert_return(m, -EINVAL);
+        assert_return(m->hdr, -EINVAL);
+        assert_return(rtnl_message_type_is_neigh(m->hdr->nlmsg_type), -EINVAL);
+
+        ndm = NLMSG_DATA(m->hdr);
+        ndm->ndm_flags |= flags;
+
+        return 0;
+}
+
+int sd_rtnl_message_neigh_set_state(sd_rtnl_message *m, uint16_t state) {
+        struct ndmsg *ndm;
+
+        assert_return(m, -EINVAL);
+        assert_return(m->hdr, -EINVAL);
+        assert_return(rtnl_message_type_is_neigh(m->hdr->nlmsg_type), -EINVAL);
+
+        ndm = NLMSG_DATA(m->hdr);
+        ndm->ndm_state |= state;
+
+        return 0;
+}
+
+int sd_rtnl_message_neigh_get_flags(sd_rtnl_message *m, uint8_t *flags) {
+        struct ndmsg *ndm;
+
+        assert_return(m, -EINVAL);
+        assert_return(m->hdr, -EINVAL);
+        assert_return(rtnl_message_type_is_neigh(m->hdr->nlmsg_type), -EINVAL);
+
+        ndm = NLMSG_DATA(m->hdr);
+        *flags = ndm->ndm_flags;
+
+        return 0;
+}
+
+int sd_rtnl_message_neigh_get_state(sd_rtnl_message *m, uint16_t *state) {
+        struct ndmsg *ndm;
+
+        assert_return(m, -EINVAL);
+        assert_return(m->hdr, -EINVAL);
+        assert_return(rtnl_message_type_is_neigh(m->hdr->nlmsg_type), -EINVAL);
+
+        ndm = NLMSG_DATA(m->hdr);
+        *state = ndm->ndm_state;
+
+        return 0;
+}
+
 int sd_rtnl_message_neigh_get_family(sd_rtnl_message *m, int *family) {
         struct ndmsg *ndm;
 
@@ -255,7 +307,9 @@ int sd_rtnl_message_new_neigh(sd_rtnl *rtnl, sd_rtnl_message **ret, uint16_t nlm
         int r;
 
         assert_return(rtnl_message_type_is_neigh(nlmsg_type), -EINVAL);
-        assert_return(ndm_family == AF_INET || ndm_family == AF_INET6, -EINVAL);
+        assert_return(ndm_family == AF_INET  ||
+                      ndm_family == AF_INET6 ||
+                      ndm_family == PF_BRIDGE, -EINVAL);
         assert_return(ret, -EINVAL);
 
         r = message_new(rtnl, ret, nlmsg_type);
index a1db2ab..735ad75 100644 (file)
@@ -332,15 +332,12 @@ static const NLTypeSystem rtnl_route_type_system = {
 static const NLType rtnl_neigh_types[NDA_MAX + 1] = {
         [NDA_DST]               = { .type = NLA_IN_ADDR },
         [NDA_LLADDR]            = { .type = NLA_ETHER_ADDR },
-/*
-        NDA_CACHEINFO,
-        NDA_PROBES,
-        NDA_VLAN,
-        NDA_PORT
-        NDA_VNI
-        NDA_IFINDEX
-        NDA_MASTER
-*/
+        [NDA_CACHEINFO]         = { .type = NLA_CACHE_INFO, .size = sizeof(struct nda_cacheinfo) },
+        [NDA_PROBES]            = { .type = NLA_U32 },
+        [NDA_VLAN]              = { .type = NLA_U16 },
+        [NDA_PORT]              = { .type = NLA_U16 },
+        [NDA_VNI]               = { .type = NLA_U32 },
+        [NDA_IFINDEX]           = { .type = NLA_U32 },
 };
 
 static const NLTypeSystem rtnl_neigh_type_system = {
diff --git a/src/network/networkd-fdb.c b/src/network/networkd-fdb.c
new file mode 100644 (file)
index 0000000..9bb45e3
--- /dev/null
@@ -0,0 +1,252 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Intel Corporation. All rights reserved.
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <ctype.h>
+#include <net/if.h>
+#include <net/ethernet.h>
+
+#include "networkd.h"
+#include "networkd-netdev.h"
+#include "networkd-link.h"
+#include "network-internal.h"
+#include "path-util.h"
+#include "conf-files.h"
+#include "conf-parser.h"
+#include "util.h"
+
+/* create a new FDB entry or get an existing one. */
+int fdb_entry_new_static(Network *const network,
+                         const unsigned section,
+                         FdbEntry **ret) {
+        _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL;
+        struct ether_addr *mac_addr = NULL;
+
+        assert(network);
+
+        /* search entry in hashmap first. */
+        if(section) {
+                fdb_entry = hashmap_get(network->fdb_entries_by_section, UINT_TO_PTR(section));
+                if (fdb_entry) {
+                        *ret = fdb_entry;
+                        fdb_entry = NULL;
+
+                        return 0;
+                }
+        }
+
+        /* allocate space for MAC address. */
+        mac_addr = new0(struct ether_addr, 1);
+        if (!mac_addr)
+                return -ENOMEM;
+
+        /* allocate space for and FDB entry. */
+        fdb_entry = new0(FdbEntry, 1);
+
+        if (!fdb_entry) {
+                /* free previously allocated space for mac_addr. */
+                free(mac_addr);
+                return -ENOMEM;
+        }
+
+        /* init FDB structure. */
+        fdb_entry->network = network;
+        fdb_entry->mac_addr = mac_addr;
+
+        LIST_PREPEND(static_fdb_entries, network->static_fdb_entries, fdb_entry);
+
+        if (section) {
+                fdb_entry->section = section;
+                hashmap_put(network->fdb_entries_by_section,
+                            UINT_TO_PTR(fdb_entry->section), fdb_entry);
+        }
+
+        /* return allocated FDB structure. */
+        *ret = fdb_entry;
+        fdb_entry = NULL;
+
+        return 0;
+}
+
+static int set_fdb_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+        _cleanup_fdbentry_free_ FdbEntry *fdb_entry = userdata;
+        int r;
+
+        assert(fdb_entry);
+
+        r = sd_rtnl_message_get_errno(m);
+        if ((r < 0) && (r != (-EEXIST)))
+                log_error("Could not add FDB entry for interface: %s error: %s",
+                          fdb_entry->network->match_name, strerror(-r));
+
+        return 1;
+}
+
+/* send a request to the kernel to add a FDB entry in its static MAC table. */
+int fdb_entry_configure(sd_rtnl *const rtnl,
+                        FdbEntry *const fdb_entry,
+                        const int ifindex) {
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+        int r;
+
+        assert(fdb_entry);
+        assert(rtnl);
+
+        /* create new RTM message */
+        r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_NEWNEIGH, ifindex, PF_BRIDGE);
+        if (r < 0)
+                return rtnl_log_create_error(r);
+
+        /* only NTF_SELF flag supported. */
+        r = sd_rtnl_message_neigh_set_flags(req, NTF_SELF);
+        if (r < 0)
+                return rtnl_log_create_error(r);
+
+        /* only NUD_PERMANENT state supported. */
+        r = sd_rtnl_message_neigh_set_state(req, NUD_NOARP | NUD_PERMANENT);
+        if (r < 0)
+                return rtnl_log_create_error(r);
+
+        r = sd_rtnl_message_append_ether_addr(req, NDA_LLADDR, fdb_entry->mac_addr);
+        if (r < 0)
+                return rtnl_log_create_error(r);
+
+        /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
+        if (0 != fdb_entry->vlan_id) {
+                r = sd_rtnl_message_append_u16(req, NDA_VLAN, fdb_entry->vlan_id);
+                if (r < 0)
+                        return rtnl_log_create_error(r);
+        }
+
+        /* send message to the kernel to update its internal static MAC table. */
+        r = sd_rtnl_call_async(rtnl, req, set_fdb_handler, fdb_entry, 0, NULL);
+        if (r < 0) {
+                log_error("Could not send rtnetlink message: %s", strerror(-r));
+                return r;
+        }
+
+        return 0;
+}
+
+/* remove and FDB entry. */
+void fdb_entry_free(FdbEntry *fdb_entry) {
+        if(!fdb_entry)
+                return;
+
+        if(fdb_entry->network) {
+                LIST_REMOVE(static_fdb_entries, fdb_entry->network->static_fdb_entries,
+                            fdb_entry);
+
+                if(fdb_entry->section)
+                    hashmap_remove(fdb_entry->network->fdb_entries_by_section,
+                                   UINT_TO_PTR(fdb_entry->section));
+        }
+
+        free(fdb_entry->mac_addr);
+
+        free(fdb_entry);
+}
+
+/* parse the HW address from config files. */
+int config_parse_fdb_hwaddr(const char *unit,
+                            const char *filename,
+                            unsigned line,
+                            const char *section,
+                            unsigned section_line,
+                            const char *lvalue,
+                            int ltype,
+                            const char *rvalue,
+                            void *data,
+                            void *userdata) {
+        Network *network = userdata;
+        _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL;
+        int r;
+
+        assert(filename);
+        assert(section);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        r = fdb_entry_new_static(network, section_line, &fdb_entry);
+        if (r < 0) {
+                log_error("Failed to allocate a new FDB entry: %s", strerror(-r));
+                return r;
+        }
+
+        /* read in the MAC address for the FDB table. */
+        r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
+                   &fdb_entry->mac_addr->ether_addr_octet[0],
+                   &fdb_entry->mac_addr->ether_addr_octet[1],
+                   &fdb_entry->mac_addr->ether_addr_octet[2],
+                   &fdb_entry->mac_addr->ether_addr_octet[3],
+                   &fdb_entry->mac_addr->ether_addr_octet[4],
+                   &fdb_entry->mac_addr->ether_addr_octet[5]);
+
+        if (ETHER_ADDR_LEN !=  r) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+                           "Not a valid MAC address, ignoring assignment: %s", rvalue);
+                return 0;
+        }
+
+        fdb_entry = NULL;
+
+        return 0;
+}
+
+/* parse the VLAN Id from config files. */
+int config_parse_fdb_vlan_id(const char *unit,
+                             const char *filename,
+                             unsigned line,
+                             const char *section,
+                             unsigned section_line,
+                             const char *lvalue,
+                             int ltype,
+                             const char *rvalue,
+                             void *data,
+                             void *userdata) {
+        Network *network = userdata;
+        _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL;
+        int r;
+
+        assert(filename);
+        assert(section);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        r = fdb_entry_new_static(network, section_line, &fdb_entry);
+        if (r < 0) {
+                log_error("Failed to allocate a new FDB entry: %s", strerror(-r));
+                return r;
+        }
+
+        r = config_parse_unsigned(unit, filename, line, section,
+                                  section_line, lvalue, ltype,
+                                  rvalue, &fdb_entry->vlan_id, userdata);
+        if (r < 0) {
+                log_error("Failed to parse the unsigned integer: %s", strerror(-r));
+                return r;
+        }
+
+        fdb_entry = NULL;
+
+        return 0;
+}
index 08f724e..341ae88 100644 (file)
@@ -656,6 +656,21 @@ int link_address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata)
         return 1;
 }
 
+static int link_set_bridge_fdb(const Link *const link) {
+        FdbEntry *fdb_entry;
+        int r = 0;
+
+        LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) {
+                r = fdb_entry_configure(link->manager->rtnl, fdb_entry, link->ifindex);
+                if(r < 0) {
+                        log_link_error(link, "Failed to add MAC entry to static MAC table: %s", strerror(-r));
+                        break;
+                }
+        }
+
+        return r;
+}
+
 static int link_set_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         _cleanup_link_unref_ Link *link = userdata;
         int r;
@@ -1147,6 +1162,10 @@ static int link_configure(Link *link) {
         assert(link->network);
         assert(link->state == LINK_STATE_PENDING);
 
+        r = link_set_bridge_fdb(link);
+        if (r < 0)
+                return r;
+
         if (link_ipv4ll_enabled(link)) {
                 r = ipv4ll_configure(link);
                 if (r < 0)
index 640a3a2..fb0a209 100644 (file)
@@ -62,6 +62,8 @@ DHCP.CriticalConnection,     config_parse_bool,                  0,
 DHCP.VendorClassIdentifier,  config_parse_string,                0,                             offsetof(Network, dhcp_vendor_class_identifier)
 DHCP.RouteMetric,            config_parse_unsigned,              0,                             offsetof(Network, dhcp_route_metric)
 Bridge.Cost,                 config_parse_unsigned,              0,                             offsetof(Network, cost)
+BridgeFDB.MACAddress,        config_parse_fdb_hwaddr,            0,                             0
+BridgeFDB.VLANId,            config_parse_fdb_vlan_id,           0,                             0
 /* backwards compatibility: do not add new entries to this section */
 DHCPv4.UseDNS,               config_parse_bool,                  0,                             offsetof(Network, dhcp_dns)
 DHCPv4.UseMTU,               config_parse_bool,                  0,                             offsetof(Network, dhcp_mtu)
index ef9e0a8..966b59b 100644 (file)
@@ -62,6 +62,7 @@ static int network_load_one(Manager *manager, const char *filename) {
 
         LIST_HEAD_INIT(network->static_addresses);
         LIST_HEAD_INIT(network->static_routes);
+        LIST_HEAD_INIT(network->static_fdb_entries);
 
         network->stacked_netdevs = hashmap_new(&string_hash_ops);
         if (!network->stacked_netdevs)
@@ -75,6 +76,10 @@ static int network_load_one(Manager *manager, const char *filename) {
         if (!network->routes_by_section)
                 return log_oom();
 
+        network->fdb_entries_by_section = hashmap_new(NULL);
+        if (!network->fdb_entries_by_section)
+                return log_oom();
+
         network->filename = strdup(filename);
         if (!network->filename)
                 return log_oom();
@@ -97,7 +102,8 @@ static int network_load_one(Manager *manager, const char *filename) {
                          "Route\0"
                          "DHCP\0"
                          "DHCPv4\0"
-                         "Bridge\0",
+                         "Bridge\0"
+                         "BridgeFDB\0",
                          config_item_perf_lookup, network_network_gperf_lookup,
                          false, false, true, network);
         if (r < 0)
@@ -154,6 +160,7 @@ void network_free(Network *network) {
         NetDev *netdev;
         Route *route;
         Address *address;
+        FdbEntry *fdb_entry;
         Iterator i;
 
         if (!network)
@@ -192,8 +199,12 @@ void network_free(Network *network) {
         while ((address = network->static_addresses))
                 address_free(address);
 
+        while ((fdb_entry = network->static_fdb_entries))
+                fdb_entry_free(fdb_entry);
+
         hashmap_free(network->addresses_by_section);
         hashmap_free(network->routes_by_section);
+        hashmap_free(network->fdb_entries_by_section);
 
         if (network->manager && network->manager->networks)
                 LIST_REMOVE(networks, network->manager->networks, network);
index 4cdcd73..a5c5b08 100644 (file)
@@ -51,6 +51,7 @@ typedef struct Address Address;
 typedef struct Route Route;
 typedef struct Manager Manager;
 typedef struct AddressPool AddressPool;
+typedef struct FdbEntry FdbEntry;
 
 typedef enum DHCPSupport {
         DHCP_SUPPORT_NONE,
@@ -69,6 +70,16 @@ typedef enum LLMNRSupport {
         _LLMNR_SUPPORT_INVALID = -1,
 } LLMNRSupport;
 
+struct FdbEntry {
+        Network *network;
+        unsigned section;
+
+        struct ether_addr *mac_addr;
+        uint16_t vlan_id;
+
+        LIST_FIELDS(FdbEntry, static_fdb_entries);
+};
+
 struct Network {
         Manager *manager;
 
@@ -113,9 +124,11 @@ struct Network {
 
         LIST_HEAD(Address, static_addresses);
         LIST_HEAD(Route, static_routes);
+        LIST_HEAD(FdbEntry, static_fdb_entries);
 
         Hashmap *addresses_by_section;
         Hashmap *routes_by_section;
+        Hashmap *fdb_entries_by_section;
 
         bool wildcard_domain;
         char **domains, **dns, **ntp;
@@ -327,6 +340,22 @@ int config_parse_label(const char *unit, const char *filename, unsigned line,
                        const char *section, unsigned section_line, const char *lvalue,
                        int ltype, const char *rvalue, void *data, void *userdata);
 
+/* Forwarding database table. */
+int fdb_entry_configure(sd_rtnl *const rtnl, FdbEntry *const fdb_entry, const int ifindex);
+void fdb_entry_free(FdbEntry *fdb_entry);
+int fdb_entry_new_static(Network *const network, const unsigned section, FdbEntry **ret);
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(FdbEntry*, fdb_entry_free);
+#define _cleanup_fdbentry_free_ _cleanup_(fdb_entry_freep)
+
+int config_parse_fdb_hwaddr(const char *unit, const char *filename, unsigned line,
+                            const char *section, unsigned section_line, const char *lvalue,
+                            int ltype, const char *rvalue, void *data, void *userdata);
+
+int config_parse_fdb_vlan_id(const char *unit, const char *filename, unsigned line,
+                             const char *section, unsigned section_line, const char *lvalue,
+                             int ltype, const char *rvalue, void *data, void *userdata);
+
 /* DHCP support */
 
 const char* dhcp_support_to_string(DHCPSupport i) _const_;
index b05f83c..b8836e2 100644 (file)
@@ -109,8 +109,12 @@ int sd_rtnl_message_route_get_family(sd_rtnl_message *m, int *family);
 int sd_rtnl_message_route_get_dst_prefixlen(sd_rtnl_message *m, unsigned char *dst_len);
 int sd_rtnl_message_route_get_src_prefixlen(sd_rtnl_message *m, unsigned char *src_len);
 
+int sd_rtnl_message_neigh_set_flags(sd_rtnl_message *m, uint8_t flags);
+int sd_rtnl_message_neigh_set_state(sd_rtnl_message *m, uint16_t state);
 int sd_rtnl_message_neigh_get_family(sd_rtnl_message *m, int *family);
 int sd_rtnl_message_neigh_get_ifindex(sd_rtnl_message *m, int *family);
+int sd_rtnl_message_neigh_get_state(sd_rtnl_message *m, uint16_t *state);
+int sd_rtnl_message_neigh_get_flags(sd_rtnl_message *m, uint8_t *flags);
 
 int sd_rtnl_message_append_string(sd_rtnl_message *m, unsigned short type, const char *data);
 int sd_rtnl_message_append_u8(sd_rtnl_message *m, unsigned short type, uint8_t data);