LCOV - code coverage report
Current view: top level - vswitchd - bridge.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1622 2078 78.1 %
Date: 2016-09-14 01:02:56 Functions: 115 128 89.8 %
Branches: 822 1471 55.9 %

           Branch data     Line data    Source code
       1                 :            : /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
       2                 :            :  *
       3                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       4                 :            :  * you may not use this file except in compliance with the License.
       5                 :            :  * You may obtain a copy of the License at:
       6                 :            :  *
       7                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
       8                 :            :  *
       9                 :            :  * Unless required by applicable law or agreed to in writing, software
      10                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      11                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12                 :            :  * See the License for the specific language governing permissions and
      13                 :            :  * limitations under the License.
      14                 :            :  */
      15                 :            : 
      16                 :            : #include <config.h>
      17                 :            : #include "bridge.h"
      18                 :            : #include <errno.h>
      19                 :            : #include <inttypes.h>
      20                 :            : #include <stdlib.h>
      21                 :            : 
      22                 :            : #include "async-append.h"
      23                 :            : #include "bfd.h"
      24                 :            : #include "bitmap.h"
      25                 :            : #include "cfm.h"
      26                 :            : #include "connectivity.h"
      27                 :            : #include "coverage.h"
      28                 :            : #include "daemon.h"
      29                 :            : #include "dirs.h"
      30                 :            : #include "dpif.h"
      31                 :            : #include "hash.h"
      32                 :            : #include "openvswitch/hmap.h"
      33                 :            : #include "hmapx.h"
      34                 :            : #include "if-notifier.h"
      35                 :            : #include "jsonrpc.h"
      36                 :            : #include "lacp.h"
      37                 :            : #include "lib/netdev-dpdk.h"
      38                 :            : #include "mac-learning.h"
      39                 :            : #include "mcast-snooping.h"
      40                 :            : #include "netdev.h"
      41                 :            : #include "nx-match.h"
      42                 :            : #include "ofproto/bond.h"
      43                 :            : #include "ofproto/ofproto.h"
      44                 :            : #include "openvswitch/dynamic-string.h"
      45                 :            : #include "openvswitch/list.h"
      46                 :            : #include "openvswitch/meta-flow.h"
      47                 :            : #include "openvswitch/ofp-print.h"
      48                 :            : #include "openvswitch/ofp-util.h"
      49                 :            : #include "openvswitch/ofpbuf.h"
      50                 :            : #include "openvswitch/vlog.h"
      51                 :            : #include "ovs-lldp.h"
      52                 :            : #include "ovs-numa.h"
      53                 :            : #include "packets.h"
      54                 :            : #include "poll-loop.h"
      55                 :            : #include "seq.h"
      56                 :            : #include "sflow_api.h"
      57                 :            : #include "sha1.h"
      58                 :            : #include "openvswitch/shash.h"
      59                 :            : #include "smap.h"
      60                 :            : #include "socket-util.h"
      61                 :            : #include "stream.h"
      62                 :            : #include "stream-ssl.h"
      63                 :            : #include "sset.h"
      64                 :            : #include "system-stats.h"
      65                 :            : #include "timeval.h"
      66                 :            : #include "util.h"
      67                 :            : #include "unixctl.h"
      68                 :            : #include "lib/vswitch-idl.h"
      69                 :            : #include "xenserver.h"
      70                 :            : #include "vlan-bitmap.h"
      71                 :            : 
      72                 :       1288 : VLOG_DEFINE_THIS_MODULE(bridge);
      73                 :            : 
      74                 :      97096 : COVERAGE_DEFINE(bridge_reconfigure);
      75                 :            : 
      76                 :            : struct iface {
      77                 :            :     /* These members are always valid.
      78                 :            :      *
      79                 :            :      * They are immutable: they never change between iface_create() and
      80                 :            :      * iface_destroy(). */
      81                 :            :     struct ovs_list port_elem;  /* Element in struct port's "ifaces" list. */
      82                 :            :     struct hmap_node name_node; /* In struct bridge's "iface_by_name" hmap. */
      83                 :            :     struct hmap_node ofp_port_node; /* In struct bridge's "ifaces" hmap. */
      84                 :            :     struct port *port;          /* Containing port. */
      85                 :            :     char *name;                 /* Host network device name. */
      86                 :            :     struct netdev *netdev;      /* Network device. */
      87                 :            :     ofp_port_t ofp_port;        /* OpenFlow port number. */
      88                 :            :     uint64_t change_seq;
      89                 :            : 
      90                 :            :     /* These members are valid only within bridge_reconfigure(). */
      91                 :            :     const char *type;           /* Usually same as cfg->type. */
      92                 :            :     const char *netdev_type;    /* type that should be used for netdev_open. */
      93                 :            :     const struct ovsrec_interface *cfg;
      94                 :            : };
      95                 :            : 
      96                 :            : struct mirror {
      97                 :            :     struct uuid uuid;           /* UUID of this "mirror" record in database. */
      98                 :            :     struct hmap_node hmap_node; /* In struct bridge's "mirrors" hmap. */
      99                 :            :     struct bridge *bridge;
     100                 :            :     char *name;
     101                 :            :     const struct ovsrec_mirror *cfg;
     102                 :            : };
     103                 :            : 
     104                 :            : struct port {
     105                 :            :     struct hmap_node hmap_node; /* Element in struct bridge's "ports" hmap. */
     106                 :            :     struct bridge *bridge;
     107                 :            :     char *name;
     108                 :            : 
     109                 :            :     const struct ovsrec_port *cfg;
     110                 :            : 
     111                 :            :     /* An ordinary bridge port has 1 interface.
     112                 :            :      * A bridge port for bonding has at least 2 interfaces. */
     113                 :            :     struct ovs_list ifaces;    /* List of "struct iface"s. */
     114                 :            : };
     115                 :            : 
     116                 :            : struct bridge {
     117                 :            :     struct hmap_node node;      /* In 'all_bridges'. */
     118                 :            :     char *name;                 /* User-specified arbitrary name. */
     119                 :            :     char *type;                 /* Datapath type. */
     120                 :            :     struct eth_addr ea;         /* Bridge Ethernet Address. */
     121                 :            :     struct eth_addr default_ea; /* Default MAC. */
     122                 :            :     const struct ovsrec_bridge *cfg;
     123                 :            : 
     124                 :            :     /* OpenFlow switch processing. */
     125                 :            :     struct ofproto *ofproto;    /* OpenFlow switch. */
     126                 :            : 
     127                 :            :     /* Bridge ports. */
     128                 :            :     struct hmap ports;          /* "struct port"s indexed by name. */
     129                 :            :     struct hmap ifaces;         /* "struct iface"s indexed by ofp_port. */
     130                 :            :     struct hmap iface_by_name;  /* "struct iface"s indexed by name. */
     131                 :            : 
     132                 :            :     /* Port mirroring. */
     133                 :            :     struct hmap mirrors;        /* "struct mirror" indexed by UUID. */
     134                 :            : 
     135                 :            :     /* Auto Attach */
     136                 :            :     struct hmap mappings;       /* "struct" indexed by UUID */
     137                 :            : 
     138                 :            :     /* Used during reconfiguration. */
     139                 :            :     struct shash wanted_ports;
     140                 :            : 
     141                 :            :     /* Synthetic local port if necessary. */
     142                 :            :     struct ovsrec_port synth_local_port;
     143                 :            :     struct ovsrec_interface synth_local_iface;
     144                 :            :     struct ovsrec_interface *synth_local_ifacep;
     145                 :            : };
     146                 :            : 
     147                 :            : struct aa_mapping {
     148                 :            :     struct hmap_node hmap_node; /* In struct bridge's "mappings" hmap. */
     149                 :            :     struct bridge *bridge;
     150                 :            :     uint32_t isid;
     151                 :            :     uint16_t vlan;
     152                 :            :     char *br_name;
     153                 :            : };
     154                 :            : 
     155                 :            : /* All bridges, indexed by name. */
     156                 :            : static struct hmap all_bridges = HMAP_INITIALIZER(&all_bridges);
     157                 :            : 
     158                 :            : /* OVSDB IDL used to obtain configuration. */
     159                 :            : static struct ovsdb_idl *idl;
     160                 :            : 
     161                 :            : /* We want to complete daemonization, fully detaching from our parent process,
     162                 :            :  * only after we have completed our initial configuration, committed our state
     163                 :            :  * to the database, and received confirmation back from the database server
     164                 :            :  * that it applied the commit.  This allows our parent process to know that,
     165                 :            :  * post-detach, ephemeral fields such as datapath-id and ofport are very likely
     166                 :            :  * to have already been filled in.  (It is only "very likely" rather than
     167                 :            :  * certain because there is always a slim possibility that the transaction will
     168                 :            :  * fail or that some other client has added new bridges, ports, etc. while
     169                 :            :  * ovs-vswitchd was configuring using an old configuration.)
     170                 :            :  *
     171                 :            :  * We only need to do this once for our initial configuration at startup, so
     172                 :            :  * 'initial_config_done' tracks whether we've already done it.  While we are
     173                 :            :  * waiting for a response to our commit, 'daemonize_txn' tracks the transaction
     174                 :            :  * itself and is otherwise NULL. */
     175                 :            : static bool initial_config_done;
     176                 :            : static struct ovsdb_idl_txn *daemonize_txn;
     177                 :            : 
     178                 :            : /* Most recently processed IDL sequence number. */
     179                 :            : static unsigned int idl_seqno;
     180                 :            : 
     181                 :            : /* Track changes to port connectivity. */
     182                 :            : static uint64_t connectivity_seqno = LLONG_MIN;
     183                 :            : 
     184                 :            : /* Status update to database.
     185                 :            :  *
     186                 :            :  * Some information in the database must be kept as up-to-date as possible to
     187                 :            :  * allow controllers to respond rapidly to network outages.  Those status are
     188                 :            :  * updated via the 'status_txn'.
     189                 :            :  *
     190                 :            :  * We use the global connectivity sequence number to detect the status change.
     191                 :            :  * Also, to prevent the status update from sending too much to the database,
     192                 :            :  * we check the return status of each update transaction and do not start new
     193                 :            :  * update if the previous transaction status is 'TXN_INCOMPLETE'.
     194                 :            :  *
     195                 :            :  * 'statux_txn' is NULL if there is no ongoing status update.
     196                 :            :  *
     197                 :            :  * If the previous database transaction was failed (is not 'TXN_SUCCESS',
     198                 :            :  * 'TXN_UNCHANGED' or 'TXN_INCOMPLETE'), 'status_txn_try_again' is set to true,
     199                 :            :  * which will cause the main thread wake up soon and retry the status update.
     200                 :            :  */
     201                 :            : static struct ovsdb_idl_txn *status_txn;
     202                 :            : static bool status_txn_try_again;
     203                 :            : 
     204                 :            : /* When the status update transaction returns 'TXN_INCOMPLETE', should register a
     205                 :            :  * timeout in 'STATUS_CHECK_AGAIN_MSEC' to check again. */
     206                 :            : #define STATUS_CHECK_AGAIN_MSEC 100
     207                 :            : 
     208                 :            : /* Statistics update to database. */
     209                 :            : static struct ovsdb_idl_txn *stats_txn;
     210                 :            : 
     211                 :            : /* Each time this timer expires, the bridge fetches interface and mirror
     212                 :            :  * statistics and pushes them into the database. */
     213                 :            : static int stats_timer_interval;
     214                 :            : static long long int stats_timer = LLONG_MIN;
     215                 :            : 
     216                 :            : /* Each time this timer expires, the bridge fetches the list of port/VLAN
     217                 :            :  * membership that has been modified by the AA.
     218                 :            :  */
     219                 :            : #define AA_REFRESH_INTERVAL (1000) /* In milliseconds. */
     220                 :            : static long long int aa_refresh_timer = LLONG_MIN;
     221                 :            : 
     222                 :            : /* Whenever system interfaces are added, removed or change state, the bridge
     223                 :            :  * will be reconfigured.
     224                 :            :  */
     225                 :            : static struct if_notifier *ifnotifier;
     226                 :            : static bool ifaces_changed = false;
     227                 :            : 
     228                 :            : static void add_del_bridges(const struct ovsrec_open_vswitch *);
     229                 :            : static void bridge_run__(void);
     230                 :            : static void bridge_create(const struct ovsrec_bridge *);
     231                 :            : static void bridge_destroy(struct bridge *, bool del);
     232                 :            : static struct bridge *bridge_lookup(const char *name);
     233                 :            : static unixctl_cb_func bridge_unixctl_dump_flows;
     234                 :            : static unixctl_cb_func bridge_unixctl_reconnect;
     235                 :            : static size_t bridge_get_controllers(const struct bridge *br,
     236                 :            :                                      struct ovsrec_controller ***controllersp);
     237                 :            : static void bridge_collect_wanted_ports(struct bridge *,
     238                 :            :                                         struct shash *wanted_ports);
     239                 :            : static void bridge_delete_ofprotos(void);
     240                 :            : static void bridge_delete_or_reconfigure_ports(struct bridge *);
     241                 :            : static void bridge_del_ports(struct bridge *,
     242                 :            :                              const struct shash *wanted_ports);
     243                 :            : static void bridge_add_ports(struct bridge *,
     244                 :            :                              const struct shash *wanted_ports);
     245                 :            : 
     246                 :            : static void bridge_configure_datapath_id(struct bridge *);
     247                 :            : static void bridge_configure_netflow(struct bridge *);
     248                 :            : static void bridge_configure_forward_bpdu(struct bridge *);
     249                 :            : static void bridge_configure_mac_table(struct bridge *);
     250                 :            : static void bridge_configure_mcast_snooping(struct bridge *);
     251                 :            : static void bridge_configure_sflow(struct bridge *, int *sflow_bridge_number);
     252                 :            : static void bridge_configure_ipfix(struct bridge *);
     253                 :            : static void bridge_configure_spanning_tree(struct bridge *);
     254                 :            : static void bridge_configure_tables(struct bridge *);
     255                 :            : static void bridge_configure_dp_desc(struct bridge *);
     256                 :            : static void bridge_configure_aa(struct bridge *);
     257                 :            : static void bridge_aa_refresh_queued(struct bridge *);
     258                 :            : static bool bridge_aa_need_refresh(struct bridge *);
     259                 :            : static void bridge_configure_remotes(struct bridge *,
     260                 :            :                                      const struct sockaddr_in *managers,
     261                 :            :                                      size_t n_managers);
     262                 :            : static void bridge_pick_local_hw_addr(struct bridge *, struct eth_addr *ea,
     263                 :            :                                       struct iface **hw_addr_iface);
     264                 :            : static uint64_t bridge_pick_datapath_id(struct bridge *,
     265                 :            :                                         const struct eth_addr bridge_ea,
     266                 :            :                                         struct iface *hw_addr_iface);
     267                 :            : static uint64_t dpid_from_hash(const void *, size_t nbytes);
     268                 :            : static bool bridge_has_bond_fake_iface(const struct bridge *,
     269                 :            :                                        const char *name);
     270                 :            : static bool port_is_bond_fake_iface(const struct port *);
     271                 :            : 
     272                 :            : static unixctl_cb_func qos_unixctl_show_types;
     273                 :            : static unixctl_cb_func qos_unixctl_show;
     274                 :            : 
     275                 :            : static struct port *port_create(struct bridge *, const struct ovsrec_port *);
     276                 :            : static void port_del_ifaces(struct port *);
     277                 :            : static void port_destroy(struct port *);
     278                 :            : static struct port *port_lookup(const struct bridge *, const char *name);
     279                 :            : static void port_configure(struct port *);
     280                 :            : static struct lacp_settings *port_configure_lacp(struct port *,
     281                 :            :                                                  struct lacp_settings *);
     282                 :            : static void port_configure_bond(struct port *, struct bond_settings *);
     283                 :            : static bool port_is_synthetic(const struct port *);
     284                 :            : 
     285                 :            : static void reconfigure_system_stats(const struct ovsrec_open_vswitch *);
     286                 :            : static void run_system_stats(void);
     287                 :            : 
     288                 :            : static void bridge_configure_mirrors(struct bridge *);
     289                 :            : static struct mirror *mirror_create(struct bridge *,
     290                 :            :                                     const struct ovsrec_mirror *);
     291                 :            : static void mirror_destroy(struct mirror *);
     292                 :            : static bool mirror_configure(struct mirror *);
     293                 :            : static void mirror_refresh_stats(struct mirror *);
     294                 :            : 
     295                 :            : static void iface_configure_lacp(struct iface *, struct lacp_slave_settings *);
     296                 :            : static bool iface_create(struct bridge *, const struct ovsrec_interface *,
     297                 :            :                          const struct ovsrec_port *);
     298                 :            : static bool iface_is_internal(const struct ovsrec_interface *iface,
     299                 :            :                               const struct ovsrec_bridge *br);
     300                 :            : static const char *iface_get_type(const struct ovsrec_interface *,
     301                 :            :                                   const struct ovsrec_bridge *);
     302                 :            : static void iface_destroy(struct iface *);
     303                 :            : static void iface_destroy__(struct iface *);
     304                 :            : static struct iface *iface_lookup(const struct bridge *, const char *name);
     305                 :            : static struct iface *iface_find(const char *name);
     306                 :            : static struct iface *iface_from_ofp_port(const struct bridge *,
     307                 :            :                                          ofp_port_t ofp_port);
     308                 :            : static void iface_set_mac(const struct bridge *, const struct port *, struct iface *);
     309                 :            : static void iface_set_ofport(const struct ovsrec_interface *, ofp_port_t ofport);
     310                 :            : static void iface_clear_db_record(const struct ovsrec_interface *if_cfg, char *errp);
     311                 :            : static void iface_configure_qos(struct iface *, const struct ovsrec_qos *);
     312                 :            : static void iface_configure_cfm(struct iface *);
     313                 :            : static void iface_refresh_cfm_stats(struct iface *);
     314                 :            : static void iface_refresh_stats(struct iface *);
     315                 :            : static void iface_refresh_netdev_status(struct iface *);
     316                 :            : static void iface_refresh_ofproto_status(struct iface *);
     317                 :            : static bool iface_is_synthetic(const struct iface *);
     318                 :            : static ofp_port_t iface_get_requested_ofp_port(
     319                 :            :     const struct ovsrec_interface *);
     320                 :            : static ofp_port_t iface_pick_ofport(const struct ovsrec_interface *);
     321                 :            : 
     322                 :            : 
     323                 :            : static void discover_types(const struct ovsrec_open_vswitch *cfg);
     324                 :            : 
     325                 :            : static void
     326                 :     103919 : bridge_init_ofproto(const struct ovsrec_open_vswitch *cfg)
     327                 :            : {
     328                 :            :     struct shash iface_hints;
     329                 :            :     static bool initialized = false;
     330                 :            :     int i;
     331                 :            : 
     332         [ +  + ]:     103919 :     if (initialized) {
     333                 :     103304 :         return;
     334                 :            :     }
     335                 :            : 
     336                 :        615 :     shash_init(&iface_hints);
     337                 :            : 
     338         [ +  + ]:        615 :     if (cfg) {
     339         [ +  + ]:        613 :         for (i = 0; i < cfg->n_bridges; i++) {
     340                 :          5 :             const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
     341                 :            :             int j;
     342                 :            : 
     343         [ +  + ]:         14 :             for (j = 0; j < br_cfg->n_ports; j++) {
     344                 :          9 :                 struct ovsrec_port *port_cfg = br_cfg->ports[j];
     345                 :            :                 int k;
     346                 :            : 
     347         [ +  + ]:         18 :                 for (k = 0; k < port_cfg->n_interfaces; k++) {
     348                 :          9 :                     struct ovsrec_interface *if_cfg = port_cfg->interfaces[k];
     349                 :            :                     struct iface_hint *iface_hint;
     350                 :            : 
     351                 :          9 :                     iface_hint = xmalloc(sizeof *iface_hint);
     352                 :          9 :                     iface_hint->br_name = br_cfg->name;
     353                 :          9 :                     iface_hint->br_type = br_cfg->datapath_type;
     354                 :          9 :                     iface_hint->ofp_port = iface_pick_ofport(if_cfg);
     355                 :            : 
     356                 :          9 :                     shash_add(&iface_hints, if_cfg->name, iface_hint);
     357                 :            :                 }
     358                 :            :             }
     359                 :            :         }
     360                 :            :     }
     361                 :            : 
     362                 :        615 :     ofproto_init(&iface_hints);
     363                 :            : 
     364                 :        615 :     shash_destroy_free_data(&iface_hints);
     365                 :        615 :     initialized = true;
     366                 :            : }
     367                 :            : 
     368                 :            : static void
     369                 :       2211 : if_change_cb(void *aux OVS_UNUSED)
     370                 :            : {
     371                 :       2211 :     ifaces_changed = true;
     372                 :       2211 : }
     373                 :            : 
     374                 :            : /* Public functions. */
     375                 :            : 
     376                 :            : /* Initializes the bridge module, configuring it to obtain its configuration
     377                 :            :  * from an OVSDB server accessed over 'remote', which should be a string in a
     378                 :            :  * form acceptable to ovsdb_idl_create(). */
     379                 :            : void
     380                 :        617 : bridge_init(const char *remote)
     381                 :            : {
     382                 :            :     /* Create connection to database. */
     383                 :        617 :     idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true, true);
     384                 :        617 :     idl_seqno = ovsdb_idl_get_seqno(idl);
     385                 :        617 :     ovsdb_idl_set_lock(idl, "ovs_vswitchd");
     386                 :        617 :     ovsdb_idl_verify_write_only(idl);
     387                 :            : 
     388                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
     389                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
     390                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_datapath_types);
     391                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_iface_types);
     392                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
     393                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_ovs_version);
     394                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_db_version);
     395                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_type);
     396                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_system_version);
     397                 :            : 
     398                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_id);
     399                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_datapath_version);
     400                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_status);
     401                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_rstp_status);
     402                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_stp_enable);
     403                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_bridge_col_rstp_enable);
     404                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
     405                 :            : 
     406                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_port_col_status);
     407                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_port_col_rstp_status);
     408                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_port_col_rstp_statistics);
     409                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_port_col_statistics);
     410                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_port_col_bond_active_slave);
     411                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
     412                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_port_col_trunks);
     413                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_port_col_vlan_mode);
     414                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_admin_state);
     415                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_duplex);
     416                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_speed);
     417                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_state);
     418                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_link_resets);
     419                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mac_in_use);
     420                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ifindex);
     421                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_mtu);
     422                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
     423                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
     424                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_status);
     425                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault);
     426                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_fault_status);
     427                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_remote_mpids);
     428                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_flap_count);
     429                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_health);
     430                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_cfm_remote_opstate);
     431                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_bfd_status);
     432                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_lacp_current);
     433                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_error);
     434                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
     435                 :            : 
     436                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_is_connected);
     437                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_role);
     438                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_controller_col_status);
     439                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_controller_col_external_ids);
     440                 :            : 
     441                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_qos_col_external_ids);
     442                 :            : 
     443                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
     444                 :            : 
     445                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
     446                 :        617 :     ovsdb_idl_omit_alert(idl, &ovsrec_mirror_col_statistics);
     447                 :            : 
     448                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
     449                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_sflow_col_external_ids);
     450                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_ipfix_col_external_ids);
     451                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_flow_sample_collector_set_col_external_ids);
     452                 :            : 
     453                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_manager_col_external_ids);
     454                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_manager_col_inactivity_probe);
     455                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_manager_col_is_connected);
     456                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_manager_col_max_backoff);
     457                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_manager_col_status);
     458                 :            : 
     459                 :        617 :     ovsdb_idl_omit(idl, &ovsrec_ssl_col_external_ids);
     460                 :            : 
     461                 :            :     /* Register unixctl commands. */
     462                 :        617 :     unixctl_command_register("qos/show-types", "interface", 1, 1,
     463                 :            :                              qos_unixctl_show_types, NULL);
     464                 :        617 :     unixctl_command_register("qos/show", "interface", 1, 1,
     465                 :            :                              qos_unixctl_show, NULL);
     466                 :        617 :     unixctl_command_register("bridge/dump-flows", "bridge", 1, 1,
     467                 :            :                              bridge_unixctl_dump_flows, NULL);
     468                 :        617 :     unixctl_command_register("bridge/reconnect", "[bridge]", 0, 1,
     469                 :            :                              bridge_unixctl_reconnect, NULL);
     470                 :        617 :     lacp_init();
     471                 :        617 :     bond_init();
     472                 :        617 :     cfm_init();
     473                 :        617 :     bfd_init();
     474                 :        617 :     ovs_numa_init();
     475                 :        617 :     stp_init();
     476                 :        617 :     lldp_init();
     477                 :        617 :     rstp_init();
     478                 :        617 :     ifnotifier = if_notifier_create(if_change_cb, NULL);
     479                 :        617 : }
     480                 :            : 
     481                 :            : void
     482                 :        617 : bridge_exit(void)
     483                 :            : {
     484                 :            :     struct bridge *br, *next_br;
     485                 :            : 
     486                 :        617 :     if_notifier_destroy(ifnotifier);
     487 [ +  + ][ -  + ]:       1355 :     HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
                 [ +  + ]
     488                 :        738 :         bridge_destroy(br, false);
     489                 :            :     }
     490                 :        617 :     ovsdb_idl_destroy(idl);
     491                 :        617 : }
     492                 :            : 
     493                 :            : /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
     494                 :            :  * addresses and ports into '*managersp' and '*n_managersp'.  The caller is
     495                 :            :  * responsible for freeing '*managersp' (with free()).
     496                 :            :  *
     497                 :            :  * You may be asking yourself "why does ovs-vswitchd care?", because
     498                 :            :  * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
     499                 :            :  * should not be and in fact is not directly involved in that.  But
     500                 :            :  * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
     501                 :            :  * it has to tell in-band control where the managers are to enable that.
     502                 :            :  * (Thus, only managers connected in-band and with non-loopback addresses
     503                 :            :  * are collected.)
     504                 :            :  */
     505                 :            : static void
     506                 :       4238 : collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
     507                 :            :                          struct sockaddr_in **managersp, size_t *n_managersp)
     508                 :            : {
     509                 :       4238 :     struct sockaddr_in *managers = NULL;
     510                 :       4238 :     size_t n_managers = 0;
     511                 :            :     struct sset targets;
     512                 :            :     size_t i;
     513                 :            : 
     514                 :            :     /* Collect all of the potential targets from the "targets" columns of the
     515                 :            :      * rows pointed to by "manager_options", excluding any that are
     516                 :            :      * out-of-band. */
     517                 :       4238 :     sset_init(&targets);
     518         [ -  + ]:       4238 :     for (i = 0; i < ovs_cfg->n_manager_options; i++) {
     519                 :          0 :         struct ovsrec_manager *m = ovs_cfg->manager_options[i];
     520                 :            : 
     521 [ #  # ][ #  # ]:          0 :         if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
     522                 :          0 :             sset_find_and_delete(&targets, m->target);
     523                 :            :         } else {
     524                 :          0 :             sset_add(&targets, m->target);
     525                 :            :         }
     526                 :            :     }
     527                 :            : 
     528                 :            :     /* Now extract the targets' IP addresses. */
     529         [ -  + ]:       4238 :     if (!sset_is_empty(&targets)) {
     530                 :            :         const char *target;
     531                 :            : 
     532                 :          0 :         managers = xmalloc(sset_count(&targets) * sizeof *managers);
     533 [ #  # ][ #  # ]:          0 :         SSET_FOR_EACH (target, &targets) {
                 [ #  # ]
     534                 :            :             union {
     535                 :            :                 struct sockaddr_storage ss;
     536                 :            :                 struct sockaddr_in in;
     537                 :            :             } sa;
     538                 :            : 
     539                 :            :             /* Ignore loopback. */
     540         [ #  # ]:          0 :             if (stream_parse_target_with_default_port(target, OVSDB_PORT,
     541                 :            :                                                       &sa.ss)
     542         [ #  # ]:          0 :                 && sa.ss.ss_family == AF_INET
     543         [ #  # ]:          0 :                 && sa.in.sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
     544                 :          0 :                 managers[n_managers++] = sa.in;
     545                 :            :             }
     546                 :            :         }
     547                 :            :     }
     548                 :       4238 :     sset_destroy(&targets);
     549                 :            : 
     550                 :       4238 :     *managersp = managers;
     551                 :       4238 :     *n_managersp = n_managers;
     552                 :       4238 : }
     553                 :            : 
     554                 :            : static void
     555                 :       4238 : bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
     556                 :            : {
     557                 :            :     struct sockaddr_in *managers;
     558                 :            :     struct bridge *br, *next;
     559                 :            :     int sflow_bridge_number;
     560                 :            :     size_t n_managers;
     561                 :            : 
     562                 :       4238 :     COVERAGE_INC(bridge_reconfigure);
     563                 :            : 
     564                 :       4238 :     ofproto_set_flow_limit(smap_get_int(&ovs_cfg->other_config, "flow-limit",
     565                 :            :                                         OFPROTO_FLOW_LIMIT_DEFAULT));
     566                 :       4238 :     ofproto_set_max_idle(smap_get_int(&ovs_cfg->other_config, "max-idle",
     567                 :            :                                       OFPROTO_MAX_IDLE_DEFAULT));
     568                 :       4238 :     ofproto_set_cpu_mask(smap_get(&ovs_cfg->other_config, "pmd-cpu-mask"));
     569                 :            : 
     570                 :       4238 :     ofproto_set_threads(
     571                 :            :         smap_get_int(&ovs_cfg->other_config, "n-handler-threads", 0),
     572                 :            :         smap_get_int(&ovs_cfg->other_config, "n-revalidator-threads", 0));
     573                 :            : 
     574                 :            :     /* Destroy "struct bridge"s, "struct port"s, and "struct iface"s according
     575                 :            :      * to 'ovs_cfg', with only very minimal configuration otherwise.
     576                 :            :      *
     577                 :            :      * This is mostly an update to bridge data structures. Nothing is pushed
     578                 :            :      * down to ofproto or lower layers. */
     579                 :       4238 :     add_del_bridges(ovs_cfg);
     580 [ +  + ][ -  + ]:       8941 :     HMAP_FOR_EACH (br, node, &all_bridges) {
     581                 :       4703 :         bridge_collect_wanted_ports(br, &br->wanted_ports);
     582                 :       4703 :         bridge_del_ports(br, &br->wanted_ports);
     583                 :            :     }
     584                 :            : 
     585                 :            :     /* Start pushing configuration changes down to the ofproto layer:
     586                 :            :      *
     587                 :            :      *   - Delete ofprotos that are no longer configured.
     588                 :            :      *
     589                 :            :      *   - Delete ports that are no longer configured.
     590                 :            :      *
     591                 :            :      *   - Reconfigure existing ports to their desired configurations, or
     592                 :            :      *     delete them if not possible.
     593                 :            :      *
     594                 :            :      * We have to do all the deletions before we can do any additions, because
     595                 :            :      * the ports to be added might require resources that will be freed up by
     596                 :            :      * deletions (they might especially overlap in name). */
     597                 :       4238 :     bridge_delete_ofprotos();
     598 [ +  + ][ -  + ]:       8941 :     HMAP_FOR_EACH (br, node, &all_bridges) {
     599         [ +  + ]:       4703 :         if (br->ofproto) {
     600                 :       3951 :             bridge_delete_or_reconfigure_ports(br);
     601                 :            :         }
     602                 :            :     }
     603                 :            : 
     604                 :            :     /* Finish pushing configuration changes to the ofproto layer:
     605                 :            :      *
     606                 :            :      *     - Create ofprotos that are missing.
     607                 :            :      *
     608                 :            :      *     - Add ports that are missing. */
     609 [ +  + ][ -  + ]:       8941 :     HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
                 [ +  + ]
     610         [ +  + ]:       4703 :         if (!br->ofproto) {
     611                 :            :             int error;
     612                 :            : 
     613                 :        752 :             error = ofproto_create(br->name, br->type, &br->ofproto);
     614         [ +  + ]:        752 :             if (error) {
     615         [ +  - ]:          3 :                 VLOG_ERR("failed to create bridge %s: %s", br->name,
     616                 :            :                          ovs_strerror(error));
     617                 :          3 :                 shash_destroy(&br->wanted_ports);
     618                 :          3 :                 bridge_destroy(br, true);
     619                 :            :             } else {
     620                 :            :                 /* Trigger storing datapath version. */
     621                 :        749 :                 seq_change(connectivity_seq_get());
     622                 :            :             }
     623                 :            :         }
     624                 :            :     }
     625 [ +  + ][ -  + ]:       8938 :     HMAP_FOR_EACH (br, node, &all_bridges) {
     626                 :       4700 :         bridge_add_ports(br, &br->wanted_ports);
     627                 :       4700 :         shash_destroy(&br->wanted_ports);
     628                 :            :     }
     629                 :            : 
     630                 :       4238 :     reconfigure_system_stats(ovs_cfg);
     631                 :            : 
     632                 :            :     /* Complete the configuration. */
     633                 :       4238 :     sflow_bridge_number = 0;
     634                 :       4238 :     collect_in_band_managers(ovs_cfg, &managers, &n_managers);
     635 [ +  + ][ -  + ]:       8938 :     HMAP_FOR_EACH (br, node, &all_bridges) {
     636                 :            :         struct port *port;
     637                 :            : 
     638                 :            :         /* We need the datapath ID early to allow LACP ports to use it as the
     639                 :            :          * default system ID. */
     640                 :       4700 :         bridge_configure_datapath_id(br);
     641                 :            : 
     642 [ +  + ][ -  + ]:      36502 :         HMAP_FOR_EACH (port, hmap_node, &br->ports) {
     643                 :            :             struct iface *iface;
     644                 :            : 
     645                 :      31802 :             port_configure(port);
     646                 :            : 
     647         [ +  + ]:      63639 :             LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
     648                 :      31837 :                 iface_set_ofport(iface->cfg, iface->ofp_port);
     649                 :            :                 /* Clear eventual previous errors */
     650                 :      31837 :                 ovsrec_interface_set_error(iface->cfg, NULL);
     651                 :      31837 :                 iface_configure_cfm(iface);
     652                 :      31837 :                 iface_configure_qos(iface, port->cfg->qos);
     653                 :      31837 :                 iface_set_mac(br, port, iface);
     654                 :      31837 :                 ofproto_port_set_bfd(br->ofproto, iface->ofp_port,
     655                 :      31837 :                                      &iface->cfg->bfd);
     656                 :      31837 :                 ofproto_port_set_lldp(br->ofproto, iface->ofp_port,
     657                 :      31837 :                                       &iface->cfg->lldp);
     658                 :      31837 :                 ofproto_port_set_config(br->ofproto, iface->ofp_port,
     659                 :      31837 :                                         &iface->cfg->other_config);
     660                 :            :             }
     661                 :            :         }
     662                 :       4700 :         bridge_configure_mirrors(br);
     663                 :       4700 :         bridge_configure_forward_bpdu(br);
     664                 :       4700 :         bridge_configure_mac_table(br);
     665                 :       4700 :         bridge_configure_mcast_snooping(br);
     666                 :       4700 :         bridge_configure_remotes(br, managers, n_managers);
     667                 :       4700 :         bridge_configure_netflow(br);
     668                 :       4700 :         bridge_configure_sflow(br, &sflow_bridge_number);
     669                 :       4700 :         bridge_configure_ipfix(br);
     670                 :       4700 :         bridge_configure_spanning_tree(br);
     671                 :       4700 :         bridge_configure_tables(br);
     672                 :       4700 :         bridge_configure_dp_desc(br);
     673                 :       4700 :         bridge_configure_aa(br);
     674                 :            :     }
     675                 :       4238 :     free(managers);
     676                 :            : 
     677                 :            :     /* The ofproto-dpif provider does some final reconfiguration in its
     678                 :            :      * ->type_run() function.  We have to call it before notifying the database
     679                 :            :      * client that reconfiguration is complete, otherwise there is a very
     680                 :            :      * narrow race window in which e.g. ofproto/trace will not recognize the
     681                 :            :      * new configuration (sometimes this causes unit test failures). */
     682                 :       4238 :     bridge_run__();
     683                 :       4238 : }
     684                 :            : 
     685                 :            : /* Delete ofprotos which aren't configured or have the wrong type.  Create
     686                 :            :  * ofprotos which don't exist but need to. */
     687                 :            : static void
     688                 :       4238 : bridge_delete_ofprotos(void)
     689                 :            : {
     690                 :            :     struct bridge *br;
     691                 :            :     struct sset names;
     692                 :            :     struct sset types;
     693                 :            :     const char *type;
     694                 :            : 
     695                 :            :     /* Delete ofprotos with no bridge or with the wrong type. */
     696                 :       4238 :     sset_init(&names);
     697                 :       4238 :     sset_init(&types);
     698                 :       4238 :     ofproto_enumerate_types(&types);
     699 [ +  - ][ +  + ]:      12646 :     SSET_FOR_EACH (type, &types) {
                 [ +  + ]
     700                 :            :         const char *name;
     701                 :            : 
     702                 :       8408 :         ofproto_enumerate_names(type, &names);
     703 [ +  + ][ +  + ]:      12359 :         SSET_FOR_EACH (name, &names) {
                 [ +  + ]
     704                 :       3951 :             br = bridge_lookup(name);
     705 [ +  - ][ -  + ]:       3951 :             if (!br || strcmp(type, br->type)) {
     706                 :          0 :                 ofproto_delete(name, type);
     707                 :            :             }
     708                 :            :         }
     709                 :            :     }
     710                 :       4238 :     sset_destroy(&names);
     711                 :       4238 :     sset_destroy(&types);
     712                 :       4238 : }
     713                 :            : 
     714                 :            : static ofp_port_t *
     715                 :        388 : add_ofp_port(ofp_port_t port, ofp_port_t *ports, size_t *n, size_t *allocated)
     716                 :            : {
     717         [ +  + ]:        388 :     if (*n >= *allocated) {
     718                 :        364 :         ports = x2nrealloc(ports, allocated, sizeof *ports);
     719                 :            :     }
     720                 :        388 :     ports[(*n)++] = port;
     721                 :        388 :     return ports;
     722                 :            : }
     723                 :            : 
     724                 :            : /* Configures the MTU of 'netdev' based on the "mtu_request" column
     725                 :            :  * in 'iface_cfg'. */
     726                 :            : static int
     727                 :      31998 : iface_set_netdev_mtu(const struct ovsrec_interface *iface_cfg,
     728                 :            :                      struct netdev *netdev)
     729                 :            : {
     730         [ +  + ]:      31998 :     if (iface_cfg->n_mtu_request == 1) {
     731                 :            :         /* The user explicitly asked for this MTU. */
     732                 :          7 :         netdev_mtu_user_config(netdev, true);
     733                 :            :         /* Try to set the MTU to the requested value. */
     734                 :          7 :         return netdev_set_mtu(netdev, *iface_cfg->mtu_request);
     735                 :            :     }
     736                 :            : 
     737                 :            :     /* The user didn't explicitly asked for any MTU. */
     738                 :      31991 :     netdev_mtu_user_config(netdev, false);
     739                 :      31991 :     return 0;
     740                 :            : }
     741                 :            : 
     742                 :            : static void
     743                 :       3951 : bridge_delete_or_reconfigure_ports(struct bridge *br)
     744                 :            : {
     745                 :            :     struct ofproto_port ofproto_port;
     746                 :            :     struct ofproto_port_dump dump;
     747                 :            : 
     748                 :            :     struct sset ofproto_ports;
     749                 :            :     struct port *port, *port_next;
     750                 :            : 
     751                 :            :     /* List of "ofp_port"s to delete.  We make a list instead of deleting them
     752                 :            :      * right away because ofproto implementations aren't necessarily able to
     753                 :            :      * iterate through a changing list of ports in an entirely robust way. */
     754                 :            :     ofp_port_t *del;
     755                 :            :     size_t n, allocated;
     756                 :            :     size_t i;
     757                 :            : 
     758                 :       3951 :     del = NULL;
     759                 :       3951 :     n = allocated = 0;
     760                 :       3951 :     sset_init(&ofproto_ports);
     761                 :            : 
     762                 :            :     /* Main task: Iterate over the ports in 'br->ofproto' and remove the ports
     763                 :            :      * that are not configured in the database.  (This commonly happens when
     764                 :            :      * ports have been deleted, e.g. with "ovs-vsctl del-port".)
     765                 :            :      *
     766                 :            :      * Side tasks: Reconfigure the ports that are still in 'br'.  Delete ports
     767                 :            :      * that have the wrong OpenFlow port number (and arrange to add them back
     768                 :            :      * with the correct OpenFlow port number). */
     769 [ +  + ][ +  + ]:      33389 :     OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
     770                 :            :         ofp_port_t requested_ofp_port;
     771                 :            :         struct iface *iface;
     772                 :            : 
     773                 :      29438 :         sset_add(&ofproto_ports, ofproto_port.name);
     774                 :            : 
     775                 :      29438 :         iface = iface_lookup(br, ofproto_port.name);
     776         [ +  + ]:      29438 :         if (!iface) {
     777                 :            :             /* No such iface is configured, so we should delete this
     778                 :            :              * ofproto_port.
     779                 :            :              *
     780                 :            :              * As a corner case exception, keep the port if it's a bond fake
     781                 :            :              * interface. */
     782         [ -  + ]:        121 :             if (bridge_has_bond_fake_iface(br, ofproto_port.name)
     783         [ #  # ]:          0 :                 && !strcmp(ofproto_port.type, "internal")) {
     784                 :          0 :                 continue;
     785                 :            :             }
     786                 :        121 :             goto delete;
     787                 :            :         }
     788                 :            : 
     789         [ +  + ]:      29317 :         if (strcmp(ofproto_port.type, iface->netdev_type)
     790         [ +  - ]:      29308 :             || netdev_set_config(iface->netdev, &iface->cfg->options, NULL)) {
     791                 :            :             /* The interface is the wrong type or can't be configured.
     792                 :            :              * Delete it. */
     793                 :            :             goto delete;
     794                 :            :         }
     795                 :            : 
     796                 :      29308 :         iface_set_netdev_mtu(iface->cfg, iface->netdev);
     797                 :            : 
     798                 :            :         /* If the requested OpenFlow port for 'iface' changed, and it's not
     799                 :            :          * already the correct port, then we might want to temporarily delete
     800                 :            :          * this interface, so we can add it back again with the new OpenFlow
     801                 :            :          * port number. */
     802                 :      29308 :         requested_ofp_port = iface_get_requested_ofp_port(iface->cfg);
     803 [ +  + ][ +  + ]:      29308 :         if (iface->ofp_port != OFPP_LOCAL &&
     804         [ +  + ]:       1351 :             requested_ofp_port != OFPP_NONE &&
     805                 :       1351 :             requested_ofp_port != iface->ofp_port) {
     806                 :            :             ofp_port_t victim_request;
     807                 :            :             struct iface *victim;
     808                 :            : 
     809                 :            :             /* Check for an existing OpenFlow port currently occupying
     810                 :            :              * 'iface''s requested port number.  If there isn't one, then
     811                 :            :              * delete this port.  Otherwise we need to consider further. */
     812                 :        184 :             victim = iface_from_ofp_port(br, requested_ofp_port);
     813         [ +  + ]:        184 :             if (!victim) {
     814                 :         46 :                 goto delete;
     815                 :            :             }
     816                 :            : 
     817                 :            :             /* 'victim' is a port currently using 'iface''s requested port
     818                 :            :              * number.  Unless 'victim' specifically requested that port
     819                 :            :              * number, too, then we can delete both 'iface' and 'victim'
     820                 :            :              * temporarily.  (We'll add both of them back again later with new
     821                 :            :              * OpenFlow port numbers.)
     822                 :            :              *
     823                 :            :              * If 'victim' did request port number 'requested_ofp_port', just
     824                 :            :              * like 'iface', then that's a configuration inconsistency that we
     825                 :            :              * can't resolve.  We might as well let it keep its current port
     826                 :            :              * number. */
     827                 :        138 :             victim_request = iface_get_requested_ofp_port(victim->cfg);
     828         [ +  + ]:        138 :             if (victim_request != requested_ofp_port) {
     829                 :        106 :                 del = add_ofp_port(victim->ofp_port, del, &n, &allocated);
     830                 :        106 :                 iface_destroy(victim);
     831                 :        106 :                 goto delete;
     832                 :            :             }
     833                 :            :         }
     834                 :            : 
     835                 :            :         /* Keep it. */
     836                 :      29156 :         continue;
     837                 :            : 
     838                 :            :     delete:
     839                 :        282 :         iface_destroy(iface);
     840                 :        282 :         del = add_ofp_port(ofproto_port.ofp_port, del, &n, &allocated);
     841                 :            :     }
     842         [ +  + ]:       4339 :     for (i = 0; i < n; i++) {
     843                 :        388 :         ofproto_port_del(br->ofproto, del[i]);
     844                 :            :     }
     845                 :       3951 :     free(del);
     846                 :            : 
     847                 :            :     /* Iterate over this module's idea of interfaces in 'br'.  Remove any ports
     848                 :            :      * that we didn't see when we iterated through the datapath, i.e. ports
     849                 :            :      * that disappeared underneath use.  This is an unusual situation, but it
     850                 :            :      * can happen in some cases:
     851                 :            :      *
     852                 :            :      *     - An admin runs a command like "ovs-dpctl del-port" (which is a bad
     853                 :            :      *       idea but could happen).
     854                 :            :      *
     855                 :            :      *     - The port represented a device that disappeared, e.g. a tuntap
     856                 :            :      *       device destroyed via "tunctl -d", a physical Ethernet device
     857                 :            :      *       whose module was just unloaded via "rmmod", or a virtual NIC for a
     858                 :            :      *       VM whose VM was just terminated. */
     859 [ +  + ][ -  + ]:      33087 :     HMAP_FOR_EACH_SAFE (port, port_next, hmap_node, &br->ports) {
                 [ +  + ]
     860                 :            :         struct iface *iface, *iface_next;
     861                 :            : 
     862 [ +  + ][ +  + ]:      58285 :         LIST_FOR_EACH_SAFE (iface, iface_next, port_elem, &port->ifaces) {
     863         [ +  + ]:      29149 :             if (!sset_contains(&ofproto_ports, iface->name)) {
     864                 :          1 :                 iface_destroy__(iface);
     865                 :            :             }
     866                 :            :         }
     867                 :            : 
     868         [ +  + ]:      29136 :         if (ovs_list_is_empty(&port->ifaces)) {
     869                 :          1 :             port_destroy(port);
     870                 :            :         }
     871                 :            :     }
     872                 :       3951 :     sset_destroy(&ofproto_ports);
     873                 :       3951 : }
     874                 :            : 
     875                 :            : static void
     876                 :       9400 : bridge_add_ports__(struct bridge *br, const struct shash *wanted_ports,
     877                 :            :                    bool with_requested_port)
     878                 :            : {
     879                 :            :     struct shash_node *port_node;
     880                 :            : 
     881 [ +  + ][ -  + ]:      73026 :     SHASH_FOR_EACH (port_node, wanted_ports) {
     882                 :      63626 :         const struct ovsrec_port *port_cfg = port_node->data;
     883                 :            :         size_t i;
     884                 :            : 
     885         [ +  + ]:     127322 :         for (i = 0; i < port_cfg->n_interfaces; i++) {
     886                 :      63696 :             const struct ovsrec_interface *iface_cfg = port_cfg->interfaces[i];
     887                 :            :             ofp_port_t requested_ofp_port;
     888                 :            : 
     889                 :      63696 :             requested_ofp_port = iface_get_requested_ofp_port(iface_cfg);
     890         [ +  + ]:      63696 :             if ((requested_ofp_port != OFPP_NONE) == with_requested_port) {
     891                 :      31848 :                 struct iface *iface = iface_lookup(br, iface_cfg->name);
     892                 :            : 
     893         [ +  + ]:      31848 :                 if (!iface) {
     894                 :       2700 :                     iface_create(br, iface_cfg, port_cfg);
     895                 :            :                 }
     896                 :            :             }
     897                 :            :         }
     898                 :            :     }
     899                 :       9400 : }
     900                 :            : 
     901                 :            : static void
     902                 :       4700 : bridge_add_ports(struct bridge *br, const struct shash *wanted_ports)
     903                 :            : {
     904                 :            :     /* First add interfaces that request a particular port number. */
     905                 :       4700 :     bridge_add_ports__(br, wanted_ports, true);
     906                 :            : 
     907                 :            :     /* Then add interfaces that want automatic port number assignment.
     908                 :            :      * We add these afterward to avoid accidentally taking a specifically
     909                 :            :      * requested port number. */
     910                 :       4700 :     bridge_add_ports__(br, wanted_ports, false);
     911                 :       4700 : }
     912                 :            : 
     913                 :            : static void
     914                 :      31802 : port_configure(struct port *port)
     915                 :            : {
     916                 :      31802 :     const struct ovsrec_port *cfg = port->cfg;
     917                 :            :     struct bond_settings bond_settings;
     918                 :            :     struct lacp_settings lacp_settings;
     919                 :            :     struct ofproto_bundle_settings s;
     920                 :            :     struct iface *iface;
     921                 :            : 
     922                 :            :     /* Get name. */
     923                 :      31802 :     s.name = port->name;
     924                 :            : 
     925                 :            :     /* Get slaves. */
     926                 :      31802 :     s.n_slaves = 0;
     927                 :      31802 :     s.slaves = xmalloc(ovs_list_size(&port->ifaces) * sizeof *s.slaves);
     928         [ +  + ]:      63639 :     LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
     929                 :      31837 :         s.slaves[s.n_slaves++] = iface->ofp_port;
     930                 :            :     }
     931                 :            : 
     932                 :            :     /* Get VLAN tag. */
     933                 :      31802 :     s.vlan = -1;
     934 [ +  + ][ +  - ]:      31802 :     if (cfg->tag && *cfg->tag >= 0 && *cfg->tag <= 4095) {
                 [ +  - ]
     935                 :         15 :         s.vlan = *cfg->tag;
     936                 :            :     }
     937                 :            : 
     938                 :            :     /* Get VLAN trunks. */
     939                 :      31802 :     s.trunks = NULL;
     940         [ +  + ]:      31802 :     if (cfg->n_trunks) {
     941                 :          3 :         s.trunks = vlan_bitmap_from_array(cfg->trunks, cfg->n_trunks);
     942                 :            :     }
     943                 :            : 
     944                 :            :     /* Get VLAN mode. */
     945         [ +  + ]:      31802 :     if (cfg->vlan_mode) {
     946         [ -  + ]:          4 :         if (!strcmp(cfg->vlan_mode, "access")) {
     947                 :          0 :             s.vlan_mode = PORT_VLAN_ACCESS;
     948         [ -  + ]:          4 :         } else if (!strcmp(cfg->vlan_mode, "trunk")) {
     949                 :          0 :             s.vlan_mode = PORT_VLAN_TRUNK;
     950         [ +  + ]:          4 :         } else if (!strcmp(cfg->vlan_mode, "native-tagged")) {
     951                 :          2 :             s.vlan_mode = PORT_VLAN_NATIVE_TAGGED;
     952         [ +  - ]:          2 :         } else if (!strcmp(cfg->vlan_mode, "native-untagged")) {
     953                 :          2 :             s.vlan_mode = PORT_VLAN_NATIVE_UNTAGGED;
     954                 :            :         } else {
     955                 :            :             /* This "can't happen" because ovsdb-server should prevent it. */
     956         [ #  # ]:          0 :             VLOG_WARN("port %s: unknown VLAN mode %s, falling "
     957                 :            :                       "back to trunk mode", port->name, cfg->vlan_mode);
     958                 :          4 :             s.vlan_mode = PORT_VLAN_TRUNK;
     959                 :            :         }
     960                 :            :     } else {
     961         [ +  + ]:      31798 :         if (s.vlan >= 0) {
     962                 :         11 :             s.vlan_mode = PORT_VLAN_ACCESS;
     963         [ -  + ]:         11 :             if (cfg->n_trunks) {
     964         [ #  # ]:         11 :                 VLOG_WARN("port %s: ignoring trunks in favor of implicit vlan",
     965                 :            :                           port->name);
     966                 :            :             }
     967                 :            :         } else {
     968                 :      31787 :             s.vlan_mode = PORT_VLAN_TRUNK;
     969                 :            :         }
     970                 :            :     }
     971                 :      31802 :     s.use_priority_tags = smap_get_bool(&cfg->other_config, "priority-tags",
     972                 :            :                                         false);
     973                 :            : 
     974                 :            :     /* Get LACP settings. */
     975                 :      31802 :     s.lacp = port_configure_lacp(port, &lacp_settings);
     976         [ +  + ]:      31802 :     if (s.lacp) {
     977                 :         20 :         size_t i = 0;
     978                 :            : 
     979                 :         20 :         s.lacp_slaves = xmalloc(s.n_slaves * sizeof *s.lacp_slaves);
     980         [ +  + ]:         61 :         LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
     981                 :         41 :             iface_configure_lacp(iface, &s.lacp_slaves[i++]);
     982                 :            :         }
     983                 :            :     } else {
     984                 :      31782 :         s.lacp_slaves = NULL;
     985                 :            :     }
     986                 :            : 
     987                 :            :     /* Get bond settings. */
     988         [ +  + ]:      31802 :     if (s.n_slaves > 1) {
     989                 :         32 :         s.bond = &bond_settings;
     990                 :         32 :         port_configure_bond(port, &bond_settings);
     991                 :            :     } else {
     992                 :      31770 :         s.bond = NULL;
     993         [ +  + ]:      63540 :         LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
     994                 :      31770 :             netdev_set_miimon_interval(iface->netdev, 0);
     995                 :            :         }
     996                 :            :     }
     997                 :            : 
     998                 :            :     /* Register. */
     999                 :      31802 :     ofproto_bundle_register(port->bridge->ofproto, port, &s);
    1000                 :            : 
    1001                 :            :     /* Clean up. */
    1002                 :      31802 :     free(s.slaves);
    1003                 :      31802 :     free(s.trunks);
    1004                 :      31802 :     free(s.lacp_slaves);
    1005                 :      31802 : }
    1006                 :            : 
    1007                 :            : /* Pick local port hardware address and datapath ID for 'br'. */
    1008                 :            : static void
    1009                 :       4700 : bridge_configure_datapath_id(struct bridge *br)
    1010                 :            : {
    1011                 :            :     struct eth_addr ea;
    1012                 :            :     uint64_t dpid;
    1013                 :            :     struct iface *local_iface;
    1014                 :            :     struct iface *hw_addr_iface;
    1015                 :            :     char *dpid_string;
    1016                 :            : 
    1017                 :       4700 :     bridge_pick_local_hw_addr(br, &ea, &hw_addr_iface);
    1018                 :       4700 :     local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
    1019         [ +  - ]:       4700 :     if (local_iface) {
    1020                 :       4700 :         int error = netdev_set_etheraddr(local_iface->netdev, ea);
    1021         [ -  + ]:       4700 :         if (error) {
    1022                 :            :             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
    1023         [ #  # ]:          0 :             VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
    1024                 :            :                         "Ethernet address: %s",
    1025                 :            :                         br->name, ovs_strerror(error));
    1026                 :            :         }
    1027                 :            :     }
    1028                 :       4700 :     br->ea = ea;
    1029                 :            : 
    1030                 :       4700 :     dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
    1031         [ +  + ]:       4700 :     if (dpid != ofproto_get_datapath_id(br->ofproto)) {
    1032         [ +  - ]:        749 :         VLOG_INFO("bridge %s: using datapath ID %016"PRIx64, br->name, dpid);
    1033                 :        749 :         ofproto_set_datapath_id(br->ofproto, dpid);
    1034                 :            :     }
    1035                 :            : 
    1036                 :       4700 :     dpid_string = xasprintf("%016"PRIx64, dpid);
    1037                 :       4700 :     ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
    1038                 :       4700 :     free(dpid_string);
    1039                 :       4700 : }
    1040                 :            : 
    1041                 :            : /* Returns a bitmap of "enum ofputil_protocol"s that are allowed for use with
    1042                 :            :  * 'br'. */
    1043                 :            : static uint32_t
    1044                 :       4700 : bridge_get_allowed_versions(struct bridge *br)
    1045                 :            : {
    1046         [ +  + ]:       4700 :     if (!br->cfg->n_protocols) {
    1047                 :       1238 :         return 0;
    1048                 :            :     }
    1049                 :            : 
    1050                 :       3462 :     return ofputil_versions_from_strings(br->cfg->protocols,
    1051                 :       3462 :                                          br->cfg->n_protocols);
    1052                 :            : }
    1053                 :            : 
    1054                 :            : /* Set NetFlow configuration on 'br'. */
    1055                 :            : static void
    1056                 :       4700 : bridge_configure_netflow(struct bridge *br)
    1057                 :            : {
    1058                 :       4700 :     struct ovsrec_netflow *cfg = br->cfg->netflow;
    1059                 :            :     struct netflow_options opts;
    1060                 :            : 
    1061         [ +  + ]:       4700 :     if (!cfg) {
    1062                 :       4694 :         ofproto_set_netflow(br->ofproto, NULL);
    1063                 :       4694 :         return;
    1064                 :            :     }
    1065                 :            : 
    1066                 :          6 :     memset(&opts, 0, sizeof opts);
    1067                 :            : 
    1068                 :            :     /* Get default NetFlow configuration from datapath.
    1069                 :            :      * Apply overrides from 'cfg'. */
    1070                 :          6 :     ofproto_get_netflow_ids(br->ofproto, &opts.engine_type, &opts.engine_id);
    1071         [ +  - ]:          6 :     if (cfg->engine_type) {
    1072                 :          6 :         opts.engine_type = *cfg->engine_type;
    1073                 :            :     }
    1074         [ +  - ]:          6 :     if (cfg->engine_id) {
    1075                 :          6 :         opts.engine_id = *cfg->engine_id;
    1076                 :            :     }
    1077                 :            : 
    1078                 :            :     /* Configure active timeout interval. */
    1079                 :          6 :     opts.active_timeout = cfg->active_timeout;
    1080         [ -  + ]:          6 :     if (!opts.active_timeout) {
    1081                 :          0 :         opts.active_timeout = -1;
    1082         [ -  + ]:          6 :     } else if (opts.active_timeout < 0) {
    1083         [ #  # ]:          0 :         VLOG_WARN("bridge %s: active timeout interval set to negative "
    1084                 :            :                   "value, using default instead (%d seconds)", br->name,
    1085                 :            :                   NF_ACTIVE_TIMEOUT_DEFAULT);
    1086                 :          0 :         opts.active_timeout = -1;
    1087                 :            :     }
    1088                 :            : 
    1089                 :            :     /* Add engine ID to interface number to disambiguate bridgs? */
    1090                 :          6 :     opts.add_id_to_iface = cfg->add_id_to_interface;
    1091         [ -  + ]:          6 :     if (opts.add_id_to_iface) {
    1092         [ #  # ]:          0 :         if (opts.engine_id > 0x7f) {
    1093         [ #  # ]:          0 :             VLOG_WARN("bridge %s: NetFlow port mangling may conflict with "
    1094                 :            :                       "another vswitch, choose an engine id less than 128",
    1095                 :            :                       br->name);
    1096                 :            :         }
    1097         [ #  # ]:          0 :         if (hmap_count(&br->ports) > 508) {
    1098         [ #  # ]:          0 :             VLOG_WARN("bridge %s: NetFlow port mangling will conflict with "
    1099                 :            :                       "another port when more than 508 ports are used",
    1100                 :            :                       br->name);
    1101                 :            :         }
    1102                 :            :     }
    1103                 :            : 
    1104                 :            :     /* Collectors. */
    1105                 :          6 :     sset_init(&opts.collectors);
    1106                 :          6 :     sset_add_array(&opts.collectors, cfg->targets, cfg->n_targets);
    1107                 :            : 
    1108                 :            :     /* Configure. */
    1109         [ -  + ]:          6 :     if (ofproto_set_netflow(br->ofproto, &opts)) {
    1110         [ #  # ]:          0 :         VLOG_ERR("bridge %s: problem setting netflow collectors", br->name);
    1111                 :            :     }
    1112                 :          6 :     sset_destroy(&opts.collectors);
    1113                 :            : }
    1114                 :            : 
    1115                 :            : /* Set sFlow configuration on 'br'. */
    1116                 :            : static void
    1117                 :       4700 : bridge_configure_sflow(struct bridge *br, int *sflow_bridge_number)
    1118                 :            : {
    1119                 :       4700 :     const struct ovsrec_sflow *cfg = br->cfg->sflow;
    1120                 :            :     struct ovsrec_controller **controllers;
    1121                 :            :     struct ofproto_sflow_options oso;
    1122                 :            :     size_t n_controllers;
    1123                 :            :     size_t i;
    1124                 :            : 
    1125         [ +  + ]:       4700 :     if (!cfg) {
    1126                 :       4694 :         ofproto_set_sflow(br->ofproto, NULL);
    1127                 :       4694 :         return;
    1128                 :            :     }
    1129                 :            : 
    1130                 :          6 :     memset(&oso, 0, sizeof oso);
    1131                 :            : 
    1132                 :          6 :     sset_init(&oso.targets);
    1133                 :          6 :     sset_add_array(&oso.targets, cfg->targets, cfg->n_targets);
    1134                 :            : 
    1135                 :          6 :     oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
    1136         [ +  - ]:          6 :     if (cfg->sampling) {
    1137                 :          6 :         oso.sampling_rate = *cfg->sampling;
    1138                 :            :     }
    1139                 :            : 
    1140                 :          6 :     oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
    1141         [ +  - ]:          6 :     if (cfg->polling) {
    1142                 :          6 :         oso.polling_interval = *cfg->polling;
    1143                 :            :     }
    1144                 :            : 
    1145                 :          6 :     oso.header_len = SFL_DEFAULT_HEADER_SIZE;
    1146         [ +  - ]:          6 :     if (cfg->header) {
    1147                 :          6 :         oso.header_len = *cfg->header;
    1148                 :            :     }
    1149                 :            : 
    1150                 :          6 :     oso.sub_id = (*sflow_bridge_number)++;
    1151                 :          6 :     oso.agent_device = cfg->agent;
    1152                 :            : 
    1153                 :          6 :     oso.control_ip = NULL;
    1154                 :          6 :     n_controllers = bridge_get_controllers(br, &controllers);
    1155         [ -  + ]:          6 :     for (i = 0; i < n_controllers; i++) {
    1156         [ #  # ]:          0 :         if (controllers[i]->local_ip) {
    1157                 :          0 :             oso.control_ip = controllers[i]->local_ip;
    1158                 :          0 :             break;
    1159                 :            :         }
    1160                 :            :     }
    1161                 :          6 :     ofproto_set_sflow(br->ofproto, &oso);
    1162                 :            : 
    1163                 :          6 :     sset_destroy(&oso.targets);
    1164                 :            : }
    1165                 :            : 
    1166                 :            : /* Returns whether a IPFIX row is valid. */
    1167                 :            : static bool
    1168                 :       4706 : ovsrec_ipfix_is_valid(const struct ovsrec_ipfix *ipfix)
    1169                 :            : {
    1170 [ +  + ][ +  - ]:       4706 :     return ipfix && ipfix->n_targets > 0;
    1171                 :            : }
    1172                 :            : 
    1173                 :            : /* Returns whether a Flow_Sample_Collector_Set row is valid. */
    1174                 :            : static bool
    1175                 :          6 : ovsrec_fscs_is_valid(const struct ovsrec_flow_sample_collector_set *fscs,
    1176                 :            :                      const struct bridge *br)
    1177                 :            : {
    1178 [ +  - ][ +  - ]:          6 :     return ovsrec_ipfix_is_valid(fscs->ipfix) && fscs->bridge == br->cfg;
    1179                 :            : }
    1180                 :            : 
    1181                 :            : /* Set IPFIX configuration on 'br'. */
    1182                 :            : static void
    1183                 :       4700 : bridge_configure_ipfix(struct bridge *br)
    1184                 :            : {
    1185                 :       4700 :     const struct ovsrec_ipfix *be_cfg = br->cfg->ipfix;
    1186                 :       4700 :     bool valid_be_cfg = ovsrec_ipfix_is_valid(be_cfg);
    1187                 :            :     const struct ovsrec_flow_sample_collector_set *fe_cfg;
    1188                 :            :     struct ofproto_ipfix_bridge_exporter_options be_opts;
    1189                 :       4700 :     struct ofproto_ipfix_flow_exporter_options *fe_opts = NULL;
    1190                 :       4700 :     size_t n_fe_opts = 0;
    1191                 :            :     const char *virtual_obs_id;
    1192                 :            : 
    1193         [ +  + ]:       4703 :     OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH(fe_cfg, idl) {
    1194         [ +  - ]:          3 :         if (ovsrec_fscs_is_valid(fe_cfg, br)) {
    1195                 :          3 :             n_fe_opts++;
    1196                 :            :         }
    1197                 :            :     }
    1198                 :            : 
    1199 [ +  + ][ +  + ]:       4700 :     if (!valid_be_cfg && n_fe_opts == 0) {
    1200                 :       4695 :         ofproto_set_ipfix(br->ofproto, NULL, NULL, 0);
    1201                 :       4695 :         return;
    1202                 :            :     }
    1203                 :            : 
    1204         [ +  + ]:          5 :     if (valid_be_cfg) {
    1205                 :          2 :         memset(&be_opts, 0, sizeof be_opts);
    1206                 :            : 
    1207                 :          2 :         sset_init(&be_opts.targets);
    1208                 :          2 :         sset_add_array(&be_opts.targets, be_cfg->targets, be_cfg->n_targets);
    1209                 :            : 
    1210         [ +  - ]:          2 :         if (be_cfg->sampling) {
    1211                 :          2 :             be_opts.sampling_rate = *be_cfg->sampling;
    1212                 :            :         } else {
    1213                 :          0 :             be_opts.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
    1214                 :            :         }
    1215         [ -  + ]:          2 :         if (be_cfg->obs_domain_id) {
    1216                 :          0 :             be_opts.obs_domain_id = *be_cfg->obs_domain_id;
    1217                 :            :         }
    1218         [ -  + ]:          2 :         if (be_cfg->obs_point_id) {
    1219                 :          0 :             be_opts.obs_point_id = *be_cfg->obs_point_id;
    1220                 :            :         }
    1221         [ -  + ]:          2 :         if (be_cfg->cache_active_timeout) {
    1222                 :          0 :             be_opts.cache_active_timeout = *be_cfg->cache_active_timeout;
    1223                 :            :         }
    1224         [ -  + ]:          2 :         if (be_cfg->cache_max_flows) {
    1225                 :          0 :             be_opts.cache_max_flows = *be_cfg->cache_max_flows;
    1226                 :            :         }
    1227                 :            : 
    1228                 :          2 :         be_opts.enable_tunnel_sampling = smap_get_bool(&be_cfg->other_config,
    1229                 :            :                                              "enable-tunnel-sampling", true);
    1230                 :            : 
    1231                 :          2 :         be_opts.enable_input_sampling = !smap_get_bool(&be_cfg->other_config,
    1232                 :          2 :                                               "enable-input-sampling", false);
    1233                 :            : 
    1234                 :          2 :         be_opts.enable_output_sampling = !smap_get_bool(&be_cfg->other_config,
    1235                 :          2 :                                               "enable-output-sampling", false);
    1236                 :            : 
    1237                 :          2 :         virtual_obs_id = smap_get(&be_cfg->other_config, "virtual_obs_id");
    1238                 :          2 :         be_opts.virtual_obs_id = nullable_xstrdup(virtual_obs_id);
    1239                 :            :     }
    1240                 :            : 
    1241         [ +  + ]:          5 :     if (n_fe_opts > 0) {
    1242                 :            :         struct ofproto_ipfix_flow_exporter_options *opts;
    1243                 :          3 :         fe_opts = xcalloc(n_fe_opts, sizeof *fe_opts);
    1244                 :          3 :         opts = fe_opts;
    1245         [ +  + ]:          6 :         OVSREC_FLOW_SAMPLE_COLLECTOR_SET_FOR_EACH(fe_cfg, idl) {
    1246         [ +  - ]:          3 :             if (ovsrec_fscs_is_valid(fe_cfg, br)) {
    1247                 :          3 :                 opts->collector_set_id = fe_cfg->id;
    1248                 :          3 :                 sset_init(&opts->targets);
    1249                 :          3 :                 sset_add_array(&opts->targets, fe_cfg->ipfix->targets,
    1250                 :          3 :                                fe_cfg->ipfix->n_targets);
    1251         [ -  + ]:          3 :                 opts->cache_active_timeout = fe_cfg->ipfix->cache_active_timeout
    1252                 :          0 :                     ? *fe_cfg->ipfix->cache_active_timeout : 0;
    1253         [ -  + ]:          3 :                 opts->cache_max_flows = fe_cfg->ipfix->cache_max_flows
    1254                 :          0 :                     ? *fe_cfg->ipfix->cache_max_flows : 0;
    1255                 :          3 :                 opts->enable_tunnel_sampling = smap_get_bool(
    1256                 :          3 :                                                    &fe_cfg->ipfix->other_config,
    1257                 :            :                                                   "enable-tunnel-sampling", true);
    1258                 :          3 :                 virtual_obs_id = smap_get(&fe_cfg->ipfix->other_config,
    1259                 :            :                                           "virtual_obs_id");
    1260                 :          3 :                 opts->virtual_obs_id = nullable_xstrdup(virtual_obs_id);
    1261                 :          3 :                 opts++;
    1262                 :            :             }
    1263                 :            :         }
    1264                 :            :     }
    1265                 :            : 
    1266         [ +  + ]:          5 :     ofproto_set_ipfix(br->ofproto, valid_be_cfg ? &be_opts : NULL, fe_opts,
    1267                 :            :                       n_fe_opts);
    1268                 :            : 
    1269         [ +  + ]:          5 :     if (valid_be_cfg) {
    1270                 :          2 :         sset_destroy(&be_opts.targets);
    1271                 :          2 :         free(be_opts.virtual_obs_id);
    1272                 :            :     }
    1273                 :            : 
    1274         [ +  + ]:          5 :     if (n_fe_opts > 0) {
    1275                 :          3 :         struct ofproto_ipfix_flow_exporter_options *opts = fe_opts;
    1276                 :            :         size_t i;
    1277         [ +  + ]:          6 :         for (i = 0; i < n_fe_opts; i++) {
    1278                 :          3 :             sset_destroy(&opts->targets);
    1279                 :          3 :             free(opts->virtual_obs_id);
    1280                 :          3 :             opts++;
    1281                 :            :         }
    1282                 :          5 :         free(fe_opts);
    1283                 :            :     }
    1284                 :            : }
    1285                 :            : 
    1286                 :            : static void
    1287                 :         20 : port_configure_stp(const struct ofproto *ofproto, struct port *port,
    1288                 :            :                    struct ofproto_port_stp_settings *port_s,
    1289                 :            :                    int *port_num_counter, unsigned long *port_num_bitmap)
    1290                 :            : {
    1291                 :            :     const char *config_str;
    1292                 :            :     struct iface *iface;
    1293                 :            : 
    1294         [ +  + ]:         20 :     if (!smap_get_bool(&port->cfg->other_config, "stp-enable", true)) {
    1295                 :         14 :         port_s->enable = false;
    1296                 :         14 :         return;
    1297                 :            :     } else {
    1298                 :          6 :         port_s->enable = true;
    1299                 :            :     }
    1300                 :            : 
    1301                 :            :     /* STP over bonds is not supported. */
    1302         [ -  + ]:          6 :     if (!ovs_list_is_singleton(&port->ifaces)) {
    1303         [ #  # ]:          0 :         VLOG_ERR("port %s: cannot enable STP on bonds, disabling",
    1304                 :            :                  port->name);
    1305                 :          0 :         port_s->enable = false;
    1306                 :          0 :         return;
    1307                 :            :     }
    1308                 :            : 
    1309                 :          6 :     iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
    1310                 :            : 
    1311                 :            :     /* Internal ports shouldn't participate in spanning tree, so
    1312                 :            :      * skip them. */
    1313         [ -  + ]:          6 :     if (!strcmp(iface->type, "internal")) {
    1314         [ #  # ]:          0 :         VLOG_DBG("port %s: disable STP on internal ports", port->name);
    1315                 :          0 :         port_s->enable = false;
    1316                 :          0 :         return;
    1317                 :            :     }
    1318                 :            : 
    1319                 :            :     /* STP on mirror output ports is not supported. */
    1320         [ -  + ]:          6 :     if (ofproto_is_mirror_output_bundle(ofproto, port)) {
    1321         [ #  # ]:          0 :         VLOG_DBG("port %s: disable STP on mirror ports", port->name);
    1322                 :          0 :         port_s->enable = false;
    1323                 :          0 :         return;
    1324                 :            :     }
    1325                 :            : 
    1326                 :          6 :     config_str = smap_get(&port->cfg->other_config, "stp-port-num");
    1327         [ -  + ]:          6 :     if (config_str) {
    1328                 :          0 :         unsigned long int port_num = strtoul(config_str, NULL, 0);
    1329                 :          0 :         int port_idx = port_num - 1;
    1330                 :            : 
    1331 [ #  # ][ #  # ]:          0 :         if (port_num < 1 || port_num > STP_MAX_PORTS) {
    1332         [ #  # ]:          0 :             VLOG_ERR("port %s: invalid stp-port-num", port->name);
    1333                 :          0 :             port_s->enable = false;
    1334                 :          0 :             return;
    1335                 :            :         }
    1336                 :            : 
    1337         [ #  # ]:          0 :         if (bitmap_is_set(port_num_bitmap, port_idx)) {
    1338         [ #  # ]:          0 :             VLOG_ERR("port %s: duplicate stp-port-num %lu, disabling",
    1339                 :            :                     port->name, port_num);
    1340                 :          0 :             port_s->enable = false;
    1341                 :          0 :             return;
    1342                 :            :         }
    1343                 :          0 :         bitmap_set1(port_num_bitmap, port_idx);
    1344                 :          0 :         port_s->port_num = port_idx;
    1345                 :            :     } else {
    1346         [ -  + ]:          6 :         if (*port_num_counter >= STP_MAX_PORTS) {
    1347         [ #  # ]:          0 :             VLOG_ERR("port %s: too many STP ports, disabling", port->name);
    1348                 :          0 :             port_s->enable = false;
    1349                 :          0 :             return;
    1350                 :            :         }
    1351                 :            : 
    1352                 :          6 :         port_s->port_num = (*port_num_counter)++;
    1353                 :            :     }
    1354                 :            : 
    1355                 :          6 :     config_str = smap_get(&port->cfg->other_config, "stp-path-cost");
    1356         [ -  + ]:          6 :     if (config_str) {
    1357                 :          0 :         port_s->path_cost = strtoul(config_str, NULL, 10);
    1358                 :            :     } else {
    1359                 :            :         enum netdev_features current;
    1360                 :            :         unsigned int mbps;
    1361                 :            : 
    1362                 :          6 :         netdev_get_features(iface->netdev, &current, NULL, NULL, NULL);
    1363                 :          6 :         mbps = netdev_features_to_bps(current, 100 * 1000 * 1000) / 1000000;
    1364                 :          6 :         port_s->path_cost = stp_convert_speed_to_cost(mbps);
    1365                 :            :     }
    1366                 :            : 
    1367                 :          6 :     config_str = smap_get(&port->cfg->other_config, "stp-port-priority");
    1368         [ -  + ]:          6 :     if (config_str) {
    1369                 :          0 :         port_s->priority = strtoul(config_str, NULL, 0);
    1370                 :            :     } else {
    1371                 :          6 :         port_s->priority = STP_DEFAULT_PORT_PRIORITY;
    1372                 :            :     }
    1373                 :            : }
    1374                 :            : 
    1375                 :            : static void
    1376                 :         30 : port_configure_rstp(const struct ofproto *ofproto, struct port *port,
    1377                 :            :         struct ofproto_port_rstp_settings *port_s, int *port_num_counter)
    1378                 :            : {
    1379                 :            :     const char *config_str;
    1380                 :            :     struct iface *iface;
    1381                 :            : 
    1382         [ +  + ]:         30 :     if (!smap_get_bool(&port->cfg->other_config, "rstp-enable", true)) {
    1383                 :         21 :         port_s->enable = false;
    1384                 :         21 :         return;
    1385                 :            :     } else {
    1386                 :          9 :         port_s->enable = true;
    1387                 :            :     }
    1388                 :            : 
    1389                 :            :     /* RSTP over bonds is not supported. */
    1390         [ -  + ]:          9 :     if (!ovs_list_is_singleton(&port->ifaces)) {
    1391         [ #  # ]:          0 :         VLOG_ERR("port %s: cannot enable RSTP on bonds, disabling",
    1392                 :            :                 port->name);
    1393                 :          0 :         port_s->enable = false;
    1394                 :          0 :         return;
    1395                 :            :     }
    1396                 :            : 
    1397                 :          9 :     iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
    1398                 :            : 
    1399                 :            :     /* Internal ports shouldn't participate in spanning tree, so
    1400                 :            :      * skip them. */
    1401         [ -  + ]:          9 :     if (!strcmp(iface->type, "internal")) {
    1402         [ #  # ]:          0 :         VLOG_DBG("port %s: disable RSTP on internal ports", port->name);
    1403                 :          0 :         port_s->enable = false;
    1404                 :          0 :         return;
    1405                 :            :     }
    1406                 :            : 
    1407                 :            :     /* RSTP on mirror output ports is not supported. */
    1408         [ -  + ]:          9 :     if (ofproto_is_mirror_output_bundle(ofproto, port)) {
    1409         [ #  # ]:          0 :         VLOG_DBG("port %s: disable RSTP on mirror ports", port->name);
    1410                 :          0 :         port_s->enable = false;
    1411                 :          0 :         return;
    1412                 :            :     }
    1413                 :            : 
    1414                 :          9 :     config_str = smap_get(&port->cfg->other_config, "rstp-port-num");
    1415         [ -  + ]:          9 :     if (config_str) {
    1416                 :          0 :         unsigned long int port_num = strtoul(config_str, NULL, 0);
    1417 [ #  # ][ #  # ]:          0 :         if (port_num < 1 || port_num > RSTP_MAX_PORTS) {
    1418         [ #  # ]:          0 :             VLOG_ERR("port %s: invalid rstp-port-num", port->name);
    1419                 :          0 :             port_s->enable = false;
    1420                 :          0 :             return;
    1421                 :            :         }
    1422                 :          0 :         port_s->port_num = port_num;
    1423                 :            :     } else {
    1424         [ -  + ]:          9 :         if (*port_num_counter >= RSTP_MAX_PORTS) {
    1425         [ #  # ]:          0 :             VLOG_ERR("port %s: too many RSTP ports, disabling", port->name);
    1426                 :          0 :             port_s->enable = false;
    1427                 :          0 :             return;
    1428                 :            :         }
    1429                 :            :         /* If rstp-port-num is not specified, use 0.
    1430                 :            :          * rstp_port_set_port_number() will look for the first free one. */
    1431                 :          9 :         port_s->port_num = 0;
    1432                 :            :     }
    1433                 :            : 
    1434                 :          9 :     config_str = smap_get(&port->cfg->other_config, "rstp-path-cost");
    1435         [ -  + ]:          9 :     if (config_str) {
    1436                 :          0 :         port_s->path_cost = strtoul(config_str, NULL, 10);
    1437                 :            :     } else {
    1438                 :            :         enum netdev_features current;
    1439                 :            :         unsigned int mbps;
    1440                 :            : 
    1441                 :          9 :         netdev_get_features(iface->netdev, &current, NULL, NULL, NULL);
    1442                 :          9 :         mbps = netdev_features_to_bps(current, 100 * 1000 * 1000) / 1000000;
    1443                 :          9 :         port_s->path_cost = rstp_convert_speed_to_cost(mbps);
    1444                 :            :     }
    1445                 :            : 
    1446                 :          9 :     config_str = smap_get(&port->cfg->other_config, "rstp-port-priority");
    1447         [ -  + ]:          9 :     if (config_str) {
    1448                 :          0 :         port_s->priority = strtoul(config_str, NULL, 0);
    1449                 :            :     } else {
    1450                 :          9 :         port_s->priority = RSTP_DEFAULT_PORT_PRIORITY;
    1451                 :            :     }
    1452                 :            : 
    1453                 :          9 :     port_s->admin_p2p_mac_state = smap_get_ullong(
    1454                 :          9 :         &port->cfg->other_config, "rstp-admin-p2p-mac",
    1455                 :            :         RSTP_ADMIN_P2P_MAC_FORCE_TRUE);
    1456                 :            : 
    1457                 :          9 :     port_s->admin_port_state = smap_get_bool(&port->cfg->other_config,
    1458                 :            :                                              "rstp-admin-port-state", true);
    1459                 :            : 
    1460                 :          9 :     port_s->admin_edge_port = smap_get_bool(&port->cfg->other_config,
    1461                 :            :                                             "rstp-port-admin-edge", false);
    1462                 :          9 :     port_s->auto_edge = smap_get_bool(&port->cfg->other_config,
    1463                 :            :                                       "rstp-port-auto-edge", true);
    1464                 :          9 :     port_s->mcheck = smap_get_bool(&port->cfg->other_config,
    1465                 :            :                                    "rstp-port-mcheck", false);
    1466                 :            : }
    1467                 :            : 
    1468                 :            : /* Set spanning tree configuration on 'br'. */
    1469                 :            : static void
    1470                 :       4700 : bridge_configure_stp(struct bridge *br, bool enable_stp)
    1471                 :            : {
    1472         [ +  + ]:       4700 :     if (!enable_stp) {
    1473                 :       4690 :         ofproto_set_stp(br->ofproto, NULL);
    1474                 :            :     } else {
    1475                 :            :         struct ofproto_stp_settings br_s;
    1476                 :            :         const char *config_str;
    1477                 :            :         struct port *port;
    1478                 :            :         int port_num_counter;
    1479                 :            :         unsigned long *port_num_bitmap;
    1480                 :            : 
    1481                 :         10 :         config_str = smap_get(&br->cfg->other_config, "stp-system-id");
    1482         [ -  + ]:         10 :         if (config_str) {
    1483                 :            :             struct eth_addr ea;
    1484                 :            : 
    1485         [ #  # ]:          0 :             if (eth_addr_from_string(config_str, &ea)) {
    1486                 :          0 :                 br_s.system_id = eth_addr_to_uint64(ea);
    1487                 :            :             } else {
    1488                 :          0 :                 br_s.system_id = eth_addr_to_uint64(br->ea);
    1489         [ #  # ]:          0 :                 VLOG_ERR("bridge %s: invalid stp-system-id, defaulting "
    1490                 :            :                          "to "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(br->ea));
    1491                 :            :             }
    1492                 :            :         } else {
    1493                 :         10 :             br_s.system_id = eth_addr_to_uint64(br->ea);
    1494                 :            :         }
    1495                 :            : 
    1496                 :         10 :         br_s.priority = smap_get_ullong(&br->cfg->other_config, "stp-priority",
    1497                 :            :                                         STP_DEFAULT_BRIDGE_PRIORITY);
    1498                 :         10 :         br_s.hello_time = smap_get_ullong(&br->cfg->other_config,
    1499                 :            :                                           "stp-hello-time",
    1500                 :            :                                           STP_DEFAULT_HELLO_TIME);
    1501                 :            : 
    1502                 :         10 :         br_s.max_age = smap_get_ullong(&br->cfg->other_config, "stp-max-age",
    1503                 :            :                                        STP_DEFAULT_HELLO_TIME / 1000) * 1000;
    1504                 :         10 :         br_s.fwd_delay = smap_get_ullong(&br->cfg->other_config,
    1505                 :            :                                          "stp-forward-delay",
    1506                 :            :                                          STP_DEFAULT_FWD_DELAY / 1000) * 1000;
    1507                 :            : 
    1508                 :            :         /* Configure STP on the bridge. */
    1509         [ -  + ]:         10 :         if (ofproto_set_stp(br->ofproto, &br_s)) {
    1510         [ #  # ]:          0 :             VLOG_ERR("bridge %s: could not enable STP", br->name);
    1511                 :          0 :             return;
    1512                 :            :         }
    1513                 :            : 
    1514                 :            :         /* Users must either set the port number with the "stp-port-num"
    1515                 :            :          * configuration on all ports or none.  If manual configuration
    1516                 :            :          * is not done, then we allocate them sequentially. */
    1517                 :         10 :         port_num_counter = 0;
    1518                 :         10 :         port_num_bitmap = bitmap_allocate(STP_MAX_PORTS);
    1519 [ +  + ][ -  + ]:         30 :         HMAP_FOR_EACH (port, hmap_node, &br->ports) {
    1520                 :            :             struct ofproto_port_stp_settings port_s;
    1521                 :            :             struct iface *iface;
    1522                 :            : 
    1523                 :         20 :             port_configure_stp(br->ofproto, port, &port_s,
    1524                 :            :                                &port_num_counter, port_num_bitmap);
    1525                 :            : 
    1526                 :            :             /* As bonds are not supported, just apply configuration to
    1527                 :            :              * all interfaces. */
    1528         [ +  + ]:         40 :             LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
    1529         [ -  + ]:         20 :                 if (ofproto_port_set_stp(br->ofproto, iface->ofp_port,
    1530                 :            :                                          &port_s)) {
    1531         [ #  # ]:          0 :                     VLOG_ERR("port %s: could not enable STP", port->name);
    1532                 :          0 :                     continue;
    1533                 :            :                 }
    1534                 :            :             }
    1535                 :            :         }
    1536                 :            : 
    1537         [ -  + ]:         10 :         if (bitmap_scan(port_num_bitmap, 1, 0, STP_MAX_PORTS) != STP_MAX_PORTS
    1538         [ #  # ]:          0 :                     && port_num_counter) {
    1539         [ #  # ]:          0 :             VLOG_ERR("bridge %s: must manually configure all STP port "
    1540                 :            :                      "IDs or none, disabling", br->name);
    1541                 :          0 :             ofproto_set_stp(br->ofproto, NULL);
    1542                 :            :         }
    1543                 :         10 :         bitmap_free(port_num_bitmap);
    1544                 :            :     }
    1545                 :            : }
    1546                 :            : 
    1547                 :            : static void
    1548                 :       4700 : bridge_configure_rstp(struct bridge *br, bool enable_rstp)
    1549                 :            : {
    1550         [ +  + ]:       4700 :     if (!enable_rstp) {
    1551                 :       4688 :         ofproto_set_rstp(br->ofproto, NULL);
    1552                 :            :     } else {
    1553                 :            :         struct ofproto_rstp_settings br_s;
    1554                 :            :         const char *config_str;
    1555                 :            :         struct port *port;
    1556                 :            :         int port_num_counter;
    1557                 :            : 
    1558                 :         12 :         config_str = smap_get(&br->cfg->other_config, "rstp-address");
    1559         [ -  + ]:         12 :         if (config_str) {
    1560                 :            :             struct eth_addr ea;
    1561                 :            : 
    1562         [ #  # ]:          0 :             if (eth_addr_from_string(config_str, &ea)) {
    1563                 :          0 :                 br_s.address = eth_addr_to_uint64(ea);
    1564                 :            :             }
    1565                 :            :             else {
    1566                 :          0 :                 br_s.address = eth_addr_to_uint64(br->ea);
    1567         [ #  # ]:          0 :                 VLOG_ERR("bridge %s: invalid rstp-address, defaulting "
    1568                 :            :                         "to "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(br->ea));
    1569                 :            :             }
    1570                 :            :         }
    1571                 :            :         else {
    1572                 :         12 :             br_s.address = eth_addr_to_uint64(br->ea);
    1573                 :            :         }
    1574                 :            : 
    1575                 :         12 :         const struct smap *oc = &br->cfg->other_config;
    1576                 :         12 :         br_s.priority = smap_get_ullong(oc, "rstp-priority",
    1577                 :            :                                         RSTP_DEFAULT_PRIORITY);
    1578                 :         12 :         br_s.ageing_time = smap_get_ullong(oc, "rstp-ageing-time",
    1579                 :            :                                            RSTP_DEFAULT_AGEING_TIME);
    1580                 :         12 :         br_s.force_protocol_version = smap_get_ullong(
    1581                 :            :             oc, "rstp-force-protocol-version", FPV_DEFAULT);
    1582                 :         12 :         br_s.bridge_max_age = smap_get_ullong(oc, "rstp-max-age",
    1583                 :            :                                               RSTP_DEFAULT_BRIDGE_MAX_AGE);
    1584                 :         12 :         br_s.bridge_forward_delay = smap_get_ullong(
    1585                 :            :             oc, "rstp-forward-delay", RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
    1586                 :         12 :         br_s.transmit_hold_count = smap_get_ullong(
    1587                 :            :             oc, "rstp-transmit-hold-count", RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
    1588                 :            : 
    1589                 :            :         /* Configure RSTP on the bridge. */
    1590         [ -  + ]:         12 :         if (ofproto_set_rstp(br->ofproto, &br_s)) {
    1591         [ #  # ]:          0 :             VLOG_ERR("bridge %s: could not enable RSTP", br->name);
    1592                 :          0 :             return;
    1593                 :            :         }
    1594                 :            : 
    1595                 :         12 :         port_num_counter = 0;
    1596 [ +  + ][ -  + ]:         42 :         HMAP_FOR_EACH (port, hmap_node, &br->ports) {
    1597                 :            :             struct ofproto_port_rstp_settings port_s;
    1598                 :            :             struct iface *iface;
    1599                 :            : 
    1600                 :         30 :             port_configure_rstp(br->ofproto, port, &port_s,
    1601                 :            :                     &port_num_counter);
    1602                 :            : 
    1603                 :            :             /* As bonds are not supported, just apply configuration to
    1604                 :            :              * all interfaces. */
    1605         [ +  + ]:         60 :             LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
    1606         [ -  + ]:         30 :                 if (ofproto_port_set_rstp(br->ofproto, iface->ofp_port,
    1607                 :            :                             &port_s)) {
    1608         [ #  # ]:          0 :                     VLOG_ERR("port %s: could not enable RSTP", port->name);
    1609                 :          0 :                     continue;
    1610                 :            :                 }
    1611                 :            :             }
    1612                 :            :         }
    1613                 :            :     }
    1614                 :            : }
    1615                 :            : 
    1616                 :            : static void
    1617                 :       4700 : bridge_configure_spanning_tree(struct bridge *br)
    1618                 :            : {
    1619                 :       4700 :     bool enable_rstp = br->cfg->rstp_enable;
    1620                 :       4700 :     bool enable_stp = br->cfg->stp_enable;
    1621                 :            : 
    1622 [ +  + ][ -  + ]:       4700 :     if (enable_rstp && enable_stp) {
    1623         [ #  # ]:          0 :         VLOG_WARN("%s: RSTP and STP are mutually exclusive but both are "
    1624                 :            :                   "configured; enabling RSTP", br->name);
    1625                 :          0 :         enable_stp = false;
    1626                 :            :     }
    1627                 :            : 
    1628                 :       4700 :     bridge_configure_stp(br, enable_stp);
    1629                 :       4700 :     bridge_configure_rstp(br, enable_rstp);
    1630                 :       4700 : }
    1631                 :            : 
    1632                 :            : static bool
    1633                 :        121 : bridge_has_bond_fake_iface(const struct bridge *br, const char *name)
    1634                 :            : {
    1635                 :        121 :     const struct port *port = port_lookup(br, name);
    1636 [ -  + ][ #  # ]:        121 :     return port && port_is_bond_fake_iface(port);
    1637                 :            : }
    1638                 :            : 
    1639                 :            : static bool
    1640                 :       2689 : port_is_bond_fake_iface(const struct port *port)
    1641                 :            : {
    1642 [ -  + ][ #  # ]:       2689 :     return port->cfg->bond_fake_iface && !ovs_list_is_short(&port->ifaces);
    1643                 :            : }
    1644                 :            : 
    1645                 :            : static void
    1646                 :       4238 : add_del_bridges(const struct ovsrec_open_vswitch *cfg)
    1647                 :            : {
    1648                 :            :     struct bridge *br, *next;
    1649                 :            :     struct shash_node *node;
    1650                 :            :     struct shash new_br;
    1651                 :            :     size_t i;
    1652                 :            : 
    1653                 :            :     /* Collect new bridges' names and types. */
    1654                 :       4238 :     shash_init(&new_br);
    1655         [ +  + ]:       8942 :     for (i = 0; i < cfg->n_bridges; i++) {
    1656                 :            :         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
    1657                 :       4704 :         const struct ovsrec_bridge *br_cfg = cfg->bridges[i];
    1658                 :            : 
    1659 [ +  + ][ -  + ]:       4704 :         if (strchr(br_cfg->name, '/') || strchr(br_cfg->name, '\\')) {
    1660                 :            :             /* Prevent remote ovsdb-server users from accessing arbitrary
    1661                 :            :              * directories, e.g. consider a bridge named "../../../etc/".
    1662                 :            :              *
    1663                 :            :              * Prohibiting "\" is only necessary on Windows but it's no great
    1664                 :            :              * loss elsewhere. */
    1665         [ +  - ]:          1 :             VLOG_WARN_RL(&rl, "ignoring bridge with invalid name \"%s\"",
    1666                 :            :                          br_cfg->name);
    1667         [ -  + ]:       4703 :         } else if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
    1668         [ #  # ]:          0 :             VLOG_WARN_RL(&rl, "bridge %s specified twice", br_cfg->name);
    1669                 :            :         }
    1670                 :            :     }
    1671                 :            : 
    1672                 :            :     /* Get rid of deleted bridges or those whose types have changed.
    1673                 :            :      * Update 'cfg' of bridges that still exist. */
    1674 [ +  + ][ -  + ]:       8200 :     HMAP_FOR_EACH_SAFE (br, next, node, &all_bridges) {
                 [ +  + ]
    1675                 :       3962 :         br->cfg = shash_find_data(&new_br, br->name);
    1676 [ +  + ][ +  + ]:       3962 :         if (!br->cfg || strcmp(br->type, ofproto_normalize_type(
    1677                 :       3952 :                                    br->cfg->datapath_type))) {
    1678                 :         11 :             bridge_destroy(br, true);
    1679                 :            :         }
    1680                 :            :     }
    1681                 :            : 
    1682                 :            :     /* Add new bridges. */
    1683 [ +  + ][ -  + ]:       8941 :     SHASH_FOR_EACH(node, &new_br) {
    1684                 :       4703 :         const struct ovsrec_bridge *br_cfg = node->data;
    1685                 :       4703 :         struct bridge *br = bridge_lookup(br_cfg->name);
    1686         [ +  + ]:       4703 :         if (!br) {
    1687                 :        752 :             bridge_create(br_cfg);
    1688                 :            :         }
    1689                 :            :     }
    1690                 :            : 
    1691                 :       4238 :     shash_destroy(&new_br);
    1692                 :       4238 : }
    1693                 :            : 
    1694                 :            : /* Configures 'netdev' based on the "options" column in 'iface_cfg'.
    1695                 :            :  * Returns 0 if successful, otherwise a positive errno value. */
    1696                 :            : static int
    1697                 :       2690 : iface_set_netdev_config(const struct ovsrec_interface *iface_cfg,
    1698                 :            :                         struct netdev *netdev, char **errp)
    1699                 :            : {
    1700                 :       2690 :     return netdev_set_config(netdev, &iface_cfg->options, errp);
    1701                 :            : }
    1702                 :            : 
    1703                 :            : /* Opens a network device for 'if_cfg' and configures it.  Adds the network
    1704                 :            :  * device to br->ofproto and stores the OpenFlow port number in '*ofp_portp'.
    1705                 :            :  *
    1706                 :            :  * If successful, returns 0 and stores the network device in '*netdevp'.  On
    1707                 :            :  * failure, returns a positive errno value and stores NULL in '*netdevp'. */
    1708                 :            : static int
    1709                 :       2700 : iface_do_create(const struct bridge *br,
    1710                 :            :                 const struct ovsrec_interface *iface_cfg,
    1711                 :            :                 ofp_port_t *ofp_portp, struct netdev **netdevp,
    1712                 :            :                 char **errp)
    1713                 :            : {
    1714                 :       2700 :     struct netdev *netdev = NULL;
    1715                 :            :     int error;
    1716                 :            :     const char *type;
    1717                 :            : 
    1718         [ +  + ]:       2700 :     if (netdev_is_reserved_name(iface_cfg->name)) {
    1719         [ +  - ]:         10 :         VLOG_WARN("could not create interface %s, name is reserved",
    1720                 :            :                   iface_cfg->name);
    1721                 :         10 :         error = EINVAL;
    1722                 :         10 :         goto error;
    1723                 :            :     }
    1724                 :            : 
    1725                 :       2690 :     type = ofproto_port_open_type(br->cfg->datapath_type,
    1726                 :            :                                   iface_get_type(iface_cfg, br->cfg));
    1727                 :       2690 :     error = netdev_open(iface_cfg->name, type, &netdev);
    1728         [ -  + ]:       2690 :     if (error) {
    1729 [ #  # ][ #  # ]:          0 :         VLOG_WARN_BUF(errp, "could not open network device %s (%s)",
    1730                 :            :                       iface_cfg->name, ovs_strerror(error));
    1731                 :          0 :         goto error;
    1732                 :            :     }
    1733                 :            : 
    1734                 :       2690 :     error = iface_set_netdev_config(iface_cfg, netdev, errp);
    1735         [ -  + ]:       2690 :     if (error) {
    1736                 :          0 :         goto error;
    1737                 :            :     }
    1738                 :            : 
    1739                 :       2690 :     iface_set_netdev_mtu(iface_cfg, netdev);
    1740                 :            : 
    1741                 :       2690 :     *ofp_portp = iface_pick_ofport(iface_cfg);
    1742                 :       2690 :     error = ofproto_port_add(br->ofproto, netdev, ofp_portp);
    1743         [ +  + ]:       2690 :     if (error) {
    1744                 :          1 :         goto error;
    1745                 :            :     }
    1746                 :            : 
    1747         [ +  - ]:       2689 :     VLOG_INFO("bridge %s: added interface %s on port %d",
    1748                 :            :               br->name, iface_cfg->name, *ofp_portp);
    1749                 :            : 
    1750                 :       2689 :     *netdevp = netdev;
    1751                 :       2689 :     return 0;
    1752                 :            : 
    1753                 :            : error:
    1754                 :         11 :     *netdevp = NULL;
    1755                 :         11 :     netdev_close(netdev);
    1756                 :       2700 :     return error;
    1757                 :            : }
    1758                 :            : 
    1759                 :            : /* Creates a new iface on 'br' based on 'if_cfg'.  The new iface has OpenFlow
    1760                 :            :  * port number 'ofp_port'.  If ofp_port is OFPP_NONE, an OpenFlow port is
    1761                 :            :  * automatically allocated for the iface.  Takes ownership of and
    1762                 :            :  * deallocates 'if_cfg'.
    1763                 :            :  *
    1764                 :            :  * Return true if an iface is successfully created, false otherwise. */
    1765                 :            : static bool
    1766                 :       2700 : iface_create(struct bridge *br, const struct ovsrec_interface *iface_cfg,
    1767                 :            :              const struct ovsrec_port *port_cfg)
    1768                 :            : {
    1769                 :            :     struct netdev *netdev;
    1770                 :            :     struct iface *iface;
    1771                 :            :     ofp_port_t ofp_port;
    1772                 :            :     struct port *port;
    1773                 :       2700 :     char *errp = NULL;
    1774                 :            :     int error;
    1775                 :            : 
    1776                 :            :     /* Do the bits that can fail up front. */
    1777         [ -  + ]:       2700 :     ovs_assert(!iface_lookup(br, iface_cfg->name));
    1778                 :       2700 :     error = iface_do_create(br, iface_cfg, &ofp_port, &netdev, &errp);
    1779         [ +  + ]:       2700 :     if (error) {
    1780                 :         11 :         iface_clear_db_record(iface_cfg, errp);
    1781                 :         11 :         free(errp);
    1782                 :         11 :         return false;
    1783                 :            :     }
    1784                 :            : 
    1785                 :            :     /* Get or create the port structure. */
    1786                 :       2689 :     port = port_lookup(br, port_cfg->name);
    1787         [ +  + ]:       2689 :     if (!port) {
    1788                 :       2667 :         port = port_create(br, port_cfg);
    1789                 :            :     }
    1790                 :            : 
    1791                 :            :     /* Create the iface structure. */
    1792                 :       2689 :     iface = xzalloc(sizeof *iface);
    1793                 :       2689 :     ovs_list_push_back(&port->ifaces, &iface->port_elem);
    1794                 :       2689 :     hmap_insert(&br->iface_by_name, &iface->name_node,
    1795                 :            :                 hash_string(iface_cfg->name, 0));
    1796                 :       2689 :     iface->port = port;
    1797                 :       2689 :     iface->name = xstrdup(iface_cfg->name);
    1798                 :       2689 :     iface->ofp_port = ofp_port;
    1799                 :       2689 :     iface->netdev = netdev;
    1800                 :       2689 :     iface->type = iface_get_type(iface_cfg, br->cfg);
    1801                 :       2689 :     iface->netdev_type = ofproto_port_open_type(br->cfg->datapath_type,
    1802                 :            :                                                 iface->type);
    1803                 :       2689 :     iface->cfg = iface_cfg;
    1804                 :       2689 :     hmap_insert(&br->ifaces, &iface->ofp_port_node,
    1805                 :            :                 hash_ofp_port(ofp_port));
    1806                 :            : 
    1807                 :            :     /* Populate initial status in database. */
    1808                 :       2689 :     iface_refresh_stats(iface);
    1809                 :       2689 :     iface_refresh_netdev_status(iface);
    1810                 :            : 
    1811                 :            :     /* Add bond fake iface if necessary. */
    1812         [ -  + ]:       2689 :     if (port_is_bond_fake_iface(port)) {
    1813                 :            :         struct ofproto_port ofproto_port;
    1814                 :            : 
    1815         [ #  # ]:          0 :         if (ofproto_port_query_by_name(br->ofproto, port->name,
    1816                 :            :                                        &ofproto_port)) {
    1817                 :            :             struct netdev *netdev;
    1818                 :            :             int error;
    1819                 :            : 
    1820                 :          0 :             error = netdev_open(port->name, "internal", &netdev);
    1821         [ #  # ]:          0 :             if (!error) {
    1822                 :          0 :                 ofp_port_t fake_ofp_port = OFPP_NONE;
    1823                 :          0 :                 ofproto_port_add(br->ofproto, netdev, &fake_ofp_port);
    1824                 :          0 :                 netdev_close(netdev);
    1825                 :            :             } else {
    1826         [ #  # ]:          0 :                 VLOG_WARN("could not open network device %s (%s)",
    1827                 :            :                           port->name, ovs_strerror(error));
    1828                 :            :             }
    1829                 :            :         } else {
    1830                 :            :             /* Already exists, nothing to do. */
    1831                 :          0 :             ofproto_port_destroy(&ofproto_port);
    1832                 :            :         }
    1833                 :            :     }
    1834                 :            : 
    1835                 :       2700 :     return true;
    1836                 :            : }
    1837                 :            : 
    1838                 :            : /* Set forward BPDU option. */
    1839                 :            : static void
    1840                 :       4700 : bridge_configure_forward_bpdu(struct bridge *br)
    1841                 :            : {
    1842                 :       4700 :     ofproto_set_forward_bpdu(br->ofproto,
    1843                 :       4700 :                              smap_get_bool(&br->cfg->other_config,
    1844                 :            :                                            "forward-bpdu",
    1845                 :            :                                            false));
    1846                 :       4700 : }
    1847                 :            : 
    1848                 :            : /* Set MAC learning table configuration for 'br'. */
    1849                 :            : static void
    1850                 :       4700 : bridge_configure_mac_table(struct bridge *br)
    1851                 :            : {
    1852                 :       4700 :     const struct smap *oc = &br->cfg->other_config;
    1853                 :       4700 :     int idle_time = smap_get_int(oc, "mac-aging-time", 0);
    1854         [ +  - ]:       4700 :     if (!idle_time) {
    1855                 :       4700 :         idle_time = MAC_ENTRY_DEFAULT_IDLE_TIME;
    1856                 :            :     }
    1857                 :            : 
    1858                 :       4700 :     int mac_table_size = smap_get_int(oc, "mac-table-size", 0);
    1859         [ +  + ]:       4700 :     if (!mac_table_size) {
    1860                 :       4696 :         mac_table_size = MAC_DEFAULT_MAX;
    1861                 :            :     }
    1862                 :            : 
    1863                 :       4700 :     ofproto_set_mac_table_config(br->ofproto, idle_time, mac_table_size);
    1864                 :       4700 : }
    1865                 :            : 
    1866                 :            : /* Set multicast snooping table configuration for 'br'. */
    1867                 :            : static void
    1868                 :       4700 : bridge_configure_mcast_snooping(struct bridge *br)
    1869                 :            : {
    1870         [ +  - ]:       4700 :     if (!br->cfg->mcast_snooping_enable) {
    1871                 :       4700 :         ofproto_set_mcast_snooping(br->ofproto, NULL);
    1872                 :            :     } else {
    1873                 :            :         struct port *port;
    1874                 :            :         struct ofproto_mcast_snooping_settings br_s;
    1875                 :            : 
    1876                 :          0 :         const struct smap *oc = &br->cfg->other_config;
    1877                 :          0 :         int idle_time = smap_get_int(oc, "mcast-snooping-aging-time", 0);
    1878         [ #  # ]:          0 :         br_s.idle_time = idle_time ? idle_time : MCAST_ENTRY_DEFAULT_IDLE_TIME;
    1879                 :          0 :         int max_entries = smap_get_int(oc, "mcast-snooping-table-size", 0);
    1880         [ #  # ]:          0 :         br_s.max_entries = (max_entries
    1881                 :            :                             ? max_entries
    1882                 :            :                             : MCAST_DEFAULT_MAX_ENTRIES);
    1883                 :            : 
    1884                 :          0 :         br_s.flood_unreg = !smap_get_bool(
    1885                 :          0 :             oc, "mcast-snooping-disable-flood-unregistered", false);
    1886                 :            : 
    1887                 :            :         /* Configure multicast snooping on the bridge */
    1888         [ #  # ]:          0 :         if (ofproto_set_mcast_snooping(br->ofproto, &br_s)) {
    1889         [ #  # ]:          0 :             VLOG_ERR("bridge %s: could not enable multicast snooping",
    1890                 :            :                      br->name);
    1891                 :          0 :             return;
    1892                 :            :         }
    1893                 :            : 
    1894 [ #  # ][ #  # ]:          0 :         HMAP_FOR_EACH (port, hmap_node, &br->ports) {
    1895                 :            :             struct ofproto_mcast_snooping_port_settings port_s;
    1896                 :          0 :             port_s.flood = smap_get_bool(&port->cfg->other_config,
    1897                 :            :                                        "mcast-snooping-flood", false);
    1898                 :          0 :             port_s.flood_reports = smap_get_bool(&port->cfg->other_config,
    1899                 :            :                                        "mcast-snooping-flood-reports", false);
    1900         [ #  # ]:          0 :             if (ofproto_port_set_mcast_snooping(br->ofproto, port, &port_s)) {
    1901         [ #  # ]:          0 :                 VLOG_ERR("port %s: could not configure mcast snooping",
    1902                 :            :                          port->name);
    1903                 :            :             }
    1904                 :            :         }
    1905                 :            :     }
    1906                 :            : }
    1907                 :            : 
    1908                 :            : static void
    1909                 :       3248 : find_local_hw_addr(const struct bridge *br, struct eth_addr *ea,
    1910                 :            :                    const struct port *fake_br, struct iface **hw_addr_iface)
    1911                 :            : {
    1912                 :            :     struct hmapx mirror_output_ports;
    1913                 :            :     struct port *port;
    1914                 :       3248 :     bool found_addr = false;
    1915                 :            :     int error;
    1916                 :            :     int i;
    1917                 :            : 
    1918                 :            :     /* Mirror output ports don't participate in picking the local hardware
    1919                 :            :      * address.  ofproto can't help us find out whether a given port is a
    1920                 :            :      * mirror output because we haven't configured mirrors yet, so we need to
    1921                 :            :      * accumulate them ourselves. */
    1922                 :       3248 :     hmapx_init(&mirror_output_ports);
    1923         [ -  + ]:       3248 :     for (i = 0; i < br->cfg->n_mirrors; i++) {
    1924                 :          0 :         struct ovsrec_mirror *m = br->cfg->mirrors[i];
    1925         [ #  # ]:          0 :         if (m->output_port) {
    1926                 :          0 :             hmapx_add(&mirror_output_ports, m->output_port);
    1927                 :            :         }
    1928                 :            :     }
    1929                 :            : 
    1930                 :            :     /* Otherwise choose the minimum non-local MAC address among all of the
    1931                 :            :      * interfaces. */
    1932 [ +  + ][ -  + ]:      11954 :     HMAP_FOR_EACH (port, hmap_node, &br->ports) {
    1933                 :            :         struct eth_addr iface_ea;
    1934                 :            :         struct iface *candidate;
    1935                 :            :         struct iface *iface;
    1936                 :            : 
    1937                 :            :         /* Mirror output ports don't participate. */
    1938         [ -  + ]:       8706 :         if (hmapx_contains(&mirror_output_ports, port->cfg)) {
    1939                 :       3253 :             continue;
    1940                 :            :         }
    1941                 :            : 
    1942                 :            :         /* Choose the MAC address to represent the port. */
    1943                 :       8706 :         iface = NULL;
    1944 [ -  + ][ #  # ]:       8706 :         if (port->cfg->mac && eth_addr_from_string(port->cfg->mac,
    1945                 :            :                                                    &iface_ea)) {
    1946                 :            :             /* Find the interface with this Ethernet address (if any) so that
    1947                 :            :              * we can provide the correct devname to the caller. */
    1948         [ #  # ]:          0 :             LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
    1949                 :            :                 struct eth_addr candidate_ea;
    1950         [ #  # ]:          0 :                 if (!netdev_get_etheraddr(candidate->netdev, &candidate_ea)
    1951         [ #  # ]:          0 :                     && eth_addr_equals(iface_ea, candidate_ea)) {
    1952                 :          0 :                     iface = candidate;
    1953                 :            :                 }
    1954                 :            :             }
    1955                 :            :         } else {
    1956                 :            :             /* Choose the interface whose MAC address will represent the port.
    1957                 :            :              * The Linux kernel bonding code always chooses the MAC address of
    1958                 :            :              * the first slave added to a bond, and the Fedora networking
    1959                 :            :              * scripts always add slaves to a bond in alphabetical order, so
    1960                 :            :              * for compatibility we choose the interface with the name that is
    1961                 :            :              * first in alphabetical order. */
    1962         [ +  + ]:      17420 :             LIST_FOR_EACH (candidate, port_elem, &port->ifaces) {
    1963 [ +  + ][ +  + ]:       8714 :                 if (!iface || strcmp(candidate->name, iface->name) < 0) {
    1964                 :       8710 :                     iface = candidate;
    1965                 :            :                 }
    1966                 :            :             }
    1967                 :            : 
    1968                 :            :             /* A port always has at least one interface. */
    1969         [ -  + ]:       8706 :             ovs_assert(iface != NULL);
    1970                 :            : 
    1971                 :            :             /* The local port doesn't count (since we're trying to choose its
    1972                 :            :              * MAC address anyway). */
    1973         [ +  + ]:       8706 :             if (iface->ofp_port == OFPP_LOCAL) {
    1974                 :       3248 :                 continue;
    1975                 :            :             }
    1976                 :            : 
    1977                 :            :             /* For fake bridges we only choose from ports with the same tag */
    1978 [ -  + ][ #  # ]:       5458 :             if (fake_br && fake_br->cfg && fake_br->cfg->tag) {
                 [ #  # ]
    1979         [ #  # ]:          0 :                 if (!port->cfg->tag) {
    1980                 :          0 :                     continue;
    1981                 :            :                 }
    1982         [ #  # ]:          0 :                 if (*port->cfg->tag != *fake_br->cfg->tag) {
    1983                 :          0 :                     continue;
    1984                 :            :                 }
    1985                 :            :             }
    1986                 :            : 
    1987                 :            :             /* Grab MAC. */
    1988                 :       5458 :             error = netdev_get_etheraddr(iface->netdev, &iface_ea);
    1989         [ +  + ]:       5458 :             if (error) {
    1990                 :          5 :                 continue;
    1991                 :            :             }
    1992                 :            :         }
    1993                 :            : 
    1994                 :            :         /* Compare against our current choice. */
    1995   [ +  -  -  + ]:      10906 :         if (!eth_addr_is_multicast(iface_ea) &&
    1996         [ #  # ]:       5453 :             !eth_addr_is_local(iface_ea) &&
    1997         [ #  # ]:          0 :             !eth_addr_is_reserved(iface_ea) &&
    1998         [ #  # ]:          0 :             !eth_addr_is_zero(iface_ea) &&
    1999         [ #  # ]:          0 :             (!found_addr || eth_addr_compare_3way(iface_ea, *ea) < 0))
    2000                 :            :         {
    2001                 :          0 :             *ea = iface_ea;
    2002                 :          0 :             *hw_addr_iface = iface;
    2003                 :       5453 :             found_addr = true;
    2004                 :            :         }
    2005                 :            :     }
    2006                 :            : 
    2007         [ +  - ]:       3248 :     if (!found_addr) {
    2008                 :       3248 :         *ea = br->default_ea;
    2009                 :       3248 :         *hw_addr_iface = NULL;
    2010                 :            :     }
    2011                 :            : 
    2012                 :       3248 :     hmapx_destroy(&mirror_output_ports);
    2013                 :       3248 : }
    2014                 :            : 
    2015                 :            : static void
    2016                 :       4700 : bridge_pick_local_hw_addr(struct bridge *br, struct eth_addr *ea,
    2017                 :            :                           struct iface **hw_addr_iface)
    2018                 :            : {
    2019                 :       4700 :     *hw_addr_iface = NULL;
    2020                 :            : 
    2021                 :            :     /* Did the user request a particular MAC? */
    2022                 :       4700 :     const char *hwaddr = smap_get_def(&br->cfg->other_config, "hwaddr", "");
    2023         [ +  + ]:       4700 :     if (eth_addr_from_string(hwaddr, ea)) {
    2024         [ -  + ]:       1452 :         if (eth_addr_is_multicast(*ea)) {
    2025         [ #  # ]:          0 :             VLOG_ERR("bridge %s: cannot set MAC address to multicast "
    2026                 :            :                      "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(*ea));
    2027         [ -  + ]:       1452 :         } else if (eth_addr_is_zero(*ea)) {
    2028         [ #  # ]:          0 :             VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
    2029                 :            :         } else {
    2030                 :       1452 :             return;
    2031                 :            :         }
    2032                 :            :     }
    2033                 :            : 
    2034                 :            :     /* Find a local hw address */
    2035                 :       3248 :     find_local_hw_addr(br, ea, NULL, hw_addr_iface);
    2036                 :            : }
    2037                 :            : 
    2038                 :            : /* Choose and returns the datapath ID for bridge 'br' given that the bridge
    2039                 :            :  * Ethernet address is 'bridge_ea'.  If 'bridge_ea' is the Ethernet address of
    2040                 :            :  * an interface on 'br', then that interface must be passed in as
    2041                 :            :  * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
    2042                 :            :  * 'hw_addr_iface' must be passed in as a null pointer. */
    2043                 :            : static uint64_t
    2044                 :       4700 : bridge_pick_datapath_id(struct bridge *br,
    2045                 :            :                         const struct eth_addr bridge_ea,
    2046                 :            :                         struct iface *hw_addr_iface)
    2047                 :            : {
    2048                 :            :     /*
    2049                 :            :      * The procedure for choosing a bridge MAC address will, in the most
    2050                 :            :      * ordinary case, also choose a unique MAC that we can use as a datapath
    2051                 :            :      * ID.  In some special cases, though, multiple bridges will end up with
    2052                 :            :      * the same MAC address.  This is OK for the bridges, but it will confuse
    2053                 :            :      * the OpenFlow controller, because each datapath needs a unique datapath
    2054                 :            :      * ID.
    2055                 :            :      *
    2056                 :            :      * Datapath IDs must be unique.  It is also very desirable that they be
    2057                 :            :      * stable from one run to the next, so that policy set on a datapath
    2058                 :            :      * "sticks".
    2059                 :            :      */
    2060                 :            :     const char *datapath_id;
    2061                 :            :     uint64_t dpid;
    2062                 :            : 
    2063                 :       4700 :     datapath_id = smap_get_def(&br->cfg->other_config, "datapath-id", "");
    2064         [ +  + ]:       4700 :     if (dpid_from_string(datapath_id, &dpid)) {
    2065                 :       1389 :         return dpid;
    2066                 :            :     }
    2067                 :            : 
    2068         [ +  - ]:       3311 :     if (!hw_addr_iface) {
    2069                 :            :         /*
    2070                 :            :          * A purely internal bridge, that is, one that has no non-virtual
    2071                 :            :          * network devices on it at all, is difficult because it has no
    2072                 :            :          * natural unique identifier at all.
    2073                 :            :          *
    2074                 :            :          * When the host is a XenServer, we handle this case by hashing the
    2075                 :            :          * host's UUID with the name of the bridge.  Names of bridges are
    2076                 :            :          * persistent across XenServer reboots, although they can be reused if
    2077                 :            :          * an internal network is destroyed and then a new one is later
    2078                 :            :          * created, so this is fairly effective.
    2079                 :            :          *
    2080                 :            :          * When the host is not a XenServer, we punt by using a random MAC
    2081                 :            :          * address on each run.
    2082                 :            :          */
    2083                 :       3311 :         const char *host_uuid = xenserver_get_host_uuid();
    2084         [ -  + ]:       3311 :         if (host_uuid) {
    2085                 :          0 :             char *combined = xasprintf("%s,%s", host_uuid, br->name);
    2086                 :          0 :             dpid = dpid_from_hash(combined, strlen(combined));
    2087                 :          0 :             free(combined);
    2088                 :          0 :             return dpid;
    2089                 :            :         }
    2090                 :            :     }
    2091                 :            : 
    2092                 :       4700 :     return eth_addr_to_uint64(bridge_ea);
    2093                 :            : }
    2094                 :            : 
    2095                 :            : static uint64_t
    2096                 :          0 : dpid_from_hash(const void *data, size_t n)
    2097                 :            : {
    2098                 :            :     union {
    2099                 :            :         uint8_t bytes[SHA1_DIGEST_SIZE];
    2100                 :            :         struct eth_addr ea;
    2101                 :            :     } hash;
    2102                 :            : 
    2103                 :          0 :     sha1_bytes(data, n, hash.bytes);
    2104                 :          0 :     eth_addr_mark_random(&hash.ea);
    2105                 :          0 :     return eth_addr_to_uint64(hash.ea);
    2106                 :            : }
    2107                 :            : 
    2108                 :            : static void
    2109                 :      39158 : iface_refresh_netdev_status(struct iface *iface)
    2110                 :            : {
    2111                 :            :     struct smap smap;
    2112                 :            : 
    2113                 :            :     enum netdev_features current;
    2114                 :            :     enum netdev_flags flags;
    2115                 :            :     const char *link_state;
    2116                 :            :     struct eth_addr mac;
    2117                 :            :     int64_t bps, mtu_64, ifindex64, link_resets;
    2118                 :            :     int mtu, error;
    2119                 :            : 
    2120         [ -  + ]:      39158 :     if (iface_is_synthetic(iface)) {
    2121                 :       5018 :         return;
    2122                 :            :     }
    2123                 :            : 
    2124         [ +  + ]:      39158 :     if (iface->change_seq == netdev_get_change_seq(iface->netdev)
    2125         [ +  + ]:      31463 :         && !status_txn_try_again) {
    2126                 :       5018 :         return;
    2127                 :            :     }
    2128                 :            : 
    2129                 :      34140 :     iface->change_seq = netdev_get_change_seq(iface->netdev);
    2130                 :            : 
    2131                 :      34140 :     smap_init(&smap);
    2132                 :            : 
    2133         [ +  + ]:      34140 :     if (!netdev_get_status(iface->netdev, &smap)) {
    2134                 :      24895 :         ovsrec_interface_set_status(iface->cfg, &smap);
    2135                 :            :     } else {
    2136                 :       9245 :         ovsrec_interface_set_status(iface->cfg, NULL);
    2137                 :            :     }
    2138                 :            : 
    2139                 :      34140 :     smap_destroy(&smap);
    2140                 :            : 
    2141                 :      34140 :     error = netdev_get_flags(iface->netdev, &flags);
    2142         [ +  - ]:      34140 :     if (!error) {
    2143         [ +  + ]:      34140 :         const char *state = flags & NETDEV_UP ? "up" : "down";
    2144                 :            : 
    2145                 :      34140 :         ovsrec_interface_set_admin_state(iface->cfg, state);
    2146                 :            :     } else {
    2147                 :          0 :         ovsrec_interface_set_admin_state(iface->cfg, NULL);
    2148                 :            :     }
    2149                 :            : 
    2150         [ +  + ]:      34140 :     link_state = netdev_get_carrier(iface->netdev) ? "up" : "down";
    2151                 :      34140 :     ovsrec_interface_set_link_state(iface->cfg, link_state);
    2152                 :            : 
    2153                 :      34140 :     link_resets = netdev_get_carrier_resets(iface->netdev);
    2154                 :      34140 :     ovsrec_interface_set_link_resets(iface->cfg, &link_resets, 1);
    2155                 :            : 
    2156                 :      34140 :     error = netdev_get_features(iface->netdev, &current, NULL, NULL, NULL);
    2157         [ +  + ]:      34140 :     bps = !error ? netdev_features_to_bps(current, 0) : 0;
    2158         [ +  + ]:      34140 :     if (bps) {
    2159         [ +  - ]:       2998 :         ovsrec_interface_set_duplex(iface->cfg,
    2160                 :       2998 :                                     netdev_features_is_full_duplex(current)
    2161                 :            :                                     ? "full" : "half");
    2162                 :       2998 :         ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
    2163                 :            :     } else {
    2164                 :      31142 :         ovsrec_interface_set_duplex(iface->cfg, NULL);
    2165                 :      31142 :         ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
    2166                 :            :     }
    2167                 :            : 
    2168                 :      34140 :     error = netdev_get_mtu(iface->netdev, &mtu);
    2169         [ +  + ]:      34140 :     if (!error) {
    2170                 :      11183 :         mtu_64 = mtu;
    2171                 :      11183 :         ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
    2172                 :            :     } else {
    2173                 :      22957 :         ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
    2174                 :            :     }
    2175                 :            : 
    2176                 :      34140 :     error = netdev_get_etheraddr(iface->netdev, &mac);
    2177         [ +  + ]:      34140 :     if (!error) {
    2178                 :            :         char mac_string[ETH_ADDR_STRLEN + 1];
    2179                 :            : 
    2180                 :     204822 :         snprintf(mac_string, sizeof mac_string,
    2181                 :     204822 :                  ETH_ADDR_FMT, ETH_ADDR_ARGS(mac));
    2182                 :      34137 :         ovsrec_interface_set_mac_in_use(iface->cfg, mac_string);
    2183                 :            :     } else {
    2184                 :          3 :         ovsrec_interface_set_mac_in_use(iface->cfg, NULL);
    2185                 :            :     }
    2186                 :            : 
    2187                 :            :     /* The netdev may return a negative number (such as -EOPNOTSUPP)
    2188                 :            :      * if there is no valid ifindex number. */
    2189                 :      34140 :     ifindex64 = netdev_get_ifindex(iface->netdev);
    2190         [ +  + ]:      34140 :     if (ifindex64 < 0) {
    2191                 :      29892 :         ifindex64 = 0;
    2192                 :            :     }
    2193                 :      34140 :     ovsrec_interface_set_ifindex(iface->cfg, &ifindex64, 1);
    2194                 :            : }
    2195                 :            : 
    2196                 :            : static void
    2197                 :      36469 : iface_refresh_ofproto_status(struct iface *iface)
    2198                 :            : {
    2199                 :            :     int current;
    2200                 :            : 
    2201         [ -  + ]:      36469 :     if (iface_is_synthetic(iface)) {
    2202                 :          0 :         return;
    2203                 :            :     }
    2204                 :            : 
    2205                 :      36469 :     current = ofproto_port_is_lacp_current(iface->port->bridge->ofproto,
    2206                 :            :                                            iface->ofp_port);
    2207         [ +  + ]:      36469 :     if (current >= 0) {
    2208                 :        145 :         bool bl = current;
    2209                 :        145 :         ovsrec_interface_set_lacp_current(iface->cfg, &bl, 1);
    2210                 :            :     } else {
    2211                 :      36324 :         ovsrec_interface_set_lacp_current(iface->cfg, NULL, 0);
    2212                 :            :     }
    2213                 :            : 
    2214         [ +  + ]:      36469 :     if (ofproto_port_cfm_status_changed(iface->port->bridge->ofproto,
    2215                 :            :                                         iface->ofp_port)
    2216         [ +  + ]:         31 :         || status_txn_try_again) {
    2217                 :      36454 :         iface_refresh_cfm_stats(iface);
    2218                 :            :     }
    2219                 :            : 
    2220         [ +  + ]:      36469 :     if (ofproto_port_bfd_status_changed(iface->port->bridge->ofproto,
    2221                 :            :                                         iface->ofp_port)
    2222         [ +  + ]:      19819 :         || status_txn_try_again) {
    2223                 :            :         struct smap smap;
    2224                 :            : 
    2225                 :      36410 :         smap_init(&smap);
    2226                 :      36410 :         ofproto_port_get_bfd_status(iface->port->bridge->ofproto,
    2227                 :            :                                     iface->ofp_port, &smap);
    2228                 :      36410 :         ovsrec_interface_set_bfd_status(iface->cfg, &smap);
    2229                 :      36410 :         smap_destroy(&smap);
    2230                 :            :     }
    2231                 :            : }
    2232                 :            : 
    2233                 :            : /* Writes 'iface''s CFM statistics to the database. 'iface' must not be
    2234                 :            :  * synthetic. */
    2235                 :            : static void
    2236                 :      36454 : iface_refresh_cfm_stats(struct iface *iface)
    2237                 :            : {
    2238                 :      36454 :     const struct ovsrec_interface *cfg = iface->cfg;
    2239                 :            :     struct cfm_status status;
    2240                 :            :     int error;
    2241                 :            : 
    2242                 :      36454 :     error = ofproto_port_get_cfm_status(iface->port->bridge->ofproto,
    2243                 :            :                                         iface->ofp_port, &status);
    2244         [ +  + ]:      36454 :     if (error > 0) {
    2245                 :      36382 :         ovsrec_interface_set_cfm_fault(cfg, NULL, 0);
    2246                 :      36382 :         ovsrec_interface_set_cfm_fault_status(cfg, NULL, 0);
    2247                 :      36382 :         ovsrec_interface_set_cfm_remote_opstate(cfg, NULL);
    2248                 :      36382 :         ovsrec_interface_set_cfm_flap_count(cfg, NULL, 0);
    2249                 :      36382 :         ovsrec_interface_set_cfm_health(cfg, NULL, 0);
    2250                 :      36382 :         ovsrec_interface_set_cfm_remote_mpids(cfg, NULL, 0);
    2251                 :            :     } else {
    2252                 :            :         const char *reasons[CFM_FAULT_N_REASONS];
    2253                 :         72 :         int64_t cfm_health = status.health;
    2254                 :         72 :         int64_t cfm_flap_count = status.flap_count;
    2255                 :         72 :         bool faulted = status.faults != 0;
    2256                 :            :         size_t i, j;
    2257                 :            : 
    2258                 :         72 :         ovsrec_interface_set_cfm_fault(cfg, &faulted, 1);
    2259                 :            : 
    2260                 :         72 :         j = 0;
    2261         [ +  + ]:        504 :         for (i = 0; i < CFM_FAULT_N_REASONS; i++) {
    2262                 :        432 :             int reason = 1 << i;
    2263         [ +  + ]:        432 :             if (status.faults & reason) {
    2264                 :         14 :                 reasons[j++] = cfm_fault_reason_to_str(reason);
    2265                 :            :             }
    2266                 :            :         }
    2267                 :         72 :         ovsrec_interface_set_cfm_fault_status(cfg, reasons, j);
    2268                 :            : 
    2269                 :         72 :         ovsrec_interface_set_cfm_flap_count(cfg, &cfm_flap_count, 1);
    2270                 :            : 
    2271         [ +  + ]:         72 :         if (status.remote_opstate >= 0) {
    2272         [ +  - ]:         63 :             const char *remote_opstate = status.remote_opstate ? "up" : "down";
    2273                 :         63 :             ovsrec_interface_set_cfm_remote_opstate(cfg, remote_opstate);
    2274                 :            :         } else {
    2275                 :          9 :             ovsrec_interface_set_cfm_remote_opstate(cfg, NULL);
    2276                 :            :         }
    2277                 :            : 
    2278                 :         72 :         ovsrec_interface_set_cfm_remote_mpids(cfg,
    2279                 :         72 :                                               (const int64_t *)status.rmps,
    2280                 :            :                                               status.n_rmps);
    2281         [ +  + ]:         72 :         if (cfm_health >= 0) {
    2282                 :         33 :             ovsrec_interface_set_cfm_health(cfg, &cfm_health, 1);
    2283                 :            :         } else {
    2284                 :         39 :             ovsrec_interface_set_cfm_health(cfg, NULL, 0);
    2285                 :            :         }
    2286                 :            : 
    2287                 :         72 :         free(status.rmps);
    2288                 :            :     }
    2289                 :      36454 : }
    2290                 :            : 
    2291                 :            : static void
    2292                 :       7977 : iface_refresh_stats(struct iface *iface)
    2293                 :            : {
    2294                 :            : #define IFACE_STATS                             \
    2295                 :            :     IFACE_STAT(rx_packets,              "rx_packets")               \
    2296                 :            :     IFACE_STAT(tx_packets,              "tx_packets")               \
    2297                 :            :     IFACE_STAT(rx_bytes,                "rx_bytes")                 \
    2298                 :            :     IFACE_STAT(tx_bytes,                "tx_bytes")                 \
    2299                 :            :     IFACE_STAT(rx_dropped,              "rx_dropped")               \
    2300                 :            :     IFACE_STAT(tx_dropped,              "tx_dropped")               \
    2301                 :            :     IFACE_STAT(rx_errors,               "rx_errors")                \
    2302                 :            :     IFACE_STAT(tx_errors,               "tx_errors")                \
    2303                 :            :     IFACE_STAT(rx_frame_errors,         "rx_frame_err")             \
    2304                 :            :     IFACE_STAT(rx_over_errors,          "rx_over_err")              \
    2305                 :            :     IFACE_STAT(rx_crc_errors,           "rx_crc_err")               \
    2306                 :            :     IFACE_STAT(collisions,              "collisions")               \
    2307                 :            :     IFACE_STAT(rx_1_to_64_packets,      "rx_1_to_64_packets")       \
    2308                 :            :     IFACE_STAT(rx_65_to_127_packets,    "rx_65_to_127_packets")     \
    2309                 :            :     IFACE_STAT(rx_128_to_255_packets,   "rx_128_to_255_packets")    \
    2310                 :            :     IFACE_STAT(rx_256_to_511_packets,   "rx_256_to_511_packets")    \
    2311                 :            :     IFACE_STAT(rx_512_to_1023_packets,  "rx_512_to_1023_packets")   \
    2312                 :            :     IFACE_STAT(rx_1024_to_1522_packets, "rx_1024_to_1518_packets")  \
    2313                 :            :     IFACE_STAT(rx_1523_to_max_packets,  "rx_1523_to_max_packets")   \
    2314                 :            :     IFACE_STAT(tx_1_to_64_packets,      "tx_1_to_64_packets")       \
    2315                 :            :     IFACE_STAT(tx_65_to_127_packets,    "tx_65_to_127_packets")     \
    2316                 :            :     IFACE_STAT(tx_128_to_255_packets,   "tx_128_to_255_packets")    \
    2317                 :            :     IFACE_STAT(tx_256_to_511_packets,   "tx_256_to_511_packets")    \
    2318                 :            :     IFACE_STAT(tx_512_to_1023_packets,  "tx_512_to_1023_packets")   \
    2319                 :            :     IFACE_STAT(tx_1024_to_1522_packets, "tx_1024_to_1518_packets")  \
    2320                 :            :     IFACE_STAT(tx_1523_to_max_packets,  "tx_1523_to_max_packets")   \
    2321                 :            :     IFACE_STAT(tx_multicast_packets,    "tx_multicast_packets")     \
    2322                 :            :     IFACE_STAT(rx_broadcast_packets,    "rx_broadcast_packets")     \
    2323                 :            :     IFACE_STAT(tx_broadcast_packets,    "tx_broadcast_packets")     \
    2324                 :            :     IFACE_STAT(rx_undersized_errors,    "rx_undersized_errors")     \
    2325                 :            :     IFACE_STAT(rx_oversize_errors,      "rx_oversize_errors")       \
    2326                 :            :     IFACE_STAT(rx_fragmented_errors,    "rx_fragmented_errors")     \
    2327                 :            :     IFACE_STAT(rx_jabber_errors,        "rx_jabber_errors")
    2328                 :            : 
    2329                 :            : #define IFACE_STAT(MEMBER, NAME) + 1
    2330                 :            :     enum { N_IFACE_STATS = IFACE_STATS };
    2331                 :            : #undef IFACE_STAT
    2332                 :            :     int64_t values[N_IFACE_STATS];
    2333                 :            :     const char *keys[N_IFACE_STATS];
    2334                 :            :     int n;
    2335                 :            : 
    2336                 :            :     struct netdev_stats stats;
    2337                 :            : 
    2338         [ -  + ]:       7977 :     if (iface_is_synthetic(iface)) {
    2339                 :          0 :         return;
    2340                 :            :     }
    2341                 :            : 
    2342                 :            :     /* Intentionally ignore return value, since errors will set 'stats' to
    2343                 :            :      * all-1s, and we will deal with that correctly below. */
    2344                 :       7977 :     netdev_get_stats(iface->netdev, &stats);
    2345                 :            : 
    2346                 :            :     /* Copy statistics into keys[] and values[]. */
    2347                 :       7977 :     n = 0;
    2348                 :            : #define IFACE_STAT(MEMBER, NAME)                \
    2349                 :            :     if (stats.MEMBER != UINT64_MAX) {           \
    2350                 :            :         keys[n] = NAME;                         \
    2351                 :            :         values[n] = stats.MEMBER;               \
    2352                 :            :         n++;                                    \
    2353                 :            :     }
    2354 [ +  - ][ +  - ]:       7977 :     IFACE_STATS;
         [ +  - ][ +  - ]
         [ +  + ][ +  + ]
         [ +  + ][ +  + ]
         [ +  + ][ +  + ]
         [ +  + ][ +  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
                 [ -  + ]
    2355                 :            : #undef IFACE_STAT
    2356         [ -  + ]:       7977 :     ovs_assert(n <= N_IFACE_STATS);
    2357                 :            : 
    2358                 :       7977 :     ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
    2359                 :            : #undef IFACE_STATS
    2360                 :            : }
    2361                 :            : 
    2362                 :            : static void
    2363                 :       6579 : br_refresh_datapath_info(struct bridge *br)
    2364                 :            : {
    2365                 :            :     const char *version;
    2366                 :            : 
    2367         [ +  - ]:       6579 :     version = (br->ofproto && br->ofproto->ofproto_class->get_datapath_version
    2368                 :       6579 :                ? br->ofproto->ofproto_class->get_datapath_version(br->ofproto)
    2369         [ +  - ]:      13158 :                : NULL);
    2370                 :            : 
    2371         [ +  - ]:       6579 :     ovsrec_bridge_set_datapath_version(br->cfg,
    2372                 :            :                                        version ? version : "<unknown>");
    2373                 :       6579 : }
    2374                 :            : 
    2375                 :            : static void
    2376                 :       6579 : br_refresh_stp_status(struct bridge *br)
    2377                 :            : {
    2378                 :       6579 :     struct smap smap = SMAP_INITIALIZER(&smap);
    2379                 :       6579 :     struct ofproto *ofproto = br->ofproto;
    2380                 :            :     struct ofproto_stp_status status;
    2381                 :            : 
    2382         [ -  + ]:       6579 :     if (ofproto_get_stp_status(ofproto, &status)) {
    2383                 :       6541 :         return;
    2384                 :            :     }
    2385                 :            : 
    2386         [ +  + ]:       6579 :     if (!status.enabled) {
    2387                 :       6541 :         ovsrec_bridge_set_status(br->cfg, NULL);
    2388                 :       6541 :         return;
    2389                 :            :     }
    2390                 :            : 
    2391                 :         38 :     smap_add_format(&smap, "stp_bridge_id", STP_ID_FMT,
    2392                 :         76 :                     STP_ID_ARGS(status.bridge_id));
    2393                 :         38 :     smap_add_format(&smap, "stp_designated_root", STP_ID_FMT,
    2394                 :         76 :                     STP_ID_ARGS(status.designated_root));
    2395                 :         38 :     smap_add_format(&smap, "stp_root_path_cost", "%d", status.root_path_cost);
    2396                 :            : 
    2397                 :         38 :     ovsrec_bridge_set_status(br->cfg, &smap);
    2398                 :         38 :     smap_destroy(&smap);
    2399                 :            : }
    2400                 :            : 
    2401                 :            : static void
    2402                 :      36359 : port_refresh_stp_status(struct port *port)
    2403                 :            : {
    2404                 :      36359 :     struct ofproto *ofproto = port->bridge->ofproto;
    2405                 :            :     struct iface *iface;
    2406                 :            :     struct ofproto_port_stp_status status;
    2407                 :            :     struct smap smap;
    2408                 :            : 
    2409         [ -  + ]:      36359 :     if (port_is_synthetic(port)) {
    2410                 :      36325 :         return;
    2411                 :            :     }
    2412                 :            : 
    2413                 :            :     /* STP doesn't currently support bonds. */
    2414         [ +  + ]:      36359 :     if (!ovs_list_is_singleton(&port->ifaces)) {
    2415                 :        102 :         ovsrec_port_set_status(port->cfg, NULL);
    2416                 :        102 :         return;
    2417                 :            :     }
    2418                 :            : 
    2419                 :      36257 :     iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
    2420         [ +  + ]:      36257 :     if (ofproto_port_get_stp_status(ofproto, iface->ofp_port, &status)) {
    2421                 :          1 :         return;
    2422                 :            :     }
    2423                 :            : 
    2424         [ +  + ]:      36256 :     if (!status.enabled) {
    2425                 :      36222 :         ovsrec_port_set_status(port->cfg, NULL);
    2426                 :      36222 :         return;
    2427                 :            :     }
    2428                 :            : 
    2429                 :            :     /* Set Status column. */
    2430                 :         34 :     smap_init(&smap);
    2431                 :         34 :     smap_add_format(&smap, "stp_port_id", STP_PORT_ID_FMT, status.port_id);
    2432                 :         34 :     smap_add(&smap, "stp_state", stp_state_name(status.state));
    2433                 :         34 :     smap_add_format(&smap, "stp_sec_in_state", "%u", status.sec_in_state);
    2434                 :         34 :     smap_add(&smap, "stp_role", stp_role_name(status.role));
    2435                 :         34 :     ovsrec_port_set_status(port->cfg, &smap);
    2436                 :         34 :     smap_destroy(&smap);
    2437                 :            : }
    2438                 :            : 
    2439                 :            : static void
    2440                 :       5264 : port_refresh_stp_stats(struct port *port)
    2441                 :            : {
    2442                 :       5264 :     struct ofproto *ofproto = port->bridge->ofproto;
    2443                 :            :     struct iface *iface;
    2444                 :            :     struct ofproto_port_stp_stats stats;
    2445                 :            :     const char *keys[3];
    2446                 :            :     int64_t int_values[3];
    2447                 :            : 
    2448         [ -  + ]:       5264 :     if (port_is_synthetic(port)) {
    2449                 :       5254 :         return;
    2450                 :            :     }
    2451                 :            : 
    2452                 :            :     /* STP doesn't currently support bonds. */
    2453         [ +  + ]:       5264 :     if (!ovs_list_is_singleton(&port->ifaces)) {
    2454                 :         24 :         return;
    2455                 :            :     }
    2456                 :            : 
    2457                 :       5240 :     iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
    2458         [ -  + ]:       5240 :     if (ofproto_port_get_stp_stats(ofproto, iface->ofp_port, &stats)) {
    2459                 :          0 :         return;
    2460                 :            :     }
    2461                 :            : 
    2462         [ +  + ]:       5240 :     if (!stats.enabled) {
    2463                 :       5230 :         ovsrec_port_set_statistics(port->cfg, NULL, NULL, 0);
    2464                 :       5230 :         return;
    2465                 :            :     }
    2466                 :            : 
    2467                 :            :     /* Set Statistics column. */
    2468                 :         10 :     keys[0] = "stp_tx_count";
    2469                 :         10 :     int_values[0] = stats.tx_count;
    2470                 :         10 :     keys[1] = "stp_rx_count";
    2471                 :         10 :     int_values[1] = stats.rx_count;
    2472                 :         10 :     keys[2] = "stp_error_count";
    2473                 :         10 :     int_values[2] = stats.error_count;
    2474                 :            : 
    2475                 :         10 :     ovsrec_port_set_statistics(port->cfg, keys, int_values,
    2476                 :            :                                ARRAY_SIZE(int_values));
    2477                 :            : }
    2478                 :            : 
    2479                 :            : static void
    2480                 :       6579 : br_refresh_rstp_status(struct bridge *br)
    2481                 :            : {
    2482                 :       6579 :     struct smap smap = SMAP_INITIALIZER(&smap);
    2483                 :       6579 :     struct ofproto *ofproto = br->ofproto;
    2484                 :            :     struct ofproto_rstp_status status;
    2485                 :            : 
    2486         [ -  + ]:       6579 :     if (ofproto_get_rstp_status(ofproto, &status)) {
    2487                 :       6554 :         return;
    2488                 :            :     }
    2489         [ +  + ]:       6579 :     if (!status.enabled) {
    2490                 :       6554 :         ovsrec_bridge_set_rstp_status(br->cfg, NULL);
    2491                 :       6554 :         return;
    2492                 :            :     }
    2493                 :         25 :     smap_add_format(&smap, "rstp_bridge_id", RSTP_ID_FMT,
    2494                 :         75 :                     RSTP_ID_ARGS(status.bridge_id));
    2495                 :         25 :     smap_add_format(&smap, "rstp_root_path_cost", "%"PRIu32,
    2496                 :            :                     status.root_path_cost);
    2497                 :         25 :     smap_add_format(&smap, "rstp_root_id", RSTP_ID_FMT,
    2498                 :         75 :                     RSTP_ID_ARGS(status.root_id));
    2499                 :         25 :     smap_add_format(&smap, "rstp_designated_id", RSTP_ID_FMT,
    2500                 :         75 :                     RSTP_ID_ARGS(status.designated_id));
    2501                 :         25 :     smap_add_format(&smap, "rstp_designated_port_id", RSTP_PORT_ID_FMT,
    2502                 :         25 :                     status.designated_port_id);
    2503                 :         25 :     smap_add_format(&smap, "rstp_bridge_port_id", RSTP_PORT_ID_FMT,
    2504                 :         25 :                     status.bridge_port_id);
    2505                 :         25 :     ovsrec_bridge_set_rstp_status(br->cfg, &smap);
    2506                 :         25 :     smap_destroy(&smap);
    2507                 :            : }
    2508                 :            : 
    2509                 :            : static void
    2510                 :      36359 : port_refresh_rstp_status(struct port *port)
    2511                 :            : {
    2512                 :      36359 :     struct ofproto *ofproto = port->bridge->ofproto;
    2513                 :            :     struct iface *iface;
    2514                 :            :     struct ofproto_port_rstp_status status;
    2515                 :            :     const char *keys[4];
    2516                 :            :     int64_t int_values[4];
    2517                 :            :     struct smap smap;
    2518                 :            : 
    2519         [ -  + ]:      36359 :     if (port_is_synthetic(port)) {
    2520                 :      36337 :         return;
    2521                 :            :     }
    2522                 :            : 
    2523                 :            :     /* RSTP doesn't currently support bonds. */
    2524         [ +  + ]:      36359 :     if (!ovs_list_is_singleton(&port->ifaces)) {
    2525                 :        102 :         ovsrec_port_set_rstp_status(port->cfg, NULL);
    2526                 :        102 :         return;
    2527                 :            :     }
    2528                 :            : 
    2529                 :      36257 :     iface = CONTAINER_OF(ovs_list_front(&port->ifaces), struct iface, port_elem);
    2530         [ +  + ]:      36257 :     if (ofproto_port_get_rstp_status(ofproto, iface->ofp_port, &status)) {
    2531                 :          1 :         return;
    2532                 :            :     }
    2533                 :            : 
    2534         [ +  + ]:      36256 :     if (!status.enabled) {
    2535                 :      36234 :         ovsrec_port_set_rstp_status(port->cfg, NULL);
    2536                 :      36234 :         ovsrec_port_set_rstp_statistics(port->cfg, NULL, NULL, 0);
    2537                 :      36234 :         return;
    2538                 :            :     }
    2539                 :            :     /* Set Status column. */
    2540                 :         22 :     smap_init(&smap);
    2541                 :            : 
    2542                 :         22 :     smap_add_format(&smap, "rstp_port_id", RSTP_PORT_ID_FMT,
    2543                 :         22 :                     status.port_id);
    2544                 :         22 :     smap_add_format(&smap, "rstp_port_role", "%s",
    2545                 :            :                     rstp_port_role_name(status.role));
    2546                 :         22 :     smap_add_format(&smap, "rstp_port_state", "%s",
    2547                 :            :                     rstp_state_name(status.state));
    2548                 :         22 :     smap_add_format(&smap, "rstp_designated_bridge_id", RSTP_ID_FMT,
    2549                 :         66 :                     RSTP_ID_ARGS(status.designated_bridge_id));
    2550                 :         22 :     smap_add_format(&smap, "rstp_designated_port_id", RSTP_PORT_ID_FMT,
    2551                 :         22 :                     status.designated_port_id);
    2552                 :         22 :     smap_add_format(&smap, "rstp_designated_path_cost", "%"PRIu32,
    2553                 :            :                     status.designated_path_cost);
    2554                 :            : 
    2555                 :         22 :     ovsrec_port_set_rstp_status(port->cfg, &smap);
    2556                 :         22 :     smap_destroy(&smap);
    2557                 :            : 
    2558                 :            :     /* Set Statistics column. */
    2559                 :         22 :     keys[0] = "rstp_tx_count";
    2560                 :         22 :     int_values[0] = status.tx_count;
    2561                 :         22 :     keys[1] = "rstp_rx_count";
    2562                 :         22 :     int_values[1] = status.rx_count;
    2563                 :         22 :     keys[2] = "rstp_uptime";
    2564                 :         22 :     int_values[2] = status.uptime;
    2565                 :         22 :     keys[3] = "rstp_error_count";
    2566                 :         22 :     int_values[3] = status.error_count;
    2567                 :         22 :     ovsrec_port_set_rstp_statistics(port->cfg, keys, int_values,
    2568                 :            :             ARRAY_SIZE(int_values));
    2569                 :            : }
    2570                 :            : 
    2571                 :            : static void
    2572                 :      36359 : port_refresh_bond_status(struct port *port, bool force_update)
    2573                 :            : {
    2574                 :            :     struct eth_addr mac;
    2575                 :            : 
    2576                 :            :     /* Return if port is not a bond */
    2577         [ +  + ]:      36359 :     if (ovs_list_is_singleton(&port->ifaces)) {
    2578                 :      36257 :         return;
    2579                 :            :     }
    2580                 :            : 
    2581         [ +  + ]:        102 :     if (bond_get_changed_active_slave(port->name, &mac, force_update)) {
    2582                 :            :         struct ds mac_s;
    2583                 :            : 
    2584                 :         46 :         ds_init(&mac_s);
    2585                 :         46 :         ds_put_format(&mac_s, ETH_ADDR_FMT, ETH_ADDR_ARGS(mac));
    2586                 :         46 :         ovsrec_port_set_bond_active_slave(port->cfg, ds_cstr(&mac_s));
    2587                 :        102 :         ds_destroy(&mac_s);
    2588                 :            :     }
    2589                 :            : }
    2590                 :            : 
    2591                 :            : static bool
    2592                 :       4238 : enable_system_stats(const struct ovsrec_open_vswitch *cfg)
    2593                 :            : {
    2594                 :       4238 :     return smap_get_bool(&cfg->other_config, "enable-statistics", false);
    2595                 :            : }
    2596                 :            : 
    2597                 :            : static void
    2598                 :       4238 : reconfigure_system_stats(const struct ovsrec_open_vswitch *cfg)
    2599                 :            : {
    2600                 :       4238 :     bool enable = enable_system_stats(cfg);
    2601                 :            : 
    2602                 :       4238 :     system_stats_enable(enable);
    2603         [ +  - ]:       4238 :     if (!enable) {
    2604                 :       4238 :         ovsrec_open_vswitch_set_statistics(cfg, NULL);
    2605                 :            :     }
    2606                 :       4238 : }
    2607                 :            : 
    2608                 :            : static void
    2609                 :     103919 : run_system_stats(void)
    2610                 :            : {
    2611                 :     103919 :     const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(idl);
    2612                 :            :     struct smap *stats;
    2613                 :            : 
    2614                 :     103919 :     stats = system_stats_run();
    2615 [ -  + ][ #  # ]:     103919 :     if (stats && cfg) {
    2616                 :            :         struct ovsdb_idl_txn *txn;
    2617                 :            :         struct ovsdb_datum datum;
    2618                 :            : 
    2619                 :          0 :         txn = ovsdb_idl_txn_create(idl);
    2620                 :          0 :         ovsdb_datum_from_smap(&datum, stats);
    2621                 :          0 :         ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
    2622                 :            :                             &datum);
    2623                 :          0 :         ovsdb_idl_txn_commit(txn);
    2624                 :          0 :         ovsdb_idl_txn_destroy(txn);
    2625                 :            : 
    2626                 :          0 :         free(stats);
    2627                 :            :     }
    2628                 :     103919 : }
    2629                 :            : 
    2630                 :            : static const char *
    2631                 :          2 : ofp12_controller_role_to_str(enum ofp12_controller_role role)
    2632                 :            : {
    2633   [ +  -  -  - ]:          2 :     switch (role) {
    2634                 :            :     case OFPCR12_ROLE_EQUAL:
    2635                 :          2 :         return "other";
    2636                 :            :     case OFPCR12_ROLE_MASTER:
    2637                 :          0 :         return "master";
    2638                 :            :     case OFPCR12_ROLE_SLAVE:
    2639                 :          0 :         return "slave";
    2640                 :            :     case OFPCR12_ROLE_NOCHANGE:
    2641                 :            :     default:
    2642                 :          0 :         return "*** INVALID ROLE ***";
    2643                 :            :     }
    2644                 :            : }
    2645                 :            : 
    2646                 :            : static void
    2647                 :       1178 : refresh_controller_status(void)
    2648                 :            : {
    2649                 :            :     struct bridge *br;
    2650                 :            :     struct shash info;
    2651                 :            :     const struct ovsrec_controller *cfg;
    2652                 :            : 
    2653                 :       1178 :     shash_init(&info);
    2654                 :            : 
    2655                 :            :     /* Accumulate status for controllers on all bridges. */
    2656 [ +  + ][ -  + ]:       2079 :     HMAP_FOR_EACH (br, node, &all_bridges) {
    2657                 :        901 :         ofproto_get_ofproto_controller_info(br->ofproto, &info);
    2658                 :            :     }
    2659                 :            : 
    2660                 :            :     /* Update each controller in the database with current status. */
    2661         [ +  + ]:       1180 :     OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
    2662                 :          2 :         struct ofproto_controller_info *cinfo =
    2663                 :          2 :             shash_find_data(&info, cfg->target);
    2664                 :            : 
    2665         [ +  - ]:          2 :         if (cinfo) {
    2666                 :          2 :             ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
    2667                 :          2 :             ovsrec_controller_set_role(cfg, ofp12_controller_role_to_str(
    2668                 :            :                                            cinfo->role));
    2669                 :          2 :             ovsrec_controller_set_status(cfg, &cinfo->pairs);
    2670                 :            :         } else {
    2671                 :          0 :             ovsrec_controller_set_is_connected(cfg, false);
    2672                 :          0 :             ovsrec_controller_set_role(cfg, NULL);
    2673                 :          0 :             ovsrec_controller_set_status(cfg, NULL);
    2674                 :            :         }
    2675                 :            :     }
    2676                 :            : 
    2677                 :       1178 :     ofproto_free_ofproto_controller_info(&info);
    2678                 :       1178 : }
    2679                 :            : 
    2680                 :            : /* Update interface and mirror statistics if necessary. */
    2681                 :            : static void
    2682                 :     103919 : run_stats_update(void)
    2683                 :            : {
    2684                 :     103919 :     const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(idl);
    2685                 :            :     int stats_interval;
    2686                 :            : 
    2687         [ +  + ]:     103919 :     if (!cfg) {
    2688                 :          8 :         return;
    2689                 :            :     }
    2690                 :            : 
    2691                 :            :     /* Statistics update interval should always be greater than or equal to
    2692                 :            :      * 5000 ms. */
    2693         [ +  + ]:     103911 :     stats_interval = MAX(smap_get_int(&cfg->other_config,
    2694                 :            :                                       "stats-update-interval",
    2695                 :            :                                       5000), 5000);
    2696         [ +  + ]:     103911 :     if (stats_timer_interval != stats_interval) {
    2697                 :        617 :         stats_timer_interval = stats_interval;
    2698                 :        617 :         stats_timer = LLONG_MIN;
    2699                 :            :     }
    2700                 :            : 
    2701         [ +  + ]:     103911 :     if (time_msec() >= stats_timer) {
    2702                 :            :         enum ovsdb_idl_txn_status status;
    2703                 :            : 
    2704                 :            :         /* Rate limit the update.  Do not start a new update if the
    2705                 :            :          * previous one is not done. */
    2706         [ +  + ]:       1637 :         if (!stats_txn) {
    2707                 :            :             struct bridge *br;
    2708                 :            : 
    2709                 :       1178 :             stats_txn = ovsdb_idl_txn_create(idl);
    2710 [ +  + ][ -  + ]:       2079 :             HMAP_FOR_EACH (br, node, &all_bridges) {
    2711                 :            :                 struct port *port;
    2712                 :            :                 struct mirror *m;
    2713                 :            : 
    2714 [ +  + ][ -  + ]:       6165 :                 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
    2715                 :            :                     struct iface *iface;
    2716                 :            : 
    2717         [ +  + ]:      10552 :                     LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
    2718                 :       5288 :                         iface_refresh_stats(iface);
    2719                 :            :                     }
    2720                 :       5264 :                     port_refresh_stp_stats(port);
    2721                 :            :                 }
    2722 [ -  + ][ -  + ]:        901 :                 HMAP_FOR_EACH (m, hmap_node, &br->mirrors) {
    2723                 :          0 :                     mirror_refresh_stats(m);
    2724                 :            :                 }
    2725                 :            :             }
    2726                 :       1178 :             refresh_controller_status();
    2727                 :            :         }
    2728                 :            : 
    2729                 :       1637 :         status = ovsdb_idl_txn_commit(stats_txn);
    2730         [ +  + ]:       1637 :         if (status != TXN_INCOMPLETE) {
    2731                 :       1178 :             stats_timer = time_msec() + stats_timer_interval;
    2732                 :       1178 :             ovsdb_idl_txn_destroy(stats_txn);
    2733                 :       1178 :             stats_txn = NULL;
    2734                 :            :         }
    2735                 :            :     }
    2736                 :            : }
    2737                 :            : 
    2738                 :            : static void
    2739                 :     102689 : stats_update_wait(void)
    2740                 :            : {
    2741                 :            :     /* If the 'stats_txn' is non-null (transaction incomplete), waits for the
    2742                 :            :      * transaction to complete.  Otherwise, waits for the 'stats_timer'. */
    2743         [ +  + ]:     102689 :     if (stats_txn) {
    2744                 :        459 :         ovsdb_idl_txn_wait(stats_txn);
    2745                 :            :     } else {
    2746                 :     102230 :         poll_timer_wait_until(stats_timer);
    2747                 :            :     }
    2748                 :     102689 : }
    2749                 :            : 
    2750                 :            : /* Update bridge/port/interface status if necessary. */
    2751                 :            : static void
    2752                 :     103919 : run_status_update(void)
    2753                 :            : {
    2754         [ +  + ]:     103919 :     if (!status_txn) {
    2755                 :            :         uint64_t seq;
    2756                 :            : 
    2757                 :            :         /* Rate limit the update.  Do not start a new update if the
    2758                 :            :          * previous one is not done. */
    2759                 :     100693 :         seq = seq_read(connectivity_seq_get());
    2760 [ +  + ][ +  + ]:     100693 :         if (seq != connectivity_seqno || status_txn_try_again) {
    2761                 :            :             struct bridge *br;
    2762                 :            : 
    2763                 :       5693 :             connectivity_seqno = seq;
    2764                 :       5693 :             status_txn = ovsdb_idl_txn_create(idl);
    2765 [ +  + ][ -  + ]:      12272 :             HMAP_FOR_EACH (br, node, &all_bridges) {
    2766                 :            :                 struct port *port;
    2767                 :            : 
    2768                 :       6579 :                 br_refresh_stp_status(br);
    2769                 :       6579 :                 br_refresh_rstp_status(br);
    2770                 :       6579 :                 br_refresh_datapath_info(br);
    2771 [ +  + ][ -  + ]:      42938 :                 HMAP_FOR_EACH (port, hmap_node, &br->ports) {
    2772                 :            :                     struct iface *iface;
    2773                 :            : 
    2774                 :      36359 :                     port_refresh_stp_status(port);
    2775                 :      36359 :                     port_refresh_rstp_status(port);
    2776                 :      36359 :                     port_refresh_bond_status(port, status_txn_try_again);
    2777         [ +  + ]:      72828 :                     LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
    2778                 :      36469 :                         iface_refresh_netdev_status(iface);
    2779                 :      36469 :                         iface_refresh_ofproto_status(iface);
    2780                 :            :                     }
    2781                 :            :                 }
    2782                 :            :             }
    2783                 :            :         }
    2784                 :            :     }
    2785                 :            : 
    2786                 :            :     /* Commit the transaction and get the status. If the transaction finishes,
    2787                 :            :      * then destroy the transaction. Otherwise, keep it so that we can check
    2788                 :            :      * progress the next time that this function is called. */
    2789         [ +  + ]:     103919 :     if (status_txn) {
    2790                 :            :         enum ovsdb_idl_txn_status status;
    2791                 :            : 
    2792                 :       8919 :         status = ovsdb_idl_txn_commit(status_txn);
    2793         [ +  + ]:       8919 :         if (status != TXN_INCOMPLETE) {
    2794                 :       5693 :             ovsdb_idl_txn_destroy(status_txn);
    2795                 :       5693 :             status_txn = NULL;
    2796                 :            : 
    2797                 :            :             /* Sets the 'status_txn_try_again' if the transaction fails. */
    2798 [ +  + ][ +  - ]:       5693 :             if (status == TXN_SUCCESS || status == TXN_UNCHANGED) {
    2799                 :       5693 :                 status_txn_try_again = false;
    2800                 :            :             } else {
    2801                 :          0 :                 status_txn_try_again = true;
    2802                 :            :             }
    2803                 :            :         }
    2804                 :            :     }
    2805                 :            : 
    2806                 :            :     /* Refresh AA port status if necessary. */
    2807         [ +  + ]:     103919 :     if (time_msec() >= aa_refresh_timer) {
    2808                 :            :         struct bridge *br;
    2809                 :            : 
    2810 [ +  + ][ -  + ]:       9078 :         HMAP_FOR_EACH (br, node, &all_bridges) {
    2811         [ -  + ]:       5132 :             if (bridge_aa_need_refresh(br)) {
    2812                 :            :                 struct ovsdb_idl_txn *txn;
    2813                 :            : 
    2814                 :          0 :                 txn = ovsdb_idl_txn_create(idl);
    2815                 :          0 :                 bridge_aa_refresh_queued(br);
    2816                 :          0 :                 ovsdb_idl_txn_commit(txn);
    2817                 :          0 :                 ovsdb_idl_txn_destroy(txn);
    2818                 :            :             }
    2819                 :            :         }
    2820                 :            : 
    2821                 :       3946 :         aa_refresh_timer = time_msec() + AA_REFRESH_INTERVAL;
    2822                 :            :     }
    2823                 :     103919 : }
    2824                 :            : 
    2825                 :            : static void
    2826                 :     102689 : status_update_wait(void)
    2827                 :            : {
    2828                 :            :     /* If the 'status_txn' is non-null (transaction incomplete), waits for the
    2829                 :            :      * transaction to complete.  If the status update to database needs to be
    2830                 :            :      * run again (transaction fails), registers a timeout in
    2831                 :            :      * 'STATUS_CHECK_AGAIN_MSEC'.  Otherwise, waits on the global connectivity
    2832                 :            :      * sequence number. */
    2833         [ +  + ]:     102689 :     if (status_txn) {
    2834                 :       3226 :         ovsdb_idl_txn_wait(status_txn);
    2835         [ -  + ]:      99463 :     } else if (status_txn_try_again) {
    2836                 :          0 :         poll_timer_wait_until(time_msec() + STATUS_CHECK_AGAIN_MSEC);
    2837                 :            :     } else {
    2838                 :      99463 :         seq_wait(connectivity_seq_get(), connectivity_seqno);
    2839                 :            :     }
    2840                 :     102689 : }
    2841                 :            : 
    2842                 :            : static void
    2843                 :     108157 : bridge_run__(void)
    2844                 :            : {
    2845                 :            :     struct bridge *br;
    2846                 :            :     struct sset types;
    2847                 :            :     const char *type;
    2848                 :            : 
    2849                 :            :     /* Let each datapath type do the work that it needs to do. */
    2850                 :     108157 :     sset_init(&types);
    2851                 :     108157 :     ofproto_enumerate_types(&types);
    2852 [ +  - ][ +  + ]:     354002 :     SSET_FOR_EACH (type, &types) {
                 [ +  + ]
    2853                 :     245845 :         ofproto_type_run(type);
    2854                 :            :     }
    2855                 :     108157 :     sset_destroy(&types);
    2856                 :            : 
    2857                 :            :     /* Let each bridge do the work that it needs to do. */
    2858 [ +  + ][ -  + ]:     265762 :     HMAP_FOR_EACH (br, node, &all_bridges) {
    2859                 :     157605 :         ofproto_run(br->ofproto);
    2860                 :            :     }
    2861                 :     108157 : }
    2862                 :            : 
    2863                 :            : void
    2864                 :     105174 : bridge_run(void)
    2865                 :            : {
    2866                 :            :     static struct ovsrec_open_vswitch null_cfg;
    2867                 :            :     const struct ovsrec_open_vswitch *cfg;
    2868                 :            : 
    2869                 :     105174 :     ovsrec_open_vswitch_init(&null_cfg);
    2870                 :            : 
    2871                 :     105174 :     ovsdb_idl_run(idl);
    2872                 :            : 
    2873                 :     105174 :     if_notifier_run();
    2874                 :            : 
    2875         [ +  + ]:     105174 :     if (ovsdb_idl_is_lock_contended(idl)) {
    2876                 :            :         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
    2877                 :            :         struct bridge *br, *next_br;
    2878                 :            : 
    2879         [ +  - ]:          5 :         VLOG_ERR_RL(&rl, "another ovs-vswitchd process is running, "
    2880                 :            :                     "disabling this process (pid %ld) until it goes away",
    2881                 :            :                     (long int) getpid());
    2882                 :            : 
    2883 [ +  - ][ -  + ]:          5 :         HMAP_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
                 [ -  + ]
    2884                 :          0 :             bridge_destroy(br, false);
    2885                 :            :         }
    2886                 :            :         /* Since we will not be running system_stats_run() in this process
    2887                 :            :          * with the current situation of multiple ovs-vswitchd daemons,
    2888                 :            :          * disable system stats collection. */
    2889                 :          5 :         system_stats_enable(false);
    2890                 :          5 :         return;
    2891         [ +  + ]:     105169 :     } else if (!ovsdb_idl_has_lock(idl)
    2892         [ +  + ]:     104062 :                || !ovsdb_idl_has_ever_connected(idl)) {
    2893                 :            :         /* Returns if not holding the lock or not done retrieving db
    2894                 :            :          * contents. */
    2895                 :       1250 :         return;
    2896                 :            :     }
    2897                 :     103919 :     cfg = ovsrec_open_vswitch_first(idl);
    2898                 :            : 
    2899         [ +  + ]:     103919 :     if (cfg) {
    2900                 :     103911 :         dpdk_init(&cfg->other_config);
    2901                 :            :     }
    2902                 :            : 
    2903                 :            :     /* Initialize the ofproto library.  This only needs to run once, but
    2904                 :            :      * it must be done after the configuration is set.  If the
    2905                 :            :      * initialization has already occurred, bridge_init_ofproto()
    2906                 :            :      * returns immediately. */
    2907                 :     103919 :     bridge_init_ofproto(cfg);
    2908                 :            : 
    2909                 :            :     /* Once the value of flow-restore-wait is false, we no longer should
    2910                 :            :      * check its value from the database. */
    2911 [ +  + ][ +  + ]:     103919 :     if (cfg && ofproto_get_flow_restore_wait()) {
    2912                 :        653 :         ofproto_set_flow_restore_wait(smap_get_bool(&cfg->other_config,
    2913                 :            :                                         "flow-restore-wait", false));
    2914                 :            :     }
    2915                 :            : 
    2916                 :     103919 :     bridge_run__();
    2917                 :            : 
    2918                 :            :     /* Re-configure SSL.  We do this on every trip through the main loop,
    2919                 :            :      * instead of just when the database changes, because the contents of the
    2920                 :            :      * key and certificate files can change without the database changing.
    2921                 :            :      *
    2922                 :            :      * We do this before bridge_reconfigure() because that function might
    2923                 :            :      * initiate SSL connections and thus requires SSL to be configured. */
    2924 [ +  + ][ +  + ]:     103919 :     if (cfg && cfg->ssl) {
    2925                 :          7 :         const struct ovsrec_ssl *ssl = cfg->ssl;
    2926                 :            : 
    2927                 :          7 :         stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
    2928                 :          7 :         stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
    2929                 :            :     }
    2930                 :            : 
    2931 [ +  + ][ +  + ]:     103919 :     if (ovsdb_idl_get_seqno(idl) != idl_seqno || ifaces_changed) {
    2932                 :            :         struct ovsdb_idl_txn *txn;
    2933                 :            : 
    2934                 :       4238 :         ifaces_changed = false;
    2935                 :            : 
    2936                 :       4238 :         idl_seqno = ovsdb_idl_get_seqno(idl);
    2937                 :       4238 :         txn = ovsdb_idl_txn_create(idl);
    2938         [ +  + ]:       4238 :         bridge_reconfigure(cfg ? cfg : &null_cfg);
    2939                 :            : 
    2940         [ +  + ]:       4238 :         if (cfg) {
    2941                 :       4231 :             ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
    2942                 :       4231 :             discover_types(cfg);
    2943                 :            :         }
    2944                 :            : 
    2945                 :            :         /* If we are completing our initial configuration for this run
    2946                 :            :          * of ovs-vswitchd, then keep the transaction around to monitor
    2947                 :            :          * it for completion. */
    2948         [ +  + ]:       4238 :         if (initial_config_done) {
    2949                 :            :             /* Always sets the 'status_txn_try_again' to check again,
    2950                 :            :              * in case that this transaction fails. */
    2951                 :       3623 :             status_txn_try_again = true;
    2952                 :       3623 :             ovsdb_idl_txn_commit(txn);
    2953                 :       3623 :             ovsdb_idl_txn_destroy(txn);
    2954                 :            :         } else {
    2955                 :        615 :             initial_config_done = true;
    2956                 :        615 :             daemonize_txn = txn;
    2957                 :            :         }
    2958                 :            :     }
    2959                 :            : 
    2960         [ +  + ]:     103919 :     if (daemonize_txn) {
    2961                 :       1223 :         enum ovsdb_idl_txn_status status = ovsdb_idl_txn_commit(daemonize_txn);
    2962         [ +  + ]:       1223 :         if (status != TXN_INCOMPLETE) {
    2963                 :        615 :             ovsdb_idl_txn_destroy(daemonize_txn);
    2964                 :        615 :             daemonize_txn = NULL;
    2965                 :            : 
    2966                 :            :             /* ovs-vswitchd has completed initialization, so allow the
    2967                 :            :              * process that forked us to exit successfully. */
    2968                 :        615 :             daemonize_complete();
    2969                 :            : 
    2970                 :        615 :             vlog_enable_async();
    2971                 :            : 
    2972         [ +  - ]:        615 :             VLOG_INFO_ONCE("%s (Open vSwitch) %s", program_name, VERSION);
    2973                 :            :         }
    2974                 :            :     }
    2975                 :            : 
    2976                 :     103919 :     run_stats_update();
    2977                 :     103919 :     run_status_update();
    2978                 :     103919 :     run_system_stats();
    2979                 :            : }
    2980                 :            : 
    2981                 :            : void
    2982                 :     105174 : bridge_wait(void)
    2983                 :            : {
    2984                 :            :     struct sset types;
    2985                 :            :     const char *type;
    2986                 :            : 
    2987                 :     105174 :     ovsdb_idl_wait(idl);
    2988         [ +  + ]:     105174 :     if (daemonize_txn) {
    2989                 :        608 :         ovsdb_idl_txn_wait(daemonize_txn);
    2990                 :            :     }
    2991                 :            : 
    2992                 :     105174 :     if_notifier_wait();
    2993         [ -  + ]:     105174 :     if (ifaces_changed) {
    2994                 :          0 :         poll_immediate_wake();
    2995                 :            :     }
    2996                 :            : 
    2997                 :     105174 :     sset_init(&types);
    2998                 :     105174 :     ofproto_enumerate_types(&types);
    2999 [ +  + ][ +  + ]:     342618 :     SSET_FOR_EACH (type, &types) {
                 [ +  + ]
    3000                 :     237444 :         ofproto_type_wait(type);
    3001                 :            :     }
    3002                 :     105174 :     sset_destroy(&types);
    3003                 :            : 
    3004         [ +  + ]:     105174 :     if (!hmap_is_empty(&all_bridges)) {
    3005                 :            :         struct bridge *br;
    3006                 :            : 
    3007 [ +  + ][ -  + ]:     256334 :         HMAP_FOR_EACH (br, node, &all_bridges) {
    3008                 :     153645 :             ofproto_wait(br->ofproto);
    3009                 :            :         }
    3010                 :     102689 :         stats_update_wait();
    3011                 :     102689 :         status_update_wait();
    3012                 :            :     }
    3013                 :            : 
    3014                 :     105174 :     system_stats_wait();
    3015                 :     105174 : }
    3016                 :            : 
    3017                 :            : /* Adds some memory usage statistics for bridges into 'usage', for use with
    3018                 :            :  * memory_report(). */
    3019                 :            : void
    3020                 :         73 : bridge_get_memory_usage(struct simap *usage)
    3021                 :            : {
    3022                 :            :     struct bridge *br;
    3023                 :            :     struct sset types;
    3024                 :            :     const char *type;
    3025                 :            : 
    3026                 :         73 :     sset_init(&types);
    3027                 :         73 :     ofproto_enumerate_types(&types);
    3028 [ +  + ][ +  + ]:        235 :     SSET_FOR_EACH (type, &types) {
                 [ +  + ]
    3029                 :        162 :         ofproto_type_get_memory_usage(type, usage);
    3030                 :            :     }
    3031                 :         73 :     sset_destroy(&types);
    3032                 :            : 
    3033 [ +  + ][ -  + ]:        182 :     HMAP_FOR_EACH (br, node, &all_bridges) {
    3034                 :        109 :         ofproto_get_memory_usage(br->ofproto, usage);
    3035                 :            :     }
    3036                 :         73 : }
    3037                 :            : 
    3038                 :            : /* QoS unixctl user interface functions. */
    3039                 :            : 
    3040                 :            : struct qos_unixctl_show_cbdata {
    3041                 :            :     struct ds *ds;
    3042                 :            :     struct iface *iface;
    3043                 :            : };
    3044                 :            : 
    3045                 :            : static void
    3046                 :          0 : qos_unixctl_show_queue(unsigned int queue_id,
    3047                 :            :                        const struct smap *details,
    3048                 :            :                        struct iface *iface,
    3049                 :            :                        struct ds *ds)
    3050                 :            : {
    3051                 :            :     struct netdev_queue_stats stats;
    3052                 :            :     struct smap_node *node;
    3053                 :            :     int error;
    3054                 :            : 
    3055                 :          0 :     ds_put_cstr(ds, "\n");
    3056         [ #  # ]:          0 :     if (queue_id) {
    3057                 :          0 :         ds_put_format(ds, "Queue %u:\n", queue_id);
    3058                 :            :     } else {
    3059                 :          0 :         ds_put_cstr(ds, "Default:\n");
    3060                 :            :     }
    3061                 :            : 
    3062 [ #  # ][ #  # ]:          0 :     SMAP_FOR_EACH (node, details) {
    3063                 :          0 :         ds_put_format(ds, "\t%s: %s\n", node->key, node->value);
    3064                 :            :     }
    3065                 :            : 
    3066                 :          0 :     error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
    3067         [ #  # ]:          0 :     if (!error) {
    3068         [ #  # ]:          0 :         if (stats.tx_packets != UINT64_MAX) {
    3069                 :          0 :             ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
    3070                 :            :         }
    3071                 :            : 
    3072         [ #  # ]:          0 :         if (stats.tx_bytes != UINT64_MAX) {
    3073                 :          0 :             ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
    3074                 :            :         }
    3075                 :            : 
    3076         [ #  # ]:          0 :         if (stats.tx_errors != UINT64_MAX) {
    3077                 :          0 :             ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
    3078                 :            :         }
    3079                 :            :     } else {
    3080                 :          0 :         ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
    3081                 :            :                       queue_id, ovs_strerror(error));
    3082                 :            :     }
    3083                 :          0 : }
    3084                 :            : 
    3085                 :            : static void
    3086                 :          0 : qos_unixctl_show_types(struct unixctl_conn *conn, int argc OVS_UNUSED,
    3087                 :            :                        const char *argv[], void *aux OVS_UNUSED)
    3088                 :            : {
    3089                 :          0 :     struct ds ds = DS_EMPTY_INITIALIZER;
    3090                 :          0 :     struct sset types = SSET_INITIALIZER(&types);
    3091                 :            :     struct iface *iface;
    3092                 :            :     const char * types_name;
    3093                 :            :     int error;
    3094                 :            : 
    3095                 :          0 :     iface = iface_find(argv[1]);
    3096         [ #  # ]:          0 :     if (!iface) {
    3097                 :          0 :         unixctl_command_reply_error(conn, "no such interface");
    3098                 :          0 :         return;
    3099                 :            :     }
    3100                 :            : 
    3101                 :          0 :     error = netdev_get_qos_types(iface->netdev, &types);
    3102         [ #  # ]:          0 :     if (!error) {
    3103         [ #  # ]:          0 :         if (!sset_is_empty(&types)) {
    3104 [ #  # ][ #  # ]:          0 :             SSET_FOR_EACH (types_name, &types) {
                 [ #  # ]
    3105                 :          0 :                 ds_put_format(&ds, "QoS type: %s\n", types_name);
    3106                 :            :             }
    3107                 :          0 :             unixctl_command_reply(conn, ds_cstr(&ds));
    3108                 :            :         } else {
    3109                 :          0 :             ds_put_format(&ds, "No QoS types supported for interface: %s\n",
    3110                 :            :                           iface->name);
    3111                 :          0 :             unixctl_command_reply(conn, ds_cstr(&ds));
    3112                 :            :         }
    3113                 :            :     } else {
    3114                 :          0 :         ds_put_format(&ds, "%s: failed to retrieve supported QoS types (%s)",
    3115                 :            :                       iface->name, ovs_strerror(error));
    3116                 :          0 :         unixctl_command_reply_error(conn, ds_cstr(&ds));
    3117                 :            :     }
    3118                 :            : 
    3119                 :          0 :     sset_destroy(&types);
    3120                 :          0 :     ds_destroy(&ds);
    3121                 :            : }
    3122                 :            : 
    3123                 :            : static void
    3124                 :          0 : qos_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
    3125                 :            :                  const char *argv[], void *aux OVS_UNUSED)
    3126                 :            : {
    3127                 :          0 :     struct ds ds = DS_EMPTY_INITIALIZER;
    3128                 :          0 :     struct smap smap = SMAP_INITIALIZER(&smap);
    3129                 :            :     struct iface *iface;
    3130                 :            :     const char *type;
    3131                 :            :     struct smap_node *node;
    3132                 :            :     int error;
    3133                 :            : 
    3134                 :          0 :     iface = iface_find(argv[1]);
    3135         [ #  # ]:          0 :     if (!iface) {
    3136                 :          0 :         unixctl_command_reply_error(conn, "no such interface");
    3137                 :          0 :         return;
    3138                 :            :     }
    3139                 :            : 
    3140                 :          0 :     error = netdev_get_qos(iface->netdev, &type, &smap);
    3141         [ #  # ]:          0 :     if (!error) {
    3142         [ #  # ]:          0 :         if (*type != '\0') {
    3143                 :            :             struct netdev_queue_dump dump;
    3144                 :            :             struct smap details;
    3145                 :            :             unsigned int queue_id;
    3146                 :            : 
    3147                 :          0 :             ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
    3148                 :            : 
    3149 [ #  # ][ #  # ]:          0 :             SMAP_FOR_EACH (node, &smap) {
    3150                 :          0 :                 ds_put_format(&ds, "%s: %s\n", node->key, node->value);
    3151                 :            :             }
    3152                 :            : 
    3153                 :          0 :             smap_init(&details);
    3154 [ #  # ][ #  # ]:          0 :             NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &dump, iface->netdev) {
    3155                 :          0 :                 qos_unixctl_show_queue(queue_id, &details, iface, &ds);
    3156                 :            :             }
    3157                 :          0 :             smap_destroy(&details);
    3158                 :            : 
    3159                 :          0 :             unixctl_command_reply(conn, ds_cstr(&ds));
    3160                 :            :         } else {
    3161                 :          0 :             ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
    3162                 :          0 :             unixctl_command_reply(conn, ds_cstr(&ds));
    3163                 :            :         }
    3164                 :            :     } else {
    3165                 :          0 :         ds_put_format(&ds, "%s: failed to retrieve QOS configuration (%s)\n",
    3166                 :            :                       iface->name, ovs_strerror(error));
    3167                 :          0 :         unixctl_command_reply_error(conn, ds_cstr(&ds));
    3168                 :            :     }
    3169                 :            : 
    3170                 :          0 :     smap_destroy(&smap);
    3171                 :          0 :     ds_destroy(&ds);
    3172                 :            : }
    3173                 :            : 
    3174                 :            : /* Bridge reconfiguration functions. */
    3175                 :            : static void
    3176                 :        752 : bridge_create(const struct ovsrec_bridge *br_cfg)
    3177                 :            : {
    3178                 :            :     struct bridge *br;
    3179                 :            : 
    3180         [ -  + ]:        752 :     ovs_assert(!bridge_lookup(br_cfg->name));
    3181                 :        752 :     br = xzalloc(sizeof *br);
    3182                 :            : 
    3183                 :        752 :     br->name = xstrdup(br_cfg->name);
    3184                 :        752 :     br->type = xstrdup(ofproto_normalize_type(br_cfg->datapath_type));
    3185                 :        752 :     br->cfg = br_cfg;
    3186                 :            : 
    3187                 :            :     /* Derive the default Ethernet address from the bridge's UUID.  This should
    3188                 :            :      * be unique and it will be stable between ovs-vswitchd runs.  */
    3189                 :        752 :     memcpy(&br->default_ea, &br_cfg->header_.uuid, ETH_ADDR_LEN);
    3190                 :        752 :     eth_addr_mark_random(&br->default_ea);
    3191                 :            : 
    3192                 :        752 :     hmap_init(&br->ports);
    3193                 :        752 :     hmap_init(&br->ifaces);
    3194                 :        752 :     hmap_init(&br->iface_by_name);
    3195                 :        752 :     hmap_init(&br->mirrors);
    3196                 :            : 
    3197                 :        752 :     hmap_init(&br->mappings);
    3198                 :        752 :     hmap_insert(&all_bridges, &br->node, hash_string(br->name, 0));
    3199                 :        752 : }
    3200                 :            : 
    3201                 :            : static void
    3202                 :        752 : bridge_destroy(struct bridge *br, bool del)
    3203                 :            : {
    3204         [ +  - ]:        752 :     if (br) {
    3205                 :            :         struct mirror *mirror, *next_mirror;
    3206                 :            :         struct port *port, *next_port;
    3207                 :            : 
    3208 [ +  + ][ -  + ]:       3032 :         HMAP_FOR_EACH_SAFE (port, next_port, hmap_node, &br->ports) {
                 [ +  + ]
    3209                 :       2280 :             port_destroy(port);
    3210                 :            :         }
    3211 [ +  + ][ -  + ]:        774 :         HMAP_FOR_EACH_SAFE (mirror, next_mirror, hmap_node, &br->mirrors) {
                 [ +  + ]
    3212                 :         22 :             mirror_destroy(mirror);
    3213                 :            :         }
    3214                 :            : 
    3215                 :        752 :         hmap_remove(&all_bridges, &br->node);
    3216                 :        752 :         ofproto_destroy(br->ofproto, del);
    3217                 :        752 :         hmap_destroy(&br->ifaces);
    3218                 :        752 :         hmap_destroy(&br->ports);
    3219                 :        752 :         hmap_destroy(&br->iface_by_name);
    3220                 :        752 :         hmap_destroy(&br->mirrors);
    3221                 :        752 :         hmap_destroy(&br->mappings);
    3222                 :        752 :         free(br->name);
    3223                 :        752 :         free(br->type);
    3224                 :        752 :         free(br);
    3225                 :            :     }
    3226                 :        752 : }
    3227                 :            : 
    3228                 :            : static struct bridge *
    3229                 :       9408 : bridge_lookup(const char *name)
    3230                 :            : {
    3231                 :            :     struct bridge *br;
    3232                 :            : 
    3233 [ +  + ][ -  + ]:       9408 :     HMAP_FOR_EACH_WITH_HASH (br, node, hash_string(name, 0), &all_bridges) {
    3234         [ +  - ]:       7904 :         if (!strcmp(br->name, name)) {
    3235                 :       7904 :             return br;
    3236                 :            :         }
    3237                 :            :     }
    3238                 :       1504 :     return NULL;
    3239                 :            : }
    3240                 :            : 
    3241                 :            : /* Handle requests for a listing of all flows known by the OpenFlow
    3242                 :            :  * stack, including those normally hidden. */
    3243                 :            : static void
    3244                 :          2 : bridge_unixctl_dump_flows(struct unixctl_conn *conn, int argc OVS_UNUSED,
    3245                 :            :                           const char *argv[], void *aux OVS_UNUSED)
    3246                 :            : {
    3247                 :            :     struct bridge *br;
    3248                 :            :     struct ds results;
    3249                 :            : 
    3250                 :          2 :     br = bridge_lookup(argv[1]);
    3251         [ -  + ]:          2 :     if (!br) {
    3252                 :          0 :         unixctl_command_reply_error(conn, "Unknown bridge");
    3253                 :          0 :         return;
    3254                 :            :     }
    3255                 :            : 
    3256                 :          2 :     ds_init(&results);
    3257                 :          2 :     ofproto_get_all_flows(br->ofproto, &results);
    3258                 :            : 
    3259                 :          2 :     unixctl_command_reply(conn, ds_cstr(&results));
    3260                 :          2 :     ds_destroy(&results);
    3261                 :            : }
    3262                 :            : 
    3263                 :            : /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
    3264                 :            :  * connections and reconnect.  If BRIDGE is not specified, then all bridges
    3265                 :            :  * drop their controller connections and reconnect. */
    3266                 :            : static void
    3267                 :          0 : bridge_unixctl_reconnect(struct unixctl_conn *conn, int argc,
    3268                 :            :                          const char *argv[], void *aux OVS_UNUSED)
    3269                 :            : {
    3270                 :            :     struct bridge *br;
    3271         [ #  # ]:          0 :     if (argc > 1) {
    3272                 :          0 :         br = bridge_lookup(argv[1]);
    3273         [ #  # ]:          0 :         if (!br) {
    3274                 :          0 :             unixctl_command_reply_error(conn,  "Unknown bridge");
    3275                 :          0 :             return;
    3276                 :            :         }
    3277                 :          0 :         ofproto_reconnect_controllers(br->ofproto);
    3278                 :            :     } else {
    3279 [ #  # ][ #  # ]:          0 :         HMAP_FOR_EACH (br, node, &all_bridges) {
    3280                 :          0 :             ofproto_reconnect_controllers(br->ofproto);
    3281                 :            :         }
    3282                 :            :     }
    3283                 :          0 :     unixctl_command_reply(conn, NULL);
    3284                 :            : }
    3285                 :            : 
    3286                 :            : static size_t
    3287                 :       9409 : bridge_get_controllers(const struct bridge *br,
    3288                 :            :                        struct ovsrec_controller ***controllersp)
    3289                 :            : {
    3290                 :            :     struct ovsrec_controller **controllers;
    3291                 :            :     size_t n_controllers;
    3292                 :            : 
    3293                 :       9409 :     controllers = br->cfg->controller;
    3294                 :       9409 :     n_controllers = br->cfg->n_controller;
    3295                 :            : 
    3296 [ +  + ][ -  + ]:       9409 :     if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
    3297                 :          0 :         controllers = NULL;
    3298                 :          0 :         n_controllers = 0;
    3299                 :            :     }
    3300                 :            : 
    3301         [ +  + ]:       9409 :     if (controllersp) {
    3302                 :       4706 :         *controllersp = controllers;
    3303                 :            :     }
    3304                 :       9409 :     return n_controllers;
    3305                 :            : }
    3306                 :            : 
    3307                 :            : static void
    3308                 :       4703 : bridge_collect_wanted_ports(struct bridge *br,
    3309                 :            :                             struct shash *wanted_ports)
    3310                 :            : {
    3311                 :            :     size_t i;
    3312                 :            : 
    3313                 :       4703 :     shash_init(wanted_ports);
    3314                 :            : 
    3315         [ +  + ]:      36519 :     for (i = 0; i < br->cfg->n_ports; i++) {
    3316                 :      31816 :         const char *name = br->cfg->ports[i]->name;
    3317         [ -  + ]:      31816 :         if (!shash_add_once(wanted_ports, name, br->cfg->ports[i])) {
    3318         [ #  # ]:          0 :             VLOG_WARN("bridge %s: %s specified twice as bridge port",
    3319                 :            :                       br->name, name);
    3320                 :            :         }
    3321                 :            :     }
    3322         [ +  + ]:       4703 :     if (bridge_get_controllers(br, NULL)
    3323         [ -  + ]:          3 :         && !shash_find(wanted_ports, br->name)) {
    3324         [ #  # ]:          0 :         VLOG_WARN("bridge %s: no port named %s, synthesizing one",
    3325                 :            :                   br->name, br->name);
    3326                 :            : 
    3327                 :          0 :         ovsrec_interface_init(&br->synth_local_iface);
    3328                 :          0 :         ovsrec_port_init(&br->synth_local_port);
    3329                 :            : 
    3330                 :          0 :         br->synth_local_port.interfaces = &br->synth_local_ifacep;
    3331                 :          0 :         br->synth_local_port.n_interfaces = 1;
    3332                 :          0 :         br->synth_local_port.name = br->name;
    3333                 :            : 
    3334                 :          0 :         br->synth_local_iface.name = br->name;
    3335                 :          0 :         br->synth_local_iface.type = "internal";
    3336                 :            : 
    3337                 :          0 :         br->synth_local_ifacep = &br->synth_local_iface;
    3338                 :            : 
    3339                 :          0 :         shash_add(wanted_ports, br->name, &br->synth_local_port);
    3340                 :            :     }
    3341                 :       4703 : }
    3342                 :            : 
    3343                 :            : /* Deletes "struct port"s and "struct iface"s under 'br' which aren't
    3344                 :            :  * consistent with 'br->cfg'.  Updates 'br->if_cfg_queue' with interfaces which
    3345                 :            :  * 'br' needs to complete its configuration. */
    3346                 :            : static void
    3347                 :       4703 : bridge_del_ports(struct bridge *br, const struct shash *wanted_ports)
    3348                 :            : {
    3349                 :            :     struct shash_node *port_node;
    3350                 :            :     struct port *port, *next;
    3351                 :            : 
    3352                 :            :     /* Get rid of deleted ports.
    3353                 :            :      * Get rid of deleted interfaces on ports that still exist. */
    3354 [ +  + ][ -  + ]:      34225 :     HMAP_FOR_EACH_SAFE (port, next, hmap_node, &br->ports) {
                 [ +  + ]
    3355                 :      29522 :         port->cfg = shash_find_data(wanted_ports, port->name);
    3356         [ +  + ]:      29522 :         if (!port->cfg) {
    3357                 :        119 :             port_destroy(port);
    3358                 :            :         } else {
    3359                 :      29403 :             port_del_ifaces(port);
    3360                 :            :         }
    3361                 :            :     }
    3362                 :            : 
    3363                 :            :     /* Update iface->cfg and iface->type in interfaces that still exist. */
    3364 [ +  + ][ -  + ]:      36519 :     SHASH_FOR_EACH (port_node, wanted_ports) {
    3365                 :      31816 :         const struct ovsrec_port *port = port_node->data;
    3366                 :            :         size_t i;
    3367                 :            : 
    3368         [ +  + ]:      63667 :         for (i = 0; i < port->n_interfaces; i++) {
    3369                 :      31851 :             const struct ovsrec_interface *cfg = port->interfaces[i];
    3370                 :      31851 :             struct iface *iface = iface_lookup(br, cfg->name);
    3371                 :      31851 :             const char *type = iface_get_type(cfg, br->cfg);
    3372                 :      31851 :             const char *dp_type = br->cfg->datapath_type;
    3373                 :      31851 :             const char *netdev_type = ofproto_port_open_type(dp_type, type);
    3374                 :            : 
    3375         [ +  + ]:      31851 :             if (iface) {
    3376                 :      29416 :                 iface->cfg = cfg;
    3377                 :      29416 :                 iface->type = type;
    3378                 :      29416 :                 iface->netdev_type = netdev_type;
    3379         [ -  + ]:       2435 :             } else if (!strcmp(type, "null")) {
    3380         [ #  # ]:          0 :                 VLOG_WARN_ONCE("%s: The null interface type is deprecated and"
    3381                 :            :                                " may be removed in February 2013. Please email"
    3382                 :            :                                " dev@openvswitch.org with concerns.",
    3383                 :            :                                cfg->name);
    3384                 :            :             } else {
    3385                 :            :                 /* We will add new interfaces later. */
    3386                 :            :             }
    3387                 :            :         }
    3388                 :            :     }
    3389                 :       4703 : }
    3390                 :            : 
    3391                 :            : /* Initializes 'oc' appropriately as a management service controller for
    3392                 :            :  * 'br'.
    3393                 :            :  *
    3394                 :            :  * The caller must free oc->target when it is no longer needed. */
    3395                 :            : static void
    3396                 :       4700 : bridge_ofproto_controller_for_mgmt(const struct bridge *br,
    3397                 :            :                                    struct ofproto_controller *oc)
    3398                 :            : {
    3399                 :       4700 :     oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
    3400                 :       4700 :     oc->max_backoff = 0;
    3401                 :       4700 :     oc->probe_interval = 60;
    3402                 :       4700 :     oc->band = OFPROTO_OUT_OF_BAND;
    3403                 :       4700 :     oc->rate_limit = 0;
    3404                 :       4700 :     oc->burst_limit = 0;
    3405                 :       4700 :     oc->enable_async_msgs = true;
    3406                 :       4700 :     oc->dscp = 0;
    3407                 :       4700 : }
    3408                 :            : 
    3409                 :            : /* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'.  */
    3410                 :            : static void
    3411                 :          2 : bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
    3412                 :            :                                       struct ofproto_controller *oc)
    3413                 :            : {
    3414                 :            :     int dscp;
    3415                 :            : 
    3416                 :          2 :     oc->target = c->target;
    3417         [ -  + ]:          2 :     oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
    3418         [ -  + ]:          2 :     oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
    3419                 :          4 :     oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
    3420 [ -  + ][ #  # ]:          2 :                 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
    3421         [ -  + ]:          2 :     oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
    3422         [ -  + ]:          2 :     oc->burst_limit = (c->controller_burst_limit
    3423                 :          0 :                        ? *c->controller_burst_limit : 0);
    3424                 :          4 :     oc->enable_async_msgs = (!c->enable_async_messages
    3425 [ -  + ][ #  # ]:          2 :                              || *c->enable_async_messages);
    3426                 :          2 :     dscp = smap_get_int(&c->other_config, "dscp", DSCP_DEFAULT);
    3427 [ +  - ][ -  + ]:          2 :     if (dscp < 0 || dscp > 63) {
    3428                 :          0 :         dscp = DSCP_DEFAULT;
    3429                 :            :     }
    3430                 :          2 :     oc->dscp = dscp;
    3431                 :          2 : }
    3432                 :            : 
    3433                 :            : /* Configures the IP stack for 'br''s local interface properly according to the
    3434                 :            :  * configuration in 'c'.  */
    3435                 :            : static void
    3436                 :          2 : bridge_configure_local_iface_netdev(struct bridge *br,
    3437                 :            :                                     struct ovsrec_controller *c)
    3438                 :            : {
    3439                 :            :     struct netdev *netdev;
    3440                 :            :     struct in_addr mask, gateway;
    3441                 :            : 
    3442                 :            :     struct iface *local_iface;
    3443                 :            :     struct in_addr ip;
    3444                 :            : 
    3445                 :            :     /* If there's no local interface or no IP address, give up. */
    3446                 :          2 :     local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
    3447 [ +  - ][ -  + ]:          2 :     if (!local_iface || !c->local_ip || !ip_parse(c->local_ip, &ip.s_addr)) {
                 [ #  # ]
    3448                 :          2 :         return;
    3449                 :            :     }
    3450                 :            : 
    3451                 :            :     /* Bring up the local interface. */
    3452                 :          0 :     netdev = local_iface->netdev;
    3453                 :          0 :     netdev_turn_flags_on(netdev, NETDEV_UP, NULL);
    3454                 :            : 
    3455                 :            :     /* Configure the IP address and netmask. */
    3456         [ #  # ]:          0 :     if (!c->local_netmask
    3457         [ #  # ]:          0 :         || !ip_parse(c->local_netmask, &mask.s_addr)
    3458         [ #  # ]:          0 :         || !mask.s_addr) {
    3459                 :          0 :         mask.s_addr = guess_netmask(ip.s_addr);
    3460                 :            :     }
    3461         [ #  # ]:          0 :     if (!netdev_set_in4(netdev, ip, mask)) {
    3462         [ #  # ]:          0 :         VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
    3463                 :            :                   br->name, IP_ARGS(ip.s_addr), IP_ARGS(mask.s_addr));
    3464                 :            :     }
    3465                 :            : 
    3466                 :            :     /* Configure the default gateway. */
    3467         [ #  # ]:          0 :     if (c->local_gateway
    3468         [ #  # ]:          0 :         && ip_parse(c->local_gateway, &gateway.s_addr)
    3469         [ #  # ]:          0 :         && gateway.s_addr) {
    3470         [ #  # ]:          0 :         if (!netdev_add_router(netdev, gateway)) {
    3471         [ #  # ]:          0 :             VLOG_INFO("bridge %s: configured gateway "IP_FMT,
    3472                 :            :                       br->name, IP_ARGS(gateway.s_addr));
    3473                 :            :         }
    3474                 :            :     }
    3475                 :            : }
    3476                 :            : 
    3477                 :            : /* Returns true if 'a' and 'b' are the same except that any number of slashes
    3478                 :            :  * in either string are treated as equal to any number of slashes in the other,
    3479                 :            :  * e.g. "x///y" is equal to "x/y".
    3480                 :            :  *
    3481                 :            :  * Also, if 'b_stoplen' bytes from 'b' are found to be equal to corresponding
    3482                 :            :  * bytes from 'a', the function considers this success.  Specify 'b_stoplen' as
    3483                 :            :  * SIZE_MAX to compare all of 'a' to all of 'b' rather than just a prefix of
    3484                 :            :  * 'b' against a prefix of 'a'.
    3485                 :            :  */
    3486                 :            : static bool
    3487                 :          2 : equal_pathnames(const char *a, const char *b, size_t b_stoplen)
    3488                 :            : {
    3489                 :          2 :     const char *b_start = b;
    3490                 :            :     for (;;) {
    3491         [ +  + ]:        188 :         if (b - b_start >= b_stoplen) {
    3492                 :          2 :             return true;
    3493         [ -  + ]:        186 :         } else if (*a != *b) {
    3494                 :          0 :             return false;
    3495         [ +  + ]:        186 :         } else if (*a == '/') {
    3496                 :         24 :             a += strspn(a, "/");
    3497                 :         24 :             b += strspn(b, "/");
    3498         [ -  + ]:        162 :         } else if (*a == '\0') {
    3499                 :          0 :             return true;
    3500                 :            :         } else {
    3501                 :        162 :             a++;
    3502                 :        162 :             b++;
    3503                 :            :         }
    3504                 :        186 :     }
    3505                 :            : }
    3506                 :            : 
    3507                 :            : static void
    3508                 :       4700 : bridge_configure_remotes(struct bridge *br,
    3509                 :            :                          const struct sockaddr_in *managers, size_t n_managers)
    3510                 :            : {
    3511                 :            :     bool disable_in_band;
    3512                 :            : 
    3513                 :            :     struct ovsrec_controller **controllers;
    3514                 :            :     size_t n_controllers;
    3515                 :            : 
    3516                 :            :     enum ofproto_fail_mode fail_mode;
    3517                 :            : 
    3518                 :            :     struct ofproto_controller *ocs;
    3519                 :            :     size_t n_ocs;
    3520                 :            :     size_t i;
    3521                 :            : 
    3522                 :            :     /* Check if we should disable in-band control on this bridge. */
    3523                 :       4700 :     disable_in_band = smap_get_bool(&br->cfg->other_config, "disable-in-band",
    3524                 :            :                                     false);
    3525                 :            : 
    3526                 :            :     /* Set OpenFlow queue ID for in-band control. */
    3527                 :       4700 :     ofproto_set_in_band_queue(br->ofproto,
    3528                 :       4700 :                               smap_get_int(&br->cfg->other_config,
    3529                 :            :                                            "in-band-queue", -1));
    3530                 :            : 
    3531         [ +  + ]:       4700 :     if (disable_in_band) {
    3532                 :        394 :         ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
    3533                 :            :     } else {
    3534                 :       4306 :         ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
    3535                 :            :     }
    3536                 :            : 
    3537                 :       4700 :     n_controllers = bridge_get_controllers(br, &controllers);
    3538                 :            : 
    3539                 :       4700 :     ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
    3540                 :       4700 :     n_ocs = 0;
    3541                 :            : 
    3542                 :       4700 :     bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
    3543         [ +  + ]:       4703 :     for (i = 0; i < n_controllers; i++) {
    3544                 :          3 :         struct ovsrec_controller *c = controllers[i];
    3545                 :            : 
    3546         [ +  - ]:          3 :         if (daemon_should_self_confine()
    3547         [ +  + ]:          3 :             && (!strncmp(c->target, "punix:", 6)
    3548         [ -  + ]:          1 :             || !strncmp(c->target, "unix:", 5))) {
    3549                 :            :             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
    3550                 :            :             char *whitelist;
    3551                 :            : 
    3552         [ -  + ]:          2 :             if (!strncmp(c->target, "unix:", 5)) {
    3553                 :            :                 /* Connect to a listening socket */
    3554                 :          0 :                 whitelist = xasprintf("unix:%s/", ovs_rundir());
    3555   [ #  #  #  # ]:          0 :                 if (strchr(c->target, '/') &&
    3556                 :          0 :                    !equal_pathnames(c->target, whitelist,
    3557                 :            :                      strlen(whitelist))) {
    3558                 :            :                     /* Absolute path specified, but not in ovs_rundir */
    3559         [ #  # ]:          0 :                     VLOG_ERR_RL(&rl, "bridge %s: Not connecting to socket "
    3560                 :            :                                   "controller \"%s\" due to possibility for "
    3561                 :            :                                   "remote exploit.  Instead, specify socket "
    3562                 :            :                                   "in whitelisted \"%s\" or connect to "
    3563                 :            :                                   "\"unix:%s/%s.mgmt\" (which is always "
    3564                 :            :                                   "available without special configuration).",
    3565                 :            :                                   br->name, c->target, whitelist,
    3566                 :            :                                   ovs_rundir(), br->name);
    3567                 :          0 :                     free(whitelist);
    3568                 :          0 :                     continue;
    3569                 :            :                 }
    3570                 :            :             } else {
    3571                 :          2 :                whitelist = xasprintf("punix:%s/%s.",
    3572                 :            :                                      ovs_rundir(), br->name);
    3573         [ +  - ]:          2 :                if (!equal_pathnames(c->target, whitelist, strlen(whitelist))
    3574         [ +  + ]:          2 :                    || strchr(c->target + strlen(whitelist), '/')) {
    3575                 :            :                    /* Prevent remote ovsdb-server users from accessing
    3576                 :            :                     * arbitrary Unix domain sockets and overwriting arbitrary
    3577                 :            :                     * local files. */
    3578         [ +  - ]:          1 :                    VLOG_ERR_RL(&rl, "bridge %s: Not adding Unix domain socket "
    3579                 :            :                                   "controller \"%s\" due to possibility of "
    3580                 :            :                                   "overwriting local files. Instead, specify "
    3581                 :            :                                   "path in whitelisted format \"%s*\" or "
    3582                 :            :                                   "connect to \"unix:%s/%s.mgmt\" (which is "
    3583                 :            :                                   "always available without special "
    3584                 :            :                                   "configuration).",
    3585                 :            :                                   br->name, c->target, whitelist,
    3586                 :            :                                   ovs_rundir(), br->name);
    3587                 :          1 :                    free(whitelist);
    3588                 :          1 :                    continue;
    3589                 :            :                }
    3590                 :            :             }
    3591                 :            : 
    3592                 :          1 :             free(whitelist);
    3593                 :            :         }
    3594                 :            : 
    3595                 :          2 :         bridge_configure_local_iface_netdev(br, c);
    3596                 :          2 :         bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
    3597         [ -  + ]:          2 :         if (disable_in_band) {
    3598                 :          0 :             ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
    3599                 :            :         }
    3600                 :          2 :         n_ocs++;
    3601                 :            :     }
    3602                 :            : 
    3603                 :       4700 :     ofproto_set_controllers(br->ofproto, ocs, n_ocs,
    3604                 :            :                             bridge_get_allowed_versions(br));
    3605                 :       4700 :     free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
    3606                 :       4700 :     free(ocs);
    3607                 :            : 
    3608                 :            :     /* Set the fail-mode. */
    3609                 :       9400 :     fail_mode = !br->cfg->fail_mode
    3610                 :       3782 :                 || !strcmp(br->cfg->fail_mode, "standalone")
    3611                 :            :                     ? OFPROTO_FAIL_STANDALONE
    3612 [ +  + ][ +  + ]:       4700 :                     : OFPROTO_FAIL_SECURE;
    3613                 :       4700 :     ofproto_set_fail_mode(br->ofproto, fail_mode);
    3614                 :            : 
    3615                 :            :     /* Configure OpenFlow controller connection snooping. */
    3616         [ +  + ]:       4700 :     if (!ofproto_has_snoops(br->ofproto)) {
    3617                 :            :         struct sset snoops;
    3618                 :            : 
    3619                 :        749 :         sset_init(&snoops);
    3620                 :        749 :         sset_add_and_free(&snoops, xasprintf("punix:%s/%s.snoop",
    3621                 :            :                                              ovs_rundir(), br->name));
    3622                 :        749 :         ofproto_set_snoops(br->ofproto, &snoops);
    3623                 :        749 :         sset_destroy(&snoops);
    3624                 :            :     }
    3625                 :       4700 : }
    3626                 :            : 
    3627                 :            : static void
    3628                 :       4700 : bridge_configure_tables(struct bridge *br)
    3629                 :            : {
    3630                 :            :     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
    3631                 :            :     int n_tables;
    3632                 :            :     int i, j, k;
    3633                 :            : 
    3634                 :       4700 :     n_tables = ofproto_get_n_tables(br->ofproto);
    3635                 :       4700 :     j = 0;
    3636         [ +  + ]:    1203200 :     for (i = 0; i < n_tables; i++) {
    3637                 :            :         struct ofproto_table_settings s;
    3638                 :    1198500 :         bool use_default_prefixes = true;
    3639                 :            : 
    3640                 :    1198500 :         s.name = NULL;
    3641                 :    1198500 :         s.max_flows = UINT_MAX;
    3642                 :    1198500 :         s.groups = NULL;
    3643                 :    1198500 :         s.enable_eviction = false;
    3644                 :    1198500 :         s.n_groups = 0;
    3645                 :    1198500 :         s.n_prefix_fields = 0;
    3646                 :    1198500 :         memset(s.prefix_fields, ~0, sizeof(s.prefix_fields));
    3647                 :            : 
    3648 [ +  + ][ +  + ]:    1198500 :         if (j < br->cfg->n_flow_tables && i == br->cfg->key_flow_tables[j]) {
    3649                 :         31 :             struct ovsrec_flow_table *cfg = br->cfg->value_flow_tables[j++];
    3650                 :            : 
    3651                 :         31 :             s.name = cfg->name;
    3652 [ +  + ][ +  - ]:         31 :             if (cfg->n_flow_limit && *cfg->flow_limit < UINT_MAX) {
    3653                 :         21 :                 s.max_flows = *cfg->flow_limit;
    3654                 :            :             }
    3655                 :            : 
    3656                 :         62 :             s.enable_eviction = (cfg->overflow_policy
    3657 [ +  + ][ +  - ]:         31 :                                  && !strcmp(cfg->overflow_policy, "evict"));
    3658         [ +  + ]:         31 :             if (cfg->n_groups) {
    3659                 :          2 :                 s.groups = xmalloc(cfg->n_groups * sizeof *s.groups);
    3660         [ +  + ]:          4 :                 for (k = 0; k < cfg->n_groups; k++) {
    3661                 :          2 :                     const char *string = cfg->groups[k];
    3662                 :            :                     char *msg;
    3663                 :            : 
    3664                 :          2 :                     msg = mf_parse_subfield__(&s.groups[k], &string);
    3665         [ -  + ]:          2 :                     if (msg) {
    3666         [ #  # ]:          0 :                         VLOG_WARN_RL(&rl, "bridge %s table %d: error parsing "
    3667                 :            :                                      "'groups' (%s)", br->name, i, msg);
    3668                 :          0 :                         free(msg);
    3669         [ -  + ]:          2 :                     } else if (*string) {
    3670         [ #  # ]:          0 :                         VLOG_WARN_RL(&rl, "bridge %s table %d: 'groups' "
    3671                 :            :                                      "element '%s' contains trailing garbage",
    3672                 :            :                                      br->name, i, cfg->groups[k]);
    3673                 :            :                     } else {
    3674                 :          2 :                         s.n_groups++;
    3675                 :            :                     }
    3676                 :            :                 }
    3677                 :            :             }
    3678                 :            : 
    3679                 :            :             /* Prefix lookup fields. */
    3680                 :         31 :             s.n_prefix_fields = 0;
    3681         [ +  + ]:         39 :             for (k = 0; k < cfg->n_prefixes; k++) {
    3682                 :          9 :                 const char *name = cfg->prefixes[k];
    3683                 :            :                 const struct mf_field *mf;
    3684                 :            : 
    3685         [ +  + ]:          9 :                 if (strcmp(name, "none") == 0) {
    3686                 :          1 :                     use_default_prefixes = false;
    3687                 :          1 :                     s.n_prefix_fields = 0;
    3688                 :          1 :                     break;
    3689                 :            :                 }
    3690                 :          8 :                 mf = mf_from_name(name);
    3691         [ -  + ]:          8 :                 if (!mf) {
    3692         [ #  # ]:          0 :                     VLOG_WARN("bridge %s: 'prefixes' with unknown field: %s",
    3693                 :            :                               br->name, name);
    3694                 :          0 :                     continue;
    3695                 :            :                 }
    3696 [ +  + ][ -  + ]:          8 :                 if (mf->flow_be32ofs < 0 || mf->n_bits % 32) {
    3697         [ +  - ]:          1 :                     VLOG_WARN("bridge %s: 'prefixes' with incompatible field: "
    3698                 :            :                               "%s", br->name, name);
    3699                 :          1 :                     continue;
    3700                 :            :                 }
    3701         [ -  + ]:          7 :                 if (s.n_prefix_fields >= ARRAY_SIZE(s.prefix_fields)) {
    3702         [ #  # ]:          0 :                     VLOG_WARN("bridge %s: 'prefixes' with too many fields, "
    3703                 :            :                               "field not used: %s", br->name, name);
    3704                 :          0 :                     continue;
    3705                 :            :                 }
    3706                 :          7 :                 use_default_prefixes = false;
    3707                 :          7 :                 s.prefix_fields[s.n_prefix_fields++] = mf->id;
    3708                 :            :             }
    3709                 :            :         }
    3710         [ +  + ]:    1198500 :         if (use_default_prefixes) {
    3711                 :            :             /* Use default values. */
    3712                 :    1198495 :             s.n_prefix_fields = ARRAY_SIZE(default_prefix_fields);
    3713                 :    1198495 :             memcpy(s.prefix_fields, default_prefix_fields,
    3714                 :            :                    sizeof default_prefix_fields);
    3715                 :            :         } else {
    3716                 :            :             int k;
    3717                 :          5 :             struct ds ds = DS_EMPTY_INITIALIZER;
    3718         [ +  + ]:         12 :             for (k = 0; k < s.n_prefix_fields; k++) {
    3719         [ +  + ]:          7 :                 if (k) {
    3720                 :          3 :                     ds_put_char(&ds, ',');
    3721                 :            :                 }
    3722                 :          7 :                 ds_put_cstr(&ds, mf_from_id(s.prefix_fields[k])->name);
    3723                 :            :             }
    3724         [ +  + ]:          5 :             if (s.n_prefix_fields == 0) {
    3725                 :          1 :                 ds_put_cstr(&ds, "none");
    3726                 :            :             }
    3727         [ +  - ]:          5 :             VLOG_INFO("bridge %s table %d: Prefix lookup with: %s.",
    3728                 :            :                       br->name, i, ds_cstr(&ds));
    3729                 :          5 :             ds_destroy(&ds);
    3730                 :            :         }
    3731                 :            : 
    3732                 :    1198500 :         ofproto_configure_table(br->ofproto, i, &s);
    3733                 :            : 
    3734                 :    1198500 :         free(s.groups);
    3735                 :            :     }
    3736         [ -  + ]:       4700 :     for (; j < br->cfg->n_flow_tables; j++) {
    3737         [ #  # ]:          0 :         VLOG_WARN_RL(&rl, "bridge %s: ignoring configuration for flow table "
    3738                 :            :                      "%"PRId64" not supported by this datapath", br->name,
    3739                 :            :                      br->cfg->key_flow_tables[j]);
    3740                 :            :     }
    3741                 :       4700 : }
    3742                 :            : 
    3743                 :            : static void
    3744                 :       4700 : bridge_configure_dp_desc(struct bridge *br)
    3745                 :            : {
    3746                 :       4700 :     ofproto_set_dp_desc(br->ofproto,
    3747                 :       4700 :                         smap_get(&br->cfg->other_config, "dp-desc"));
    3748                 :       4700 : }
    3749                 :            : 
    3750                 :            : static struct aa_mapping *
    3751                 :          0 : bridge_aa_mapping_find(struct bridge *br, const int64_t isid)
    3752                 :            : {
    3753                 :            :     struct aa_mapping *m;
    3754                 :            : 
    3755 [ #  # ][ #  # ]:          0 :     HMAP_FOR_EACH_IN_BUCKET (m,
    3756                 :            :                              hmap_node,
    3757                 :            :                              hash_bytes(&isid, sizeof isid, 0),
    3758                 :            :                              &br->mappings) {
    3759         [ #  # ]:          0 :         if (isid == m->isid) {
    3760                 :          0 :             return m;
    3761                 :            :         }
    3762                 :            :     }
    3763                 :          0 :     return NULL;
    3764                 :            : }
    3765                 :            : 
    3766                 :            : static struct aa_mapping *
    3767                 :          0 : bridge_aa_mapping_create(struct bridge *br,
    3768                 :            :                          const int64_t isid,
    3769                 :            :                          const int64_t vlan)
    3770                 :            : {
    3771                 :            :     struct aa_mapping *m;
    3772                 :            : 
    3773                 :          0 :     m = xzalloc(sizeof *m);
    3774                 :          0 :     m->bridge = br;
    3775                 :          0 :     m->isid = isid;
    3776                 :          0 :     m->vlan = vlan;
    3777                 :          0 :     m->br_name = xstrdup(br->name);
    3778                 :          0 :     hmap_insert(&br->mappings,
    3779                 :            :                 &m->hmap_node,
    3780                 :            :                 hash_bytes(&isid, sizeof isid, 0));
    3781                 :            : 
    3782                 :          0 :     return m;
    3783                 :            : }
    3784                 :            : 
    3785                 :            : static void
    3786                 :          0 : bridge_aa_mapping_destroy(struct aa_mapping *m)
    3787                 :            : {
    3788         [ #  # ]:          0 :     if (m) {
    3789                 :          0 :         struct bridge *br = m->bridge;
    3790                 :            : 
    3791         [ #  # ]:          0 :         if (br->ofproto) {
    3792                 :          0 :             ofproto_aa_mapping_unregister(br->ofproto, m);
    3793                 :            :         }
    3794                 :            : 
    3795                 :          0 :         hmap_remove(&br->mappings, &m->hmap_node);
    3796         [ #  # ]:          0 :         if (m->br_name) {
    3797                 :          0 :             free(m->br_name);
    3798                 :            :         }
    3799                 :          0 :         free(m);
    3800                 :            :     }
    3801                 :          0 : }
    3802                 :            : 
    3803                 :            : static bool
    3804                 :          0 : bridge_aa_mapping_configure(struct aa_mapping *m)
    3805                 :            : {
    3806                 :            :     struct aa_mapping_settings s;
    3807                 :            : 
    3808                 :          0 :     s.isid = m->isid;
    3809                 :          0 :     s.vlan = m->vlan;
    3810                 :            : 
    3811                 :            :     /* Configure. */
    3812                 :          0 :     ofproto_aa_mapping_register(m->bridge->ofproto, m, &s);
    3813                 :            : 
    3814                 :          0 :     return true;
    3815                 :            : }
    3816                 :            : 
    3817                 :            : static void
    3818                 :       4700 : bridge_configure_aa(struct bridge *br)
    3819                 :            : {
    3820                 :            :     const struct ovsdb_datum *mc;
    3821                 :       4700 :     struct ovsrec_autoattach *auto_attach = br->cfg->auto_attach;
    3822                 :            :     struct aa_settings aa_s;
    3823                 :            :     struct aa_mapping *m, *next;
    3824                 :            :     size_t i;
    3825                 :            : 
    3826         [ +  - ]:       4700 :     if (!auto_attach) {
    3827                 :       4700 :         ofproto_set_aa(br->ofproto, NULL, NULL);
    3828                 :       4700 :         return;
    3829                 :            :     }
    3830                 :            : 
    3831                 :          0 :     memset(&aa_s, 0, sizeof aa_s);
    3832                 :          0 :     aa_s.system_description = auto_attach->system_description;
    3833                 :          0 :     aa_s.system_name = auto_attach->system_name;
    3834                 :          0 :     ofproto_set_aa(br->ofproto, NULL, &aa_s);
    3835                 :            : 
    3836                 :          0 :     mc = ovsrec_autoattach_get_mappings(auto_attach,
    3837                 :            :                                         OVSDB_TYPE_INTEGER,
    3838                 :            :                                         OVSDB_TYPE_INTEGER);
    3839 [ #  # ][ #  # ]:          0 :     HMAP_FOR_EACH_SAFE (m, next, hmap_node, &br->mappings) {
                 [ #  # ]
    3840                 :            :         union ovsdb_atom atom;
    3841                 :            : 
    3842                 :          0 :         atom.integer = m->isid;
    3843         [ #  # ]:          0 :         if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
    3844         [ #  # ]:          0 :             VLOG_INFO("Deleting isid=%"PRIu32", vlan=%"PRIu16,
    3845                 :            :                       m->isid, m->vlan);
    3846                 :          0 :             bridge_aa_mapping_destroy(m);
    3847                 :            :         }
    3848                 :            :     }
    3849                 :            : 
    3850                 :            :     /* Add new mappings and reconfigure existing ones. */
    3851         [ #  # ]:          0 :     for (i = 0; i < auto_attach->n_mappings; ++i) {
    3852                 :          0 :         struct aa_mapping *m =
    3853                 :          0 :             bridge_aa_mapping_find(br, auto_attach->key_mappings[i]);
    3854                 :            : 
    3855         [ #  # ]:          0 :         if (!m) {
    3856         [ #  # ]:          0 :             VLOG_INFO("Adding isid=%"PRId64", vlan=%"PRId64,
    3857                 :            :                       auto_attach->key_mappings[i],
    3858                 :            :                       auto_attach->value_mappings[i]);
    3859                 :          0 :             m = bridge_aa_mapping_create(br,
    3860                 :          0 :                                          auto_attach->key_mappings[i],
    3861                 :          0 :                                          auto_attach->value_mappings[i]);
    3862                 :            : 
    3863         [ #  # ]:          0 :             if (!bridge_aa_mapping_configure(m)) {
    3864                 :          0 :                 bridge_aa_mapping_destroy(m);
    3865                 :            :             }
    3866                 :            :         }
    3867                 :            :     }
    3868                 :            : }
    3869                 :            : 
    3870                 :            : static bool
    3871                 :       5132 : bridge_aa_need_refresh(struct bridge *br)
    3872                 :            : {
    3873                 :       5132 :     return ofproto_aa_vlan_get_queue_size(br->ofproto) > 0;
    3874                 :            : }
    3875                 :            : 
    3876                 :            : static void
    3877                 :          0 : bridge_aa_update_trunks(struct port *port, struct bridge_aa_vlan *m)
    3878                 :            : {
    3879                 :          0 :     int64_t *trunks = NULL;
    3880                 :          0 :     unsigned int i = 0;
    3881                 :          0 :     bool found = false, reconfigure = false;
    3882                 :            : 
    3883         [ #  # ]:          0 :     for (i = 0; i < port->cfg->n_trunks; i++) {
    3884         [ #  # ]:          0 :         if (port->cfg->trunks[i] == m->vlan) {
    3885                 :          0 :             found = true;
    3886                 :          0 :             break;
    3887                 :            :         }
    3888                 :            :     }
    3889                 :            : 
    3890      [ #  #  # ]:          0 :     switch (m->oper) {
    3891                 :            :         case BRIDGE_AA_VLAN_OPER_ADD:
    3892         [ #  # ]:          0 :             if (!found) {
    3893                 :          0 :                 trunks = xmalloc(sizeof *trunks * (port->cfg->n_trunks + 1));
    3894                 :            : 
    3895         [ #  # ]:          0 :                 for (i = 0; i < port->cfg->n_trunks; i++) {
    3896                 :          0 :                     trunks[i] = port->cfg->trunks[i];
    3897                 :            :                 }
    3898                 :          0 :                 trunks[i++] = m->vlan;
    3899                 :          0 :                 reconfigure = true;
    3900                 :            :             }
    3901                 :            : 
    3902                 :          0 :             break;
    3903                 :            : 
    3904                 :            :         case BRIDGE_AA_VLAN_OPER_REMOVE:
    3905         [ #  # ]:          0 :             if (found) {
    3906                 :          0 :                 unsigned int j = 0;
    3907                 :            : 
    3908                 :          0 :                 trunks = xmalloc(sizeof *trunks * (port->cfg->n_trunks - 1));
    3909                 :            : 
    3910         [ #  # ]:          0 :                 for (i = 0; i < port->cfg->n_trunks; i++) {
    3911         [ #  # ]:          0 :                     if (port->cfg->trunks[i] != m->vlan) {
    3912                 :          0 :                         trunks[j++] = port->cfg->trunks[i];
    3913                 :            :                     }
    3914                 :            :                 }
    3915                 :          0 :                 i = j;
    3916                 :          0 :                 reconfigure = true;
    3917                 :            :             }
    3918                 :            : 
    3919                 :          0 :             break;
    3920                 :            : 
    3921                 :            :         case BRIDGE_AA_VLAN_OPER_UNDEF:
    3922                 :            :         default:
    3923         [ #  # ]:          0 :             VLOG_WARN("unrecognized operation %u", m->oper);
    3924                 :          0 :             break;
    3925                 :            :     }
    3926                 :            : 
    3927         [ #  # ]:          0 :     if (reconfigure) {
    3928                 :            :         /* VLAN switching under trunk mode cause the trunk port to switch all
    3929                 :            :          * VLANs, see ovs-vswitchd.conf.db
    3930                 :            :          */
    3931         [ #  # ]:          0 :         if (i == 0)  {
    3932                 :            :             static char *vlan_mode_access = "access";
    3933                 :          0 :             ovsrec_port_set_vlan_mode(port->cfg, vlan_mode_access);
    3934                 :            :         }
    3935                 :            : 
    3936         [ #  # ]:          0 :         if (i == 1) {
    3937                 :            :             static char *vlan_mode_trunk = "trunk";
    3938                 :          0 :             ovsrec_port_set_vlan_mode(port->cfg, vlan_mode_trunk);
    3939                 :            :         }
    3940                 :            : 
    3941                 :          0 :         ovsrec_port_set_trunks(port->cfg, trunks, i);
    3942                 :            : 
    3943                 :            :         /* Force reconfigure of the port. */
    3944                 :          0 :         port_configure(port);
    3945                 :            :     }
    3946                 :          0 : }
    3947                 :            : 
    3948                 :            : static void
    3949                 :          0 : bridge_aa_refresh_queued(struct bridge *br)
    3950                 :            : {
    3951                 :          0 :     struct ovs_list *list = xmalloc(sizeof *list);
    3952                 :            :     struct bridge_aa_vlan *node, *next;
    3953                 :            : 
    3954                 :          0 :     ovs_list_init(list);
    3955                 :          0 :     ofproto_aa_vlan_get_queued(br->ofproto, list);
    3956                 :            : 
    3957 [ #  # ][ #  # ]:          0 :     LIST_FOR_EACH_SAFE (node, next, list_node, list) {
    3958                 :            :         struct port *port;
    3959                 :            : 
    3960         [ #  # ]:          0 :         VLOG_INFO("ifname=%s, vlan=%u, oper=%u", node->port_name, node->vlan,
    3961                 :            :                   node->oper);
    3962                 :            : 
    3963                 :          0 :         port = port_lookup(br, node->port_name);
    3964         [ #  # ]:          0 :         if (port) {
    3965                 :          0 :             bridge_aa_update_trunks(port, node);
    3966                 :            :         }
    3967                 :            : 
    3968                 :          0 :         ovs_list_remove(&node->list_node);
    3969                 :          0 :         free(node->port_name);
    3970                 :          0 :         free(node);
    3971                 :            :     }
    3972                 :            : 
    3973                 :          0 :     free(list);
    3974                 :          0 : }
    3975                 :            : 
    3976                 :            : 
    3977                 :            : /* Port functions. */
    3978                 :            : 
    3979                 :            : static struct port *
    3980                 :       2667 : port_create(struct bridge *br, const struct ovsrec_port *cfg)
    3981                 :            : {
    3982                 :            :     struct port *port;
    3983                 :            : 
    3984                 :       2667 :     port = xzalloc(sizeof *port);
    3985                 :       2667 :     port->bridge = br;
    3986                 :       2667 :     port->name = xstrdup(cfg->name);
    3987                 :       2667 :     port->cfg = cfg;
    3988                 :       2667 :     ovs_list_init(&port->ifaces);
    3989                 :            : 
    3990                 :       2667 :     hmap_insert(&br->ports, &port->hmap_node, hash_string(port->name, 0));
    3991                 :       2667 :     return port;
    3992                 :            : }
    3993                 :            : 
    3994                 :            : /* Deletes interfaces from 'port' that are no longer configured for it. */
    3995                 :            : static void
    3996                 :      29403 : port_del_ifaces(struct port *port)
    3997                 :            : {
    3998                 :            :     struct iface *iface, *next;
    3999                 :            :     struct sset new_ifaces;
    4000                 :            :     size_t i;
    4001                 :            : 
    4002                 :            :     /* Collect list of new interfaces. */
    4003                 :      29403 :     sset_init(&new_ifaces);
    4004         [ +  + ]:      58819 :     for (i = 0; i < port->cfg->n_interfaces; i++) {
    4005                 :      29416 :         const char *name = port->cfg->interfaces[i]->name;
    4006                 :      29416 :         const char *type = port->cfg->interfaces[i]->type;
    4007         [ +  - ]:      29416 :         if (strcmp(type, "null")) {
    4008                 :      29416 :             sset_add(&new_ifaces, name);
    4009                 :            :         }
    4010                 :            :     }
    4011                 :            : 
    4012                 :            :     /* Get rid of deleted interfaces. */
    4013 [ +  + ][ +  + ]:      58819 :     LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
    4014         [ -  + ]:      29416 :         if (!sset_contains(&new_ifaces, iface->name)) {
    4015                 :          0 :             iface_destroy(iface);
    4016                 :            :         }
    4017                 :            :     }
    4018                 :            : 
    4019                 :      29403 :     sset_destroy(&new_ifaces);
    4020                 :      29403 : }
    4021                 :            : 
    4022                 :            : static void
    4023                 :       2667 : port_destroy(struct port *port)
    4024                 :            : {
    4025         [ +  - ]:       2667 :     if (port) {
    4026                 :       2667 :         struct bridge *br = port->bridge;
    4027                 :            :         struct iface *iface, *next;
    4028                 :            : 
    4029         [ +  - ]:       2667 :         if (br->ofproto) {
    4030                 :       2667 :             ofproto_bundle_unregister(br->ofproto, port);
    4031                 :            :         }
    4032                 :            : 
    4033 [ +  + ][ +  + ]:       5088 :         LIST_FOR_EACH_SAFE (iface, next, port_elem, &port->ifaces) {
    4034                 :       2421 :             iface_destroy__(iface);
    4035                 :            :         }
    4036                 :            : 
    4037                 :       2667 :         hmap_remove(&br->ports, &port->hmap_node);
    4038                 :       2667 :         free(port->name);
    4039                 :       2667 :         free(port);
    4040                 :            :     }
    4041                 :       2667 : }
    4042                 :            : 
    4043                 :            : static struct port *
    4044                 :       2833 : port_lookup(const struct bridge *br, const char *name)
    4045                 :            : {
    4046                 :            :     struct port *port;
    4047                 :            : 
    4048 [ +  + ][ -  + ]:       2833 :     HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_string(name, 0),
    4049                 :            :                              &br->ports) {
    4050         [ +  - ]:         45 :         if (!strcmp(port->name, name)) {
    4051                 :         45 :             return port;
    4052                 :            :         }
    4053                 :            :     }
    4054                 :       2788 :     return NULL;
    4055                 :            : }
    4056                 :            : 
    4057                 :            : static bool
    4058                 :      31802 : enable_lacp(struct port *port, bool *activep)
    4059                 :            : {
    4060         [ +  + ]:      31802 :     if (!port->cfg->lacp) {
    4061                 :            :         /* XXX when LACP implementation has been sufficiently tested, enable by
    4062                 :            :          * default and make active on bonded ports. */
    4063                 :      31782 :         return false;
    4064         [ -  + ]:         20 :     } else if (!strcmp(port->cfg->lacp, "off")) {
    4065                 :          0 :         return false;
    4066         [ +  - ]:         20 :     } else if (!strcmp(port->cfg->lacp, "active")) {
    4067                 :         20 :         *activep = true;
    4068                 :         20 :         return true;
    4069         [ #  # ]:          0 :     } else if (!strcmp(port->cfg->lacp, "passive")) {
    4070                 :          0 :         *activep = false;
    4071                 :          0 :         return true;
    4072                 :            :     } else {
    4073         [ #  # ]:          0 :         VLOG_WARN("port %s: unknown LACP mode %s",
    4074                 :            :                   port->name, port->cfg->lacp);
    4075                 :          0 :         return false;
    4076                 :            :     }
    4077                 :            : }
    4078                 :            : 
    4079                 :            : static struct lacp_settings *
    4080                 :      31802 : port_configure_lacp(struct port *port, struct lacp_settings *s)
    4081                 :            : {
    4082                 :            :     const char *lacp_time, *system_id;
    4083                 :            :     int priority;
    4084                 :            : 
    4085         [ +  + ]:      31802 :     if (!enable_lacp(port, &s->active)) {
    4086                 :      31782 :         return NULL;
    4087                 :            :     }
    4088                 :            : 
    4089                 :         20 :     s->name = port->name;
    4090                 :            : 
    4091                 :         20 :     system_id = smap_get(&port->cfg->other_config, "lacp-system-id");
    4092         [ +  + ]:         20 :     if (system_id) {
    4093         [ -  + ]:          3 :         if (!ovs_scan(system_id, ETH_ADDR_SCAN_FMT,
    4094                 :            :                       ETH_ADDR_SCAN_ARGS(s->id))) {
    4095         [ #  # ]:          0 :             VLOG_WARN("port %s: LACP system ID (%s) must be an Ethernet"
    4096                 :            :                       " address.", port->name, system_id);
    4097                 :          0 :             return NULL;
    4098                 :            :         }
    4099                 :            :     } else {
    4100                 :         17 :         s->id = port->bridge->ea;
    4101                 :            :     }
    4102                 :            : 
    4103         [ -  + ]:         20 :     if (eth_addr_is_zero(s->id)) {
    4104         [ #  # ]:          0 :         VLOG_WARN("port %s: Invalid zero LACP system ID.", port->name);
    4105                 :          0 :         return NULL;
    4106                 :            :     }
    4107                 :            : 
    4108                 :            :     /* Prefer bondable links if unspecified. */
    4109                 :         20 :     priority = smap_get_int(&port->cfg->other_config, "lacp-system-priority",
    4110                 :            :                             0);
    4111 [ +  + ][ -  + ]:         37 :     s->priority = (priority > 0 && priority <= UINT16_MAX
    4112                 :            :                    ? priority
    4113                 :         17 :                    : UINT16_MAX - !ovs_list_is_short(&port->ifaces));
    4114                 :            : 
    4115                 :         20 :     lacp_time = smap_get_def(&port->cfg->other_config, "lacp-time", "");
    4116                 :         20 :     s->fast = !strcasecmp(lacp_time, "fast");
    4117                 :            : 
    4118                 :         20 :     s->fallback_ab_cfg = smap_get_bool(&port->cfg->other_config,
    4119                 :            :                                        "lacp-fallback-ab", false);
    4120                 :            : 
    4121                 :         20 :     return s;
    4122                 :            : }
    4123                 :            : 
    4124                 :            : static void
    4125                 :         41 : iface_configure_lacp(struct iface *iface, struct lacp_slave_settings *s)
    4126                 :            : {
    4127                 :            :     int priority, portid, key;
    4128                 :            : 
    4129                 :         41 :     portid = smap_get_int(&iface->cfg->other_config, "lacp-port-id", 0);
    4130                 :         41 :     priority = smap_get_int(&iface->cfg->other_config, "lacp-port-priority",
    4131                 :            :                             0);
    4132                 :         41 :     key = smap_get_int(&iface->cfg->other_config, "lacp-aggregation-key", 0);
    4133                 :            : 
    4134 [ +  + ][ -  + ]:         41 :     if (portid <= 0 || portid > UINT16_MAX) {
    4135                 :         35 :         portid = ofp_to_u16(iface->ofp_port);
    4136                 :            :     }
    4137                 :            : 
    4138 [ +  + ][ -  + ]:         41 :     if (priority <= 0 || priority > UINT16_MAX) {
    4139                 :         35 :         priority = UINT16_MAX;
    4140                 :            :     }
    4141                 :            : 
    4142 [ +  - ][ -  + ]:         41 :     if (key < 0 || key > UINT16_MAX) {
    4143                 :          0 :         key = 0;
    4144                 :            :     }
    4145                 :            : 
    4146                 :         41 :     s->name = iface->name;
    4147                 :         41 :     s->id = portid;
    4148                 :         41 :     s->priority = priority;
    4149                 :         41 :     s->key = key;
    4150                 :         41 : }
    4151                 :            : 
    4152                 :            : static void
    4153                 :         32 : port_configure_bond(struct port *port, struct bond_settings *s)
    4154                 :            : {
    4155                 :            :     const char *detect_s;
    4156                 :            :     struct iface *iface;
    4157                 :            :     const char *mac_s;
    4158                 :            :     int miimon_interval;
    4159                 :            : 
    4160                 :         32 :     s->name = port->name;
    4161                 :         32 :     s->balance = BM_AB;
    4162         [ +  - ]:         32 :     if (port->cfg->bond_mode) {
    4163         [ -  + ]:         32 :         if (!bond_mode_from_string(&s->balance, port->cfg->bond_mode)) {
    4164         [ #  # ]:         32 :             VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
    4165                 :            :                       port->name, port->cfg->bond_mode,
    4166                 :            :                       bond_mode_to_string(s->balance));
    4167                 :            :         }
    4168                 :            :     } else {
    4169                 :            :         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
    4170                 :            : 
    4171                 :            :         /* XXX: Post version 1.5.*, the default bond_mode changed from SLB to
    4172                 :            :          * active-backup. At some point we should remove this warning. */
    4173         [ #  # ]:          0 :         VLOG_WARN_RL(&rl, "port %s: Using the default bond_mode %s. Note that"
    4174                 :            :                      " in previous versions, the default bond_mode was"
    4175                 :            :                      " balance-slb", port->name,
    4176                 :            :                      bond_mode_to_string(s->balance));
    4177                 :            :     }
    4178 [ +  + ][ -  + ]:         32 :     if (s->balance == BM_SLB && port->bridge->cfg->n_flood_vlans) {
    4179         [ #  # ]:          0 :         VLOG_WARN("port %s: SLB bonds are incompatible with flood_vlans, "
    4180                 :            :                   "please use another bond type or disable flood_vlans",
    4181                 :            :                   port->name);
    4182                 :            :     }
    4183                 :            : 
    4184                 :         32 :     miimon_interval = smap_get_int(&port->cfg->other_config,
    4185                 :            :                                    "bond-miimon-interval", 0);
    4186         [ +  - ]:         32 :     if (miimon_interval <= 0) {
    4187                 :         32 :         miimon_interval = 200;
    4188                 :            :     }
    4189                 :            : 
    4190                 :         32 :     detect_s = smap_get(&port->cfg->other_config, "bond-detect-mode");
    4191 [ -  + ][ #  # ]:         32 :     if (!detect_s || !strcmp(detect_s, "carrier")) {
    4192                 :         32 :         miimon_interval = 0;
    4193         [ #  # ]:          0 :     } else if (strcmp(detect_s, "miimon")) {
    4194         [ #  # ]:          0 :         VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
    4195                 :            :                   "defaulting to carrier", port->name, detect_s);
    4196                 :          0 :         miimon_interval = 0;
    4197                 :            :     }
    4198                 :            : 
    4199                 :         32 :     s->up_delay = MAX(0, port->cfg->bond_updelay);
    4200                 :         32 :     s->down_delay = MAX(0, port->cfg->bond_downdelay);
    4201                 :         32 :     s->basis = smap_get_int(&port->cfg->other_config, "bond-hash-basis", 0);
    4202                 :         32 :     s->rebalance_interval = smap_get_int(&port->cfg->other_config,
    4203                 :            :                                            "bond-rebalance-interval", 10000);
    4204 [ +  + ][ -  + ]:         32 :     if (s->rebalance_interval && s->rebalance_interval < 1000) {
    4205                 :          0 :         s->rebalance_interval = 1000;
    4206                 :            :     }
    4207                 :            : 
    4208                 :         32 :     s->lacp_fallback_ab_cfg = smap_get_bool(&port->cfg->other_config,
    4209                 :            :                                        "lacp-fallback-ab", false);
    4210                 :            : 
    4211         [ +  + ]:         99 :     LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
    4212                 :         67 :         netdev_set_miimon_interval(iface->netdev, miimon_interval);
    4213                 :            :     }
    4214                 :            : 
    4215                 :         32 :     mac_s = port->cfg->bond_active_slave;
    4216 [ +  + ][ -  + ]:         32 :     if (!mac_s || !ovs_scan(mac_s, ETH_ADDR_SCAN_FMT,
    4217                 :            :                             ETH_ADDR_SCAN_ARGS(s->active_slave_mac))) {
    4218                 :            :         /* OVSDB did not store the last active interface */
    4219                 :         21 :         s->active_slave_mac = eth_addr_zero;
    4220                 :            :     }
    4221                 :         32 : }
    4222                 :            : 
    4223                 :            : /* Returns true if 'port' is synthetic, that is, if we constructed it locally
    4224                 :            :  * instead of obtaining it from the database. */
    4225                 :            : static bool
    4226                 :      77982 : port_is_synthetic(const struct port *port)
    4227                 :            : {
    4228                 :      77982 :     return ovsdb_idl_row_is_synthetic(&port->cfg->header_);
    4229                 :            : }
    4230                 :            : 
    4231                 :            : /* Interface functions. */
    4232                 :            : 
    4233                 :            : static bool
    4234                 :      37230 : iface_is_internal(const struct ovsrec_interface *iface,
    4235                 :            :                   const struct ovsrec_bridge *br)
    4236                 :            : {
    4237                 :            :     /* The local port and "internal" ports are always "internal". */
    4238 [ +  + ][ -  + ]:      37230 :     return !strcmp(iface->type, "internal") || !strcmp(iface->name, br->name);
    4239                 :            : }
    4240                 :            : 
    4241                 :            : /* Returns the correct network device type for interface 'iface' in bridge
    4242                 :            :  * 'br'. */
    4243                 :            : static const char *
    4244                 :      37230 : iface_get_type(const struct ovsrec_interface *iface,
    4245                 :            :                const struct ovsrec_bridge *br)
    4246                 :            : {
    4247                 :            :     const char *type;
    4248                 :            : 
    4249                 :            :     /* The local port always has type "internal".  Other ports take
    4250                 :            :      * their type from the database and default to "system" if none is
    4251                 :            :      * specified. */
    4252         [ +  + ]:      37230 :     if (iface_is_internal(iface, br)) {
    4253                 :       6240 :         type = "internal";
    4254                 :            :     } else {
    4255         [ +  + ]:      30990 :         type = iface->type[0] ? iface->type : "system";
    4256                 :            :     }
    4257                 :            : 
    4258                 :      37230 :     return type;
    4259                 :            : }
    4260                 :            : 
    4261                 :            : static void
    4262                 :       2689 : iface_destroy__(struct iface *iface)
    4263                 :            : {
    4264         [ +  - ]:       2689 :     if (iface) {
    4265                 :       2689 :         struct port *port = iface->port;
    4266                 :       2689 :         struct bridge *br = port->bridge;
    4267                 :            : 
    4268 [ +  - ][ +  - ]:       2689 :         if (br->ofproto && iface->ofp_port != OFPP_NONE) {
    4269                 :       2689 :             ofproto_port_unregister(br->ofproto, iface->ofp_port);
    4270                 :            :         }
    4271                 :            : 
    4272         [ +  - ]:       2689 :         if (iface->ofp_port != OFPP_NONE) {
    4273                 :       2689 :             hmap_remove(&br->ifaces, &iface->ofp_port_node);
    4274                 :            :         }
    4275                 :            : 
    4276                 :       2689 :         ovs_list_remove(&iface->port_elem);
    4277                 :       2689 :         hmap_remove(&br->iface_by_name, &iface->name_node);
    4278                 :            : 
    4279                 :            :         /* The user is changing configuration here, so netdev_remove needs to be
    4280                 :            :          * used as opposed to netdev_close */
    4281                 :       2689 :         netdev_remove(iface->netdev);
    4282                 :            : 
    4283                 :       2689 :         free(iface->name);
    4284                 :       2689 :         free(iface);
    4285                 :            :     }
    4286                 :       2689 : }
    4287                 :            : 
    4288                 :            : static void
    4289                 :        388 : iface_destroy(struct iface *iface)
    4290                 :            : {
    4291         [ +  + ]:        388 :     if (iface) {
    4292                 :        267 :         struct port *port = iface->port;
    4293                 :            : 
    4294                 :        267 :         iface_destroy__(iface);
    4295         [ +  - ]:        267 :         if (ovs_list_is_empty(&port->ifaces)) {
    4296                 :        267 :             port_destroy(port);
    4297                 :            :         }
    4298                 :            :     }
    4299                 :        388 : }
    4300                 :            : 
    4301                 :            : static struct iface *
    4302                 :      95837 : iface_lookup(const struct bridge *br, const char *name)
    4303                 :            : {
    4304                 :            :     struct iface *iface;
    4305                 :            : 
    4306 [ +  + ][ -  + ]:      95837 :     HMAP_FOR_EACH_WITH_HASH (iface, name_node, hash_string(name, 0),
    4307                 :            :                              &br->iface_by_name) {
    4308         [ +  - ]:      87881 :         if (!strcmp(iface->name, name)) {
    4309                 :      87881 :             return iface;
    4310                 :            :         }
    4311                 :            :     }
    4312                 :            : 
    4313                 :       7956 :     return NULL;
    4314                 :            : }
    4315                 :            : 
    4316                 :            : static struct iface *
    4317                 :          0 : iface_find(const char *name)
    4318                 :            : {
    4319                 :            :     const struct bridge *br;
    4320                 :            : 
    4321 [ #  # ][ #  # ]:          0 :     HMAP_FOR_EACH (br, node, &all_bridges) {
    4322                 :          0 :         struct iface *iface = iface_lookup(br, name);
    4323                 :            : 
    4324         [ #  # ]:          0 :         if (iface) {
    4325                 :          0 :             return iface;
    4326                 :            :         }
    4327                 :            :     }
    4328                 :          0 :     return NULL;
    4329                 :            : }
    4330                 :            : 
    4331                 :            : static struct iface *
    4332                 :       4886 : iface_from_ofp_port(const struct bridge *br, ofp_port_t ofp_port)
    4333                 :            : {
    4334                 :            :     struct iface *iface;
    4335                 :            : 
    4336 [ +  + ][ -  + ]:       6038 :     HMAP_FOR_EACH_IN_BUCKET (iface, ofp_port_node, hash_ofp_port(ofp_port),
    4337                 :            :                              &br->ifaces) {
    4338         [ +  + ]:       5992 :         if (iface->ofp_port == ofp_port) {
    4339                 :       4840 :             return iface;
    4340                 :            :         }
    4341                 :            :     }
    4342                 :         46 :     return NULL;
    4343                 :            : }
    4344                 :            : 
    4345                 :            : /* Set Ethernet address of 'iface', if one is specified in the configuration
    4346                 :            :  * file. */
    4347                 :            : static void
    4348                 :      31837 : iface_set_mac(const struct bridge *br, const struct port *port, struct iface *iface)
    4349                 :            : {
    4350                 :      31837 :     struct eth_addr ea, *mac = NULL;
    4351                 :            :     struct iface *hw_addr_iface;
    4352                 :            : 
    4353         [ +  + ]:      31837 :     if (strcmp(iface->type, "internal")) {
    4354                 :      27114 :         return;
    4355                 :            :     }
    4356                 :            : 
    4357 [ +  + ][ +  - ]:       4723 :     if (iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, &ea)) {
    4358                 :          1 :         mac = &ea;
    4359         [ -  + ]:       4722 :     } else if (port->cfg->fake_bridge) {
    4360                 :            :         /* Fake bridge and no MAC set in the configuration. Pick a local one. */
    4361                 :          0 :         find_local_hw_addr(br, &ea, port, &hw_addr_iface);
    4362                 :          0 :         mac = &ea;
    4363                 :            :     }
    4364                 :            : 
    4365         [ +  + ]:       4723 :     if (mac) {
    4366         [ -  + ]:          1 :         if (iface->ofp_port == OFPP_LOCAL) {
    4367         [ #  # ]:          0 :             VLOG_ERR("interface %s: ignoring mac in Interface record "
    4368                 :            :                      "(use Bridge record to set local port's mac)",
    4369                 :            :                      iface->name);
    4370         [ -  + ]:          1 :         } else if (eth_addr_is_multicast(*mac)) {
    4371         [ #  # ]:          0 :             VLOG_ERR("interface %s: cannot set MAC to multicast address",
    4372                 :            :                      iface->name);
    4373                 :            :         } else {
    4374                 :          1 :             int error = netdev_set_etheraddr(iface->netdev, *mac);
    4375         [ -  + ]:          1 :             if (error) {
    4376         [ #  # ]:       4723 :                 VLOG_ERR("interface %s: setting MAC failed (%s)",
    4377                 :            :                          iface->name, ovs_strerror(error));
    4378                 :            :             }
    4379                 :            :         }
    4380                 :            :     }
    4381                 :            : }
    4382                 :            : 
    4383                 :            : /* Sets the ofport column of 'if_cfg' to 'ofport'. */
    4384                 :            : static void
    4385                 :      31848 : iface_set_ofport(const struct ovsrec_interface *if_cfg, ofp_port_t ofport)
    4386                 :            : {
    4387 [ +  - ][ +  - ]:      31848 :     if (if_cfg && !ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
    4388         [ +  + ]:      31848 :         int64_t port = ofport == OFPP_NONE ? -1 : ofp_to_u16(ofport);
    4389                 :      31848 :         ovsrec_interface_set_ofport(if_cfg, &port, 1);
    4390                 :            :     }
    4391                 :      31848 : }
    4392                 :            : 
    4393                 :            : /* Clears all of the fields in 'if_cfg' that indicate interface status, and
    4394                 :            :  * sets the "ofport" field to -1.
    4395                 :            :  *
    4396                 :            :  * This is appropriate when 'if_cfg''s interface cannot be created or is
    4397                 :            :  * otherwise invalid. */
    4398                 :            : static void
    4399                 :         11 : iface_clear_db_record(const struct ovsrec_interface *if_cfg, char *errp)
    4400                 :            : {
    4401         [ +  - ]:         11 :     if (!ovsdb_idl_row_is_synthetic(&if_cfg->header_)) {
    4402                 :         11 :         iface_set_ofport(if_cfg, OFPP_NONE);
    4403                 :         11 :         ovsrec_interface_set_error(if_cfg, errp);
    4404                 :         11 :         ovsrec_interface_set_status(if_cfg, NULL);
    4405                 :         11 :         ovsrec_interface_set_admin_state(if_cfg, NULL);
    4406                 :         11 :         ovsrec_interface_set_duplex(if_cfg, NULL);
    4407                 :         11 :         ovsrec_interface_set_link_speed(if_cfg, NULL, 0);
    4408                 :         11 :         ovsrec_interface_set_link_state(if_cfg, NULL);
    4409                 :         11 :         ovsrec_interface_set_mac_in_use(if_cfg, NULL);
    4410                 :         11 :         ovsrec_interface_set_mtu(if_cfg, NULL, 0);
    4411                 :         11 :         ovsrec_interface_set_cfm_fault(if_cfg, NULL, 0);
    4412                 :         11 :         ovsrec_interface_set_cfm_fault_status(if_cfg, NULL, 0);
    4413                 :         11 :         ovsrec_interface_set_cfm_remote_mpids(if_cfg, NULL, 0);
    4414                 :         11 :         ovsrec_interface_set_lacp_current(if_cfg, NULL, 0);
    4415                 :         11 :         ovsrec_interface_set_statistics(if_cfg, NULL, NULL, 0);
    4416                 :         11 :         ovsrec_interface_set_ifindex(if_cfg, NULL, 0);
    4417                 :            :     }
    4418                 :         11 : }
    4419                 :            : 
    4420                 :            : static bool
    4421                 :          1 : queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
    4422                 :            : {
    4423                 :            :     union ovsdb_atom atom;
    4424                 :            : 
    4425                 :          1 :     atom.integer = target;
    4426                 :          1 :     return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
    4427                 :            : }
    4428                 :            : 
    4429                 :            : static void
    4430                 :      31837 : iface_configure_qos(struct iface *iface, const struct ovsrec_qos *qos)
    4431                 :            : {
    4432                 :            :     struct ofpbuf queues_buf;
    4433                 :            : 
    4434                 :      31837 :     ofpbuf_init(&queues_buf, 0);
    4435                 :            : 
    4436 [ +  + ][ -  + ]:      31837 :     if (!qos || qos->type[0] == '\0') {
    4437                 :      31836 :         netdev_set_qos(iface->netdev, NULL, NULL);
    4438                 :            :     } else {
    4439                 :            :         const struct ovsdb_datum *queues;
    4440                 :            :         struct netdev_queue_dump dump;
    4441                 :            :         unsigned int queue_id;
    4442                 :            :         struct smap details;
    4443                 :            :         bool queue_zero;
    4444                 :            :         size_t i;
    4445                 :            : 
    4446                 :            :         /* Configure top-level Qos for 'iface'. */
    4447                 :          1 :         netdev_set_qos(iface->netdev, qos->type, &qos->other_config);
    4448                 :            : 
    4449                 :            :         /* Deconfigure queues that were deleted. */
    4450                 :          1 :         queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
    4451                 :            :                                        OVSDB_TYPE_UUID);
    4452                 :          1 :         smap_init(&details);
    4453 [ +  + ][ +  + ]:          2 :         NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &dump, iface->netdev) {
    4454         [ +  - ]:          1 :             if (!queue_ids_include(queues, queue_id)) {
    4455                 :          1 :                 netdev_delete_queue(iface->netdev, queue_id);
    4456                 :            :             }
    4457                 :            :         }
    4458                 :          1 :         smap_destroy(&details);
    4459                 :            : 
    4460                 :            :         /* Configure queues for 'iface'. */
    4461                 :          1 :         queue_zero = false;
    4462         [ +  + ]:          3 :         for (i = 0; i < qos->n_queues; i++) {
    4463                 :          2 :             const struct ovsrec_queue *queue = qos->value_queues[i];
    4464                 :          2 :             unsigned int queue_id = qos->key_queues[i];
    4465                 :            : 
    4466         [ -  + ]:          2 :             if (queue_id == 0) {
    4467                 :          0 :                 queue_zero = true;
    4468                 :            :             }
    4469                 :            : 
    4470         [ +  - ]:          2 :             if (queue->n_dscp == 1) {
    4471                 :            :                 struct ofproto_port_queue *port_queue;
    4472                 :            : 
    4473                 :          2 :                 port_queue = ofpbuf_put_uninit(&queues_buf,
    4474                 :            :                                                sizeof *port_queue);
    4475                 :          2 :                 port_queue->queue = queue_id;
    4476                 :          2 :                 port_queue->dscp = queue->dscp[0];
    4477                 :            :             }
    4478                 :            : 
    4479                 :          2 :             netdev_set_queue(iface->netdev, queue_id, &queue->other_config);
    4480                 :            :         }
    4481         [ +  - ]:          1 :         if (!queue_zero) {
    4482                 :            :             struct smap details;
    4483                 :            : 
    4484                 :          1 :             smap_init(&details);
    4485                 :          1 :             netdev_set_queue(iface->netdev, 0, &details);
    4486                 :          1 :             smap_destroy(&details);
    4487                 :            :         }
    4488                 :            :     }
    4489                 :            : 
    4490         [ +  - ]:      31837 :     if (iface->ofp_port != OFPP_NONE) {
    4491                 :      31837 :         const struct ofproto_port_queue *port_queues = queues_buf.data;
    4492                 :      31837 :         size_t n_queues = queues_buf.size / sizeof *port_queues;
    4493                 :            : 
    4494                 :      31837 :         ofproto_port_set_queues(iface->port->bridge->ofproto, iface->ofp_port,
    4495                 :            :                                 port_queues, n_queues);
    4496                 :            :     }
    4497                 :            : 
    4498                 :      31837 :     netdev_set_policing(iface->netdev,
    4499                 :      31837 :                         MIN(UINT32_MAX, iface->cfg->ingress_policing_rate),
    4500                 :      31837 :                         MIN(UINT32_MAX, iface->cfg->ingress_policing_burst));
    4501                 :            : 
    4502                 :      31837 :     ofpbuf_uninit(&queues_buf);
    4503                 :      31837 : }
    4504                 :            : 
    4505                 :            : static void
    4506                 :      31837 : iface_configure_cfm(struct iface *iface)
    4507                 :            : {
    4508                 :      31837 :     const struct ovsrec_interface *cfg = iface->cfg;
    4509                 :            :     const char *opstate_str;
    4510                 :            :     const char *cfm_ccm_vlan;
    4511                 :            :     struct cfm_settings s;
    4512                 :            :     struct smap netdev_args;
    4513                 :            : 
    4514         [ +  + ]:      31837 :     if (!cfg->n_cfm_mpid) {
    4515                 :      31805 :         ofproto_port_clear_cfm(iface->port->bridge->ofproto, iface->ofp_port);
    4516                 :      31805 :         return;
    4517                 :            :     }
    4518                 :            : 
    4519                 :         32 :     s.check_tnl_key = false;
    4520                 :         32 :     smap_init(&netdev_args);
    4521         [ +  - ]:         32 :     if (!netdev_get_config(iface->netdev, &netdev_args)) {
    4522                 :         32 :         const char *key = smap_get(&netdev_args, "key");
    4523                 :         32 :         const char *in_key = smap_get(&netdev_args, "in_key");
    4524                 :            : 
    4525         [ #  # ]:         32 :         s.check_tnl_key = (key && !strcmp(key, "flow"))
    4526 [ -  + ][ -  + ]:         32 :                            || (in_key && !strcmp(in_key, "flow"));
                 [ #  # ]
    4527                 :            :     }
    4528                 :         32 :     smap_destroy(&netdev_args);
    4529                 :            : 
    4530                 :         32 :     s.mpid = *cfg->cfm_mpid;
    4531                 :         32 :     s.interval = smap_get_int(&iface->cfg->other_config, "cfm_interval", 0);
    4532                 :         32 :     cfm_ccm_vlan = smap_get(&iface->cfg->other_config, "cfm_ccm_vlan");
    4533                 :         32 :     s.ccm_pcp = smap_get_int(&iface->cfg->other_config, "cfm_ccm_pcp", 0);
    4534                 :            : 
    4535         [ +  + ]:         32 :     if (s.interval <= 0) {
    4536                 :          8 :         s.interval = 1000;
    4537                 :            :     }
    4538                 :            : 
    4539         [ +  - ]:         32 :     if (!cfm_ccm_vlan) {
    4540                 :         32 :         s.ccm_vlan = 0;
    4541         [ #  # ]:          0 :     } else if (!strcasecmp("random", cfm_ccm_vlan)) {
    4542                 :          0 :         s.ccm_vlan = CFM_RANDOM_VLAN;
    4543                 :            :     } else {
    4544                 :          0 :         s.ccm_vlan = atoi(cfm_ccm_vlan);
    4545         [ #  # ]:          0 :         if (s.ccm_vlan == CFM_RANDOM_VLAN) {
    4546                 :          0 :             s.ccm_vlan = 0;
    4547                 :            :         }
    4548                 :            :     }
    4549                 :            : 
    4550                 :         32 :     s.extended = smap_get_bool(&iface->cfg->other_config, "cfm_extended",
    4551                 :            :                                false);
    4552                 :         32 :     s.demand = smap_get_bool(&iface->cfg->other_config, "cfm_demand", false);
    4553                 :            : 
    4554                 :         32 :     opstate_str = smap_get(&iface->cfg->other_config, "cfm_opstate");
    4555 [ -  + ][ #  # ]:         32 :     s.opup = !opstate_str || !strcasecmp("up", opstate_str);
    4556                 :            : 
    4557                 :         32 :     ofproto_port_set_cfm(iface->port->bridge->ofproto, iface->ofp_port, &s);
    4558                 :            : }
    4559                 :            : 
    4560                 :            : /* Returns true if 'iface' is synthetic, that is, if we constructed it locally
    4561                 :            :  * instead of obtaining it from the database. */
    4562                 :            : static bool
    4563                 :      83604 : iface_is_synthetic(const struct iface *iface)
    4564                 :            : {
    4565                 :      83604 :     return ovsdb_idl_row_is_synthetic(&iface->cfg->header_);
    4566                 :            : }
    4567                 :            : 
    4568                 :            : static ofp_port_t
    4569                 :      97572 : iface_validate_ofport__(size_t n, int64_t *ofport)
    4570                 :            : {
    4571 [ +  - ][ +  + ]:     104376 :     return (n && *ofport >= 1 && *ofport < ofp_to_u16(OFPP_MAX)
    4572                 :       6793 :             ? u16_to_ofp(*ofport)
    4573         [ +  + ]:     104376 :             : OFPP_NONE);
    4574                 :            : }
    4575                 :            : 
    4576                 :            : static ofp_port_t
    4577                 :      95841 : iface_get_requested_ofp_port(const struct ovsrec_interface *cfg)
    4578                 :            : {
    4579                 :      95841 :     return iface_validate_ofport__(cfg->n_ofport_request, cfg->ofport_request);
    4580                 :            : }
    4581                 :            : 
    4582                 :            : static ofp_port_t
    4583                 :       2699 : iface_pick_ofport(const struct ovsrec_interface *cfg)
    4584                 :            : {
    4585                 :       2699 :     ofp_port_t requested_ofport = iface_get_requested_ofp_port(cfg);
    4586                 :       2699 :     return (requested_ofport != OFPP_NONE
    4587                 :            :             ? requested_ofport
    4588         [ +  + ]:       2699 :             : iface_validate_ofport__(cfg->n_ofport, cfg->ofport));
    4589                 :            : }
    4590                 :            : 
    4591                 :            : /* Port mirroring. */
    4592                 :            : 
    4593                 :            : static struct mirror *
    4594                 :         22 : mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
    4595                 :            : {
    4596                 :            :     struct mirror *m;
    4597                 :            : 
    4598 [ +  + ][ -  + ]:         24 :     HMAP_FOR_EACH_IN_BUCKET (m, hmap_node, uuid_hash(uuid), &br->mirrors) {
    4599         [ -  + ]:          2 :         if (uuid_equals(uuid, &m->uuid)) {
    4600                 :          0 :             return m;
    4601                 :            :         }
    4602                 :            :     }
    4603                 :         22 :     return NULL;
    4604                 :            : }
    4605                 :            : 
    4606                 :            : static void
    4607                 :       4700 : bridge_configure_mirrors(struct bridge *br)
    4608                 :            : {
    4609                 :            :     const struct ovsdb_datum *mc;
    4610                 :            :     unsigned long *flood_vlans;
    4611                 :            :     struct mirror *m, *next;
    4612                 :            :     size_t i;
    4613                 :            : 
    4614                 :            :     /* Get rid of deleted mirrors. */
    4615                 :       4700 :     mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
    4616 [ +  - ][ -  + ]:       4700 :     HMAP_FOR_EACH_SAFE (m, next, hmap_node, &br->mirrors) {
                 [ -  + ]
    4617                 :            :         union ovsdb_atom atom;
    4618                 :            : 
    4619                 :          0 :         atom.uuid = m->uuid;
    4620         [ #  # ]:          0 :         if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
    4621                 :          0 :             mirror_destroy(m);
    4622                 :            :         }
    4623                 :            :     }
    4624                 :            : 
    4625                 :            :     /* Add new mirrors and reconfigure existing ones. */
    4626         [ +  + ]:       4722 :     for (i = 0; i < br->cfg->n_mirrors; i++) {
    4627                 :         22 :         const struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
    4628                 :         22 :         struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
    4629         [ +  - ]:         22 :         if (!m) {
    4630                 :         22 :             m = mirror_create(br, cfg);
    4631                 :            :         }
    4632                 :         22 :         m->cfg = cfg;
    4633         [ -  + ]:         22 :         if (!mirror_configure(m)) {
    4634                 :          0 :             mirror_destroy(m);
    4635                 :            :         }
    4636                 :            :     }
    4637                 :            : 
    4638                 :            :     /* Update flooded vlans (for RSPAN). */
    4639                 :       4700 :     flood_vlans = vlan_bitmap_from_array(br->cfg->flood_vlans,
    4640                 :       4700 :                                          br->cfg->n_flood_vlans);
    4641                 :       4700 :     ofproto_set_flood_vlans(br->ofproto, flood_vlans);
    4642                 :       4700 :     bitmap_free(flood_vlans);
    4643                 :       4700 : }
    4644                 :            : 
    4645                 :            : static struct mirror *
    4646                 :         22 : mirror_create(struct bridge *br, const struct ovsrec_mirror *cfg)
    4647                 :            : {
    4648                 :            :     struct mirror *m;
    4649                 :            : 
    4650                 :         22 :     m = xzalloc(sizeof *m);
    4651                 :         22 :     m->uuid = cfg->header_.uuid;
    4652                 :         22 :     hmap_insert(&br->mirrors, &m->hmap_node, uuid_hash(&m->uuid));
    4653                 :         22 :     m->bridge = br;
    4654                 :         22 :     m->name = xstrdup(cfg->name);
    4655                 :            : 
    4656                 :         22 :     return m;
    4657                 :            : }
    4658                 :            : 
    4659                 :            : static void
    4660                 :         22 : mirror_destroy(struct mirror *m)
    4661                 :            : {
    4662         [ +  - ]:         22 :     if (m) {
    4663                 :         22 :         struct bridge *br = m->bridge;
    4664                 :            : 
    4665         [ +  - ]:         22 :         if (br->ofproto) {
    4666                 :         22 :             ofproto_mirror_unregister(br->ofproto, m);
    4667                 :            :         }
    4668                 :            : 
    4669                 :         22 :         hmap_remove(&br->mirrors, &m->hmap_node);
    4670                 :         22 :         free(m->name);
    4671                 :         22 :         free(m);
    4672                 :            :     }
    4673                 :         22 : }
    4674                 :            : 
    4675                 :            : static void
    4676                 :         10 : mirror_collect_ports(struct mirror *m,
    4677                 :            :                      struct ovsrec_port **in_ports, int n_in_ports,
    4678                 :            :                      void ***out_portsp, size_t *n_out_portsp)
    4679                 :            : {
    4680                 :         10 :     void **out_ports = xmalloc(n_in_ports * sizeof *out_ports);
    4681                 :         10 :     size_t n_out_ports = 0;
    4682                 :            :     size_t i;
    4683                 :            : 
    4684         [ +  + ]:         17 :     for (i = 0; i < n_in_ports; i++) {
    4685                 :          7 :         const char *name = in_ports[i]->name;
    4686                 :          7 :         struct port *port = port_lookup(m->bridge, name);
    4687         [ +  - ]:          7 :         if (port) {
    4688                 :          7 :             out_ports[n_out_ports++] = port;
    4689                 :            :         } else {
    4690         [ #  # ]:          0 :             VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
    4691                 :            :                       "port %s", m->bridge->name, m->name, name);
    4692                 :            :         }
    4693                 :            :     }
    4694                 :         10 :     *out_portsp = out_ports;
    4695                 :         10 :     *n_out_portsp = n_out_ports;
    4696                 :         10 : }
    4697                 :            : 
    4698                 :            : static bool
    4699                 :         22 : mirror_configure(struct mirror *m)
    4700                 :            : {
    4701                 :         22 :     const struct ovsrec_mirror *cfg = m->cfg;
    4702                 :            :     struct ofproto_mirror_settings s;
    4703                 :            : 
    4704                 :            :     /* Set name. */
    4705         [ -  + ]:         22 :     if (strcmp(cfg->name, m->name)) {
    4706                 :          0 :         free(m->name);
    4707                 :          0 :         m->name = xstrdup(cfg->name);
    4708                 :            :     }
    4709                 :         22 :     s.name = m->name;
    4710                 :            : 
    4711                 :            :     /* Get output port or VLAN. */
    4712         [ +  + ]:         22 :     if (cfg->output_port) {
    4713                 :         16 :         s.out_bundle = port_lookup(m->bridge, cfg->output_port->name);
    4714         [ -  + ]:         16 :         if (!s.out_bundle) {
    4715         [ #  # ]:          0 :             VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
    4716                 :            :                      m->bridge->name, m->name);
    4717                 :          0 :             return false;
    4718                 :            :         }
    4719                 :         16 :         s.out_vlan = UINT16_MAX;
    4720                 :            : 
    4721         [ -  + ]:         16 :         if (cfg->output_vlan) {
    4722         [ #  # ]:         16 :             VLOG_ERR("bridge %s: mirror %s specifies both output port and "
    4723                 :            :                      "output vlan; ignoring output vlan",
    4724                 :            :                      m->bridge->name, m->name);
    4725                 :            :         }
    4726         [ +  - ]:          6 :     } else if (cfg->output_vlan) {
    4727                 :            :         /* The database should prevent invalid VLAN values. */
    4728                 :          6 :         s.out_bundle = NULL;
    4729                 :          6 :         s.out_vlan = *cfg->output_vlan;
    4730                 :            :     } else {
    4731         [ #  # ]:          0 :         VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
    4732                 :            :                  m->bridge->name, m->name);
    4733                 :          0 :         return false;
    4734                 :            :     }
    4735                 :            : 
    4736         [ +  + ]:         22 :     if (cfg->snaplen) {
    4737                 :          9 :         s.snaplen = *cfg->snaplen;
    4738                 :            :     } else {
    4739                 :         13 :         s.snaplen = 0;
    4740                 :            :     }
    4741                 :            : 
    4742                 :            :     /* Get port selection. */
    4743         [ +  + ]:         22 :     if (cfg->select_all) {
    4744                 :         17 :         size_t n_ports = hmap_count(&m->bridge->ports);
    4745                 :         17 :         void **ports = xmalloc(n_ports * sizeof *ports);
    4746                 :            :         struct port *port;
    4747                 :            :         size_t i;
    4748                 :            : 
    4749                 :         17 :         i = 0;
    4750 [ +  + ][ -  + ]:         82 :         HMAP_FOR_EACH (port, hmap_node, &m->bridge->ports) {
    4751                 :         65 :             ports[i++] = port;
    4752                 :            :         }
    4753                 :            : 
    4754                 :         17 :         s.srcs = ports;
    4755                 :         17 :         s.n_srcs = n_ports;
    4756                 :            : 
    4757                 :         17 :         s.dsts = ports;
    4758                 :         17 :         s.n_dsts = n_ports;
    4759                 :            :     } else {
    4760                 :            :         /* Get ports, dropping ports that don't exist.
    4761                 :            :          * The IDL ensures that there are no duplicates. */
    4762                 :          5 :         mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
    4763                 :            :                              &s.srcs, &s.n_srcs);
    4764                 :          5 :         mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
    4765                 :            :                              &s.dsts, &s.n_dsts);
    4766                 :            :     }
    4767                 :            : 
    4768                 :            :     /* Get VLAN selection. */
    4769                 :         22 :     s.src_vlans = vlan_bitmap_from_array(cfg->select_vlan, cfg->n_select_vlan);
    4770                 :            : 
    4771                 :            :     /* Configure. */
    4772                 :         22 :     ofproto_mirror_register(m->bridge->ofproto, m, &s);
    4773                 :            : 
    4774                 :            :     /* Clean up. */
    4775         [ +  + ]:         22 :     if (s.srcs != s.dsts) {
    4776                 :          5 :         free(s.dsts);
    4777                 :            :     }
    4778                 :         22 :     free(s.srcs);
    4779                 :         22 :     free(s.src_vlans);
    4780                 :            : 
    4781                 :         22 :     return true;
    4782                 :            : }
    4783                 :            : 
    4784                 :            : 
    4785                 :            : static void
    4786                 :          0 : mirror_refresh_stats(struct mirror *m)
    4787                 :            : {
    4788                 :          0 :     struct ofproto *ofproto = m->bridge->ofproto;
    4789                 :            :     uint64_t tx_packets, tx_bytes;
    4790                 :            :     const char *keys[2];
    4791                 :            :     int64_t values[2];
    4792                 :          0 :     size_t stat_cnt = 0;
    4793                 :            : 
    4794         [ #  # ]:          0 :     if (ofproto_mirror_get_stats(ofproto, m, &tx_packets, &tx_bytes)) {
    4795                 :          0 :         ovsrec_mirror_set_statistics(m->cfg, NULL, NULL, 0);
    4796                 :          0 :         return;
    4797                 :            :     }
    4798                 :            : 
    4799         [ #  # ]:          0 :     if (tx_packets != UINT64_MAX) {
    4800                 :          0 :         keys[stat_cnt] = "tx_packets";
    4801                 :          0 :         values[stat_cnt] = tx_packets;
    4802                 :          0 :         stat_cnt++;
    4803                 :            :     }
    4804         [ #  # ]:          0 :     if (tx_bytes != UINT64_MAX) {
    4805                 :          0 :         keys[stat_cnt] = "tx_bytes";
    4806                 :          0 :         values[stat_cnt] = tx_bytes;
    4807                 :          0 :         stat_cnt++;
    4808                 :            :     }
    4809                 :            : 
    4810                 :          0 :     ovsrec_mirror_set_statistics(m->cfg, keys, values, stat_cnt);
    4811                 :            : }
    4812                 :            : 
    4813                 :            : /*
    4814                 :            :  * Add registered netdev and dpif types to ovsdb to allow external
    4815                 :            :  * applications to query the capabilities of the Open vSwitch instance
    4816                 :            :  * running on the node.
    4817                 :            :  */
    4818                 :            : static void
    4819                 :       4231 : discover_types(const struct ovsrec_open_vswitch *cfg)
    4820                 :            : {
    4821                 :            :     struct sset types;
    4822                 :            : 
    4823                 :            :     /* Datapath types. */
    4824                 :       4231 :     sset_init(&types);
    4825                 :       4231 :     dp_enumerate_types(&types);
    4826                 :       4231 :     const char **datapath_types = sset_array(&types);
    4827                 :       4231 :     ovsrec_open_vswitch_set_datapath_types(cfg, datapath_types,
    4828                 :            :                                            sset_count(&types));
    4829                 :       4231 :     free(datapath_types);
    4830                 :       4231 :     sset_destroy(&types);
    4831                 :            : 
    4832                 :            :     /* Port types. */
    4833                 :       4231 :     sset_init(&types);
    4834                 :       4231 :     netdev_enumerate_types(&types);
    4835                 :       4231 :     const char **iface_types = sset_array(&types);
    4836                 :       4231 :     ovsrec_open_vswitch_set_iface_types(cfg, iface_types, sset_count(&types));
    4837                 :       4231 :     free(iface_types);
    4838                 :       4231 :     sset_destroy(&types);
    4839                 :       4231 : }

Generated by: LCOV version 1.12