LCOV - code coverage report
Current view: top level - ofproto - in-band.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 150 199 75.4 %
Date: 2016-09-14 01:02:56 Functions: 14 15 93.3 %
Branches: 50 109 45.9 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at:
       7                 :            :  *
       8                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : #include <config.h>
      18                 :            : #include <arpa/inet.h>
      19                 :            : #include <errno.h>
      20                 :            : #include <inttypes.h>
      21                 :            : #include <sys/socket.h>
      22                 :            : #include <net/if.h>
      23                 :            : #include <string.h>
      24                 :            : #include <stdlib.h>
      25                 :            : #include "classifier.h"
      26                 :            : #include "dhcp.h"
      27                 :            : #include "flow.h"
      28                 :            : #include "in-band.h"
      29                 :            : #include "netdev.h"
      30                 :            : #include "netlink.h"
      31                 :            : #include "odp-util.h"
      32                 :            : #include "ofproto.h"
      33                 :            : #include "ofproto-provider.h"
      34                 :            : #include "openflow/openflow.h"
      35                 :            : #include "openvswitch/ofp-actions.h"
      36                 :            : #include "openvswitch/ofpbuf.h"
      37                 :            : #include "openvswitch/vlog.h"
      38                 :            : #include "packets.h"
      39                 :            : #include "poll-loop.h"
      40                 :            : #include "timeval.h"
      41                 :            : 
      42                 :       1288 : VLOG_DEFINE_THIS_MODULE(in_band);
      43                 :            : 
      44                 :            : /* Priorities used in classifier for in-band rules.  These values are higher
      45                 :            :  * than any that may be set with OpenFlow, and "18" kind of looks like "IB".
      46                 :            :  * The ordering of priorities is not important because all of the rules set up
      47                 :            :  * by in-band control have the same action.  The only reason to use more than
      48                 :            :  * one priority is to make the kind of flow easier to see during debugging. */
      49                 :            : enum {
      50                 :            :     /* One set per bridge. */
      51                 :            :     IBR_FROM_LOCAL_DHCP = 180000, /* (a) From local port, DHCP. */
      52                 :            :     IBR_TO_LOCAL_ARP,             /* (b) To local port, ARP. */
      53                 :            :     IBR_FROM_LOCAL_ARP,           /* (c) From local port, ARP. */
      54                 :            : 
      55                 :            :     /* One set per unique next-hop MAC. */
      56                 :            :     IBR_TO_NEXT_HOP_ARP,          /* (d) To remote MAC, ARP. */
      57                 :            :     IBR_FROM_NEXT_HOP_ARP,        /* (e) From remote MAC, ARP. */
      58                 :            : 
      59                 :            :     /* One set per unique remote IP address. */
      60                 :            :     IBR_TO_REMOTE_ARP,            /* (f) To remote IP, ARP. */
      61                 :            :     IBR_FROM_REMOTE_ARP,          /* (g) From remote IP, ARP. */
      62                 :            : 
      63                 :            :     /* One set per unique remote (IP,port) pair. */
      64                 :            :     IBR_TO_REMOTE_TCP,            /* (h) To remote IP, TCP port. */
      65                 :            :     IBR_FROM_REMOTE_TCP           /* (i) From remote IP, TCP port. */
      66                 :            : };
      67                 :            : 
      68                 :            : /* Track one remote IP and next hop information. */
      69                 :            : struct in_band_remote {
      70                 :            :     struct sockaddr_in remote_addr;  /* IP address, in network byte order. */
      71                 :            :     struct eth_addr remote_mac;      /* Next-hop MAC, all-zeros if unknown. */
      72                 :            :     struct eth_addr last_remote_mac; /* Previous nonzero next-hop MAC. */
      73                 :            :     struct netdev *remote_netdev;    /* Device to send to next-hop MAC. */
      74                 :            : };
      75                 :            : 
      76                 :            : /* What to do to an in_band_rule. */
      77                 :            : enum in_band_op {
      78                 :            :     ADD,                       /* Add the rule to ofproto's flow table. */
      79                 :            :     DEL                        /* Delete the rule from ofproto's flow table. */
      80                 :            : };
      81                 :            : 
      82                 :            : /* A rule to add to or delete from ofproto's flow table.  */
      83                 :            : struct in_band_rule {
      84                 :            :     struct hmap_node hmap_node; /* In struct in_band's "rules" hmap. */
      85                 :            :     struct match match;
      86                 :            :     int priority;
      87                 :            :     enum in_band_op op;
      88                 :            : };
      89                 :            : 
      90                 :            : struct in_band {
      91                 :            :     struct ofproto *ofproto;
      92                 :            :     int queue_id;
      93                 :            : 
      94                 :            :     /* Remote information. */
      95                 :            :     time_t next_remote_refresh; /* Refresh timer. */
      96                 :            :     struct in_band_remote *remotes;
      97                 :            :     size_t n_remotes;
      98                 :            : 
      99                 :            :     /* Local information. */
     100                 :            :     time_t next_local_refresh;       /* Refresh timer. */
     101                 :            :     struct eth_addr local_mac;       /* Current MAC. */
     102                 :            :     struct netdev *local_netdev;     /* Local port's network device. */
     103                 :            : 
     104                 :            :     /* Flow tracking. */
     105                 :            :     struct hmap rules;          /* Contains "struct in_band_rule"s. */
     106                 :            : };
     107                 :            : 
     108                 :            : static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
     109                 :            : 
     110                 :            : static int
     111                 :         12 : refresh_remote(struct in_band *ib, struct in_band_remote *r)
     112                 :            : {
     113                 :            :     struct in_addr next_hop_inaddr;
     114                 :            :     char *next_hop_dev;
     115                 :            :     int retval;
     116                 :            : 
     117                 :            :     /* Find the next-hop IP address. */
     118                 :         12 :     r->remote_mac = eth_addr_zero;
     119                 :         12 :     retval = netdev_get_next_hop(ib->local_netdev, &r->remote_addr.sin_addr,
     120                 :            :                                  &next_hop_inaddr, &next_hop_dev);
     121         [ +  - ]:         12 :     if (retval) {
     122         [ +  - ]:         12 :         VLOG_WARN("%s: cannot find route for controller ("IP_FMT"): %s",
     123                 :            :                   ib->ofproto->name, IP_ARGS(r->remote_addr.sin_addr.s_addr),
     124                 :            :                   ovs_strerror(retval));
     125                 :         12 :         return 1;
     126                 :            :     }
     127         [ #  # ]:          0 :     if (!next_hop_inaddr.s_addr) {
     128                 :          0 :         next_hop_inaddr = r->remote_addr.sin_addr;
     129                 :            :     }
     130                 :            : 
     131                 :            :     /* Open the next-hop network device. */
     132         [ #  # ]:          0 :     if (!r->remote_netdev
     133         [ #  # ]:          0 :         || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev))
     134                 :            :     {
     135                 :          0 :         netdev_close(r->remote_netdev);
     136                 :            : 
     137                 :          0 :         retval = netdev_open(next_hop_dev, NULL, &r->remote_netdev);
     138         [ #  # ]:          0 :         if (retval) {
     139         [ #  # ]:          0 :             VLOG_WARN_RL(&rl, "%s: cannot open netdev %s (next hop "
     140                 :            :                          "to controller "IP_FMT"): %s",
     141                 :            :                          ib->ofproto->name, next_hop_dev,
     142                 :            :                          IP_ARGS(r->remote_addr.sin_addr.s_addr),
     143                 :            :                          ovs_strerror(retval));
     144                 :          0 :             free(next_hop_dev);
     145                 :          0 :             return 1;
     146                 :            :         }
     147                 :            :     }
     148                 :          0 :     free(next_hop_dev);
     149                 :            : 
     150                 :            :     /* Look up the MAC address of the next-hop IP address. */
     151                 :          0 :     retval = netdev_arp_lookup(r->remote_netdev, next_hop_inaddr.s_addr,
     152                 :            :                                &r->remote_mac);
     153         [ #  # ]:          0 :     if (retval) {
     154         [ #  # ]:          0 :         VLOG_DBG_RL(&rl, "%s: cannot look up remote MAC address ("IP_FMT"): %s",
     155                 :            :                     ib->ofproto->name, IP_ARGS(next_hop_inaddr.s_addr),
     156                 :            :                     ovs_strerror(retval));
     157                 :            :     }
     158                 :            : 
     159                 :            :     /* If we don't have a MAC address, then refresh quickly, since we probably
     160                 :            :      * will get a MAC address soon (via ARP).  Otherwise, we can afford to wait
     161                 :            :      * a little while. */
     162         [ #  # ]:         12 :     return eth_addr_is_zero(r->remote_mac) ? 1 : 10;
     163                 :            : }
     164                 :            : 
     165                 :            : static bool
     166                 :         48 : refresh_remotes(struct in_band *ib)
     167                 :            : {
     168                 :            :     struct in_band_remote *r;
     169                 :            :     bool any_changes;
     170                 :            : 
     171         [ +  + ]:         48 :     if (time_now() < ib->next_remote_refresh) {
     172                 :         36 :         return false;
     173                 :            :     }
     174                 :            : 
     175                 :         12 :     any_changes = false;
     176                 :         12 :     ib->next_remote_refresh = TIME_MAX;
     177         [ +  + ]:         24 :     for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
     178                 :            :         struct eth_addr old_remote_mac;
     179                 :            :         time_t next_refresh;
     180                 :            : 
     181                 :            :         /* Save old MAC. */
     182                 :         12 :         old_remote_mac = r->remote_mac;
     183                 :            : 
     184                 :            :         /* Refresh remote information. */
     185                 :         12 :         next_refresh = refresh_remote(ib, r) + time_now();
     186                 :         12 :         ib->next_remote_refresh = MIN(ib->next_remote_refresh, next_refresh);
     187                 :            : 
     188                 :            :         /* If the MAC changed, log the changes. */
     189         [ -  + ]:         12 :         if (!eth_addr_equals(r->remote_mac, old_remote_mac)) {
     190                 :          0 :             any_changes = true;
     191         [ #  # ]:          0 :             if (!eth_addr_is_zero(r->remote_mac)
     192         [ #  # ]:          0 :                 && !eth_addr_equals(r->last_remote_mac, r->remote_mac)) {
     193         [ #  # ]:          0 :                 VLOG_DBG("%s: remote MAC address changed from "ETH_ADDR_FMT
     194                 :            :                          " to "ETH_ADDR_FMT,
     195                 :            :                          ib->ofproto->name,
     196                 :            :                          ETH_ADDR_ARGS(r->last_remote_mac),
     197                 :            :                          ETH_ADDR_ARGS(r->remote_mac));
     198                 :          0 :                 r->last_remote_mac = r->remote_mac;
     199                 :            :             }
     200                 :            :         }
     201                 :            :     }
     202                 :            : 
     203                 :         12 :     return any_changes;
     204                 :            : }
     205                 :            : 
     206                 :            : /* Refreshes the MAC address of the local port into ib->local_mac, if it is due
     207                 :            :  * for a refresh.  Returns true if anything changed, otherwise false.  */
     208                 :            : static bool
     209                 :         48 : refresh_local(struct in_band *ib)
     210                 :            : {
     211                 :            :     struct eth_addr ea;
     212                 :            :     time_t now;
     213                 :            : 
     214                 :         48 :     now = time_now();
     215         [ +  + ]:         48 :     if (now < ib->next_local_refresh) {
     216                 :         36 :         return false;
     217                 :            :     }
     218                 :         12 :     ib->next_local_refresh = now + 1;
     219                 :            : 
     220         [ +  - ]:         12 :     if (netdev_get_etheraddr(ib->local_netdev, &ea)
     221         [ +  + ]:         12 :         || eth_addr_equals(ea, ib->local_mac)) {
     222                 :         11 :         return false;
     223                 :            :     }
     224                 :            : 
     225                 :          1 :     ib->local_mac = ea;
     226                 :         48 :     return true;
     227                 :            : }
     228                 :            : 
     229                 :            : /* Returns true if packets in 'flow' should be directed to the local port.
     230                 :            :  * (This keeps the flow table from preventing DHCP replies from being seen by
     231                 :            :  * the local port.) */
     232                 :            : bool
     233                 :          0 : in_band_must_output_to_local_port(const struct flow *flow)
     234                 :            : {
     235                 :          0 :     return (flow->dl_type == htons(ETH_TYPE_IP)
     236         [ #  # ]:          0 :             && flow->nw_proto == IPPROTO_UDP
     237         [ #  # ]:          0 :             && flow->tp_src == htons(DHCP_SERVER_PORT)
     238 [ #  # ][ #  # ]:          0 :             && flow->tp_dst == htons(DHCP_CLIENT_PORT));
     239                 :            : }
     240                 :            : 
     241                 :            : /* Returns the number of in-band rules currently installed in the flow
     242                 :            :  * table. */
     243                 :            : int
     244                 :          1 : in_band_count_rules(const struct in_band *ib)
     245                 :            : {
     246                 :          1 :     return hmap_count(&ib->rules);
     247                 :            : }
     248                 :            : 
     249                 :            : static void
     250                 :        336 : add_rule(struct in_band *ib, const struct match *match, int priority)
     251                 :            : {
     252                 :        336 :     uint32_t hash = match_hash(match, 0);
     253                 :            :     struct in_band_rule *rule;
     254                 :            : 
     255 [ +  + ][ -  + ]:        336 :     HMAP_FOR_EACH_WITH_HASH (rule, hmap_node, hash, &ib->rules) {
     256         [ +  - ]:        329 :         if (match_equal(&rule->match, match)) {
     257                 :        329 :             rule->op = ADD;
     258                 :        329 :             return;
     259                 :            :         }
     260                 :            :     }
     261                 :            : 
     262                 :          7 :     rule = xmalloc(sizeof *rule);
     263                 :          7 :     rule->match = *match;
     264                 :          7 :     rule->priority = priority;
     265                 :          7 :     rule->op = ADD;
     266                 :          7 :     hmap_insert(&ib->rules, &rule->hmap_node, hash);
     267                 :            : }
     268                 :            : 
     269                 :            : static void
     270                 :         48 : update_rules(struct in_band *ib)
     271                 :            : {
     272                 :            :     struct in_band_rule *ib_rule;
     273                 :            :     struct in_band_remote *r;
     274                 :            :     struct match match;
     275                 :            : 
     276                 :            :     /* Mark all the existing rules for deletion.  (Afterward we will re-add any
     277                 :            :      * rules that are still valid.) */
     278 [ +  + ][ -  + ]:        377 :     HMAP_FOR_EACH (ib_rule, hmap_node, &ib->rules) {
     279                 :        329 :         ib_rule->op = DEL;
     280                 :            :     }
     281                 :            : 
     282 [ +  - ][ +  - ]:         48 :     if (ib->n_remotes && !eth_addr_is_zero(ib->local_mac)) {
     283                 :            :         /* (a) Allow DHCP requests sent from the local port. */
     284                 :         48 :         match_init_catchall(&match);
     285                 :         48 :         match_set_in_port(&match, OFPP_LOCAL);
     286                 :         48 :         match_set_dl_type(&match, htons(ETH_TYPE_IP));
     287                 :         48 :         match_set_dl_src(&match, ib->local_mac);
     288                 :         48 :         match_set_nw_proto(&match, IPPROTO_UDP);
     289                 :         48 :         match_set_tp_src(&match, htons(DHCP_CLIENT_PORT));
     290                 :         48 :         match_set_tp_dst(&match, htons(DHCP_SERVER_PORT));
     291                 :         48 :         add_rule(ib, &match, IBR_FROM_LOCAL_DHCP);
     292                 :            : 
     293                 :            :         /* (b) Allow ARP replies to the local port's MAC address. */
     294                 :         48 :         match_init_catchall(&match);
     295                 :         48 :         match_set_dl_type(&match, htons(ETH_TYPE_ARP));
     296                 :         48 :         match_set_dl_dst(&match, ib->local_mac);
     297                 :         48 :         match_set_nw_proto(&match, ARP_OP_REPLY);
     298                 :         48 :         add_rule(ib, &match, IBR_TO_LOCAL_ARP);
     299                 :            : 
     300                 :            :         /* (c) Allow ARP requests from the local port's MAC address.  */
     301                 :         48 :         match_init_catchall(&match);
     302                 :         48 :         match_set_dl_type(&match, htons(ETH_TYPE_ARP));
     303                 :         48 :         match_set_dl_src(&match, ib->local_mac);
     304                 :         48 :         match_set_nw_proto(&match, ARP_OP_REQUEST);
     305                 :         48 :         add_rule(ib, &match, IBR_FROM_LOCAL_ARP);
     306                 :            :     }
     307                 :            : 
     308         [ +  + ]:         96 :     for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
     309         [ +  - ]:         48 :         if (eth_addr_is_zero(r->remote_mac)) {
     310                 :         48 :             continue;
     311                 :            :         }
     312                 :            : 
     313                 :            :         /* (d) Allow ARP replies to the next hop's MAC address. */
     314                 :          0 :         match_init_catchall(&match);
     315                 :          0 :         match_set_dl_type(&match, htons(ETH_TYPE_ARP));
     316                 :          0 :         match_set_dl_dst(&match, r->remote_mac);
     317                 :          0 :         match_set_nw_proto(&match, ARP_OP_REPLY);
     318                 :          0 :         add_rule(ib, &match, IBR_TO_NEXT_HOP_ARP);
     319                 :            : 
     320                 :            :         /* (e) Allow ARP requests from the next hop's MAC address. */
     321                 :          0 :         match_init_catchall(&match);
     322                 :          0 :         match_set_dl_type(&match, htons(ETH_TYPE_ARP));
     323                 :          0 :         match_set_dl_src(&match, r->remote_mac);
     324                 :          0 :         match_set_nw_proto(&match, ARP_OP_REQUEST);
     325                 :          0 :         add_rule(ib, &match, IBR_FROM_NEXT_HOP_ARP);
     326                 :            :     }
     327                 :            : 
     328         [ +  + ]:         96 :     for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
     329                 :         48 :         const struct sockaddr_in *a = &r->remote_addr;
     330                 :            : 
     331                 :            :         /* (f) Allow ARP replies containing the remote's IP address as a
     332                 :            :          * target. */
     333                 :         48 :         match_init_catchall(&match);
     334                 :         48 :         match_set_dl_type(&match, htons(ETH_TYPE_ARP));
     335                 :         48 :         match_set_nw_proto(&match, ARP_OP_REPLY);
     336                 :         48 :         match_set_nw_dst(&match, a->sin_addr.s_addr);
     337                 :         48 :         add_rule(ib, &match, IBR_TO_REMOTE_ARP);
     338                 :            : 
     339                 :            :         /* (g) Allow ARP requests containing the remote's IP address as a
     340                 :            :          * source. */
     341                 :         48 :         match_init_catchall(&match);
     342                 :         48 :         match_set_dl_type(&match, htons(ETH_TYPE_ARP));
     343                 :         48 :         match_set_nw_proto(&match, ARP_OP_REQUEST);
     344                 :         48 :         match_set_nw_src(&match, a->sin_addr.s_addr);
     345                 :         48 :         add_rule(ib, &match, IBR_FROM_REMOTE_ARP);
     346                 :            : 
     347                 :            :         /* (h) Allow TCP traffic to the remote's IP and port. */
     348                 :         48 :         match_init_catchall(&match);
     349                 :         48 :         match_set_dl_type(&match, htons(ETH_TYPE_IP));
     350                 :         48 :         match_set_nw_proto(&match, IPPROTO_TCP);
     351                 :         48 :         match_set_nw_dst(&match, a->sin_addr.s_addr);
     352                 :         48 :         match_set_tp_dst(&match, a->sin_port);
     353                 :         48 :         add_rule(ib, &match, IBR_TO_REMOTE_TCP);
     354                 :            : 
     355                 :            :         /* (i) Allow TCP traffic from the remote's IP and port. */
     356                 :         48 :         match_init_catchall(&match);
     357                 :         48 :         match_set_dl_type(&match, htons(ETH_TYPE_IP));
     358                 :         48 :         match_set_nw_proto(&match, IPPROTO_TCP);
     359                 :         48 :         match_set_nw_src(&match, a->sin_addr.s_addr);
     360                 :         48 :         match_set_tp_src(&match, a->sin_port);
     361                 :         48 :         add_rule(ib, &match, IBR_FROM_REMOTE_TCP);
     362                 :            :     }
     363                 :         48 : }
     364                 :            : 
     365                 :            : /* Updates the OpenFlow flow table for the current state of in-band control.
     366                 :            :  * Returns true ordinarily.  Returns false if no remotes are configured on 'ib'
     367                 :            :  * and 'ib' doesn't have any rules left to remove from the OpenFlow flow
     368                 :            :  * table.  Thus, a false return value means that the caller can destroy 'ib'
     369                 :            :  * without leaving extra flows hanging around in the flow table. */
     370                 :            : bool
     371                 :         48 : in_band_run(struct in_band *ib)
     372                 :            : {
     373                 :            :     uint64_t ofpacts_stub[128 / 8];
     374                 :            :     struct ofpbuf ofpacts;
     375                 :            : 
     376                 :            :     struct in_band_rule *rule, *next;
     377                 :            : 
     378                 :         48 :     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
     379                 :            : 
     380         [ -  + ]:         48 :     if (ib->queue_id >= 0) {
     381                 :          0 :         ofpact_put_SET_QUEUE(&ofpacts)->queue_id = ib->queue_id;
     382                 :            :     }
     383                 :         48 :     ofpact_put_OUTPUT(&ofpacts)->port = OFPP_NORMAL;
     384                 :            : 
     385                 :         48 :     refresh_local(ib);
     386                 :         48 :     refresh_remotes(ib);
     387                 :            : 
     388                 :         48 :     update_rules(ib);
     389                 :            : 
     390 [ +  + ][ -  + ]:        384 :     HMAP_FOR_EACH_SAFE (rule, next, hmap_node, &ib->rules) {
                 [ +  + ]
     391      [ +  -  - ]:        336 :         switch (rule->op) {
     392                 :            :         case ADD:
     393                 :        336 :             ofproto_add_flow(ib->ofproto, &rule->match, rule->priority,
     394                 :        672 :                              ofpacts.data, ofpacts.size);
     395                 :        336 :             break;
     396                 :            : 
     397                 :            :         case DEL:
     398                 :          0 :             ofproto_delete_flow(ib->ofproto, &rule->match, rule->priority);
     399                 :          0 :             hmap_remove(&ib->rules, &rule->hmap_node);
     400                 :          0 :             free(rule);
     401                 :          0 :             break;
     402                 :            :         }
     403                 :            :     }
     404                 :            : 
     405                 :         48 :     ofpbuf_uninit(&ofpacts);
     406                 :            : 
     407 [ -  + ][ #  # ]:         48 :     return ib->n_remotes || !hmap_is_empty(&ib->rules);
     408                 :            : }
     409                 :            : 
     410                 :            : void
     411                 :         48 : in_band_wait(struct in_band *in_band)
     412                 :            : {
     413                 :         48 :     long long int wakeup
     414                 :         48 :             = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
     415                 :         48 :     poll_timer_wait_until(wakeup * 1000);
     416                 :         48 : }
     417                 :            : 
     418                 :            : int
     419                 :          1 : in_band_create(struct ofproto *ofproto, const char *local_name,
     420                 :            :                struct in_band **in_bandp)
     421                 :            : {
     422                 :            :     struct in_band *in_band;
     423                 :            :     struct netdev *local_netdev;
     424                 :            :     int error;
     425                 :          1 :     const char *type = ofproto_port_open_type(ofproto->type, "internal");
     426                 :            : 
     427                 :          1 :     *in_bandp = NULL;
     428                 :          1 :     error = netdev_open(local_name, type, &local_netdev);
     429         [ -  + ]:          1 :     if (error) {
     430         [ #  # ]:          0 :         VLOG_ERR("%s: failed to initialize in-band control: cannot open "
     431                 :            :                  "datapath local port %s (%s)", ofproto->name,
     432                 :            :                  local_name, ovs_strerror(error));
     433                 :          0 :         return error;
     434                 :            :     }
     435                 :            : 
     436                 :          1 :     in_band = xzalloc(sizeof *in_band);
     437                 :          1 :     in_band->ofproto = ofproto;
     438                 :          1 :     in_band->queue_id = -1;
     439                 :          1 :     in_band->next_remote_refresh = TIME_MIN;
     440                 :          1 :     in_band->next_local_refresh = TIME_MIN;
     441                 :          1 :     in_band->local_netdev = local_netdev;
     442                 :          1 :     hmap_init(&in_band->rules);
     443                 :            : 
     444                 :          1 :     *in_bandp = in_band;
     445                 :            : 
     446                 :          1 :     return 0;
     447                 :            : }
     448                 :            : 
     449                 :            : void
     450                 :        749 : in_band_destroy(struct in_band *ib)
     451                 :            : {
     452         [ +  + ]:        749 :     if (ib) {
     453                 :            :         struct in_band_rule *rule;
     454                 :            : 
     455 [ +  + ][ -  + ]:          8 :         HMAP_FOR_EACH_POP (rule, hmap_node, &ib->rules) {
                 [ +  + ]
     456                 :          7 :             free(rule);
     457                 :            :         }
     458                 :          1 :         hmap_destroy(&ib->rules);
     459                 :          1 :         in_band_set_remotes(ib, NULL, 0);
     460                 :          1 :         netdev_close(ib->local_netdev);
     461                 :          1 :         free(ib);
     462                 :            :     }
     463                 :        749 : }
     464                 :            : 
     465                 :            : static bool
     466                 :          2 : any_addresses_changed(struct in_band *ib,
     467                 :            :                       const struct sockaddr_in *addresses, size_t n)
     468                 :            : {
     469                 :            :     size_t i;
     470                 :            : 
     471         [ +  - ]:          2 :     if (n != ib->n_remotes) {
     472                 :          2 :         return true;
     473                 :            :     }
     474                 :            : 
     475         [ #  # ]:          0 :     for (i = 0; i < n; i++) {
     476                 :          0 :         const struct sockaddr_in *old = &ib->remotes[i].remote_addr;
     477                 :          0 :         const struct sockaddr_in *new = &addresses[i];
     478                 :            : 
     479 [ #  # ][ #  # ]:          0 :         if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
     480                 :          0 :             old->sin_port != new->sin_port) {
     481                 :          0 :             return true;
     482                 :            :         }
     483                 :            :     }
     484                 :            : 
     485                 :          0 :     return false;
     486                 :            : }
     487                 :            : 
     488                 :            : void
     489                 :          2 : in_band_set_remotes(struct in_band *ib,
     490                 :            :                     const struct sockaddr_in *addresses, size_t n)
     491                 :            : {
     492                 :            :     size_t i;
     493                 :            : 
     494         [ -  + ]:          2 :     if (!any_addresses_changed(ib, addresses, n)) {
     495                 :          0 :         return;
     496                 :            :     }
     497                 :            : 
     498                 :            :     /* Clear old remotes. */
     499         [ +  + ]:          3 :     for (i = 0; i < ib->n_remotes; i++) {
     500                 :          1 :         netdev_close(ib->remotes[i].remote_netdev);
     501                 :            :     }
     502                 :          2 :     free(ib->remotes);
     503                 :            : 
     504                 :            :     /* Set up new remotes. */
     505         [ +  + ]:          2 :     ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL;
     506                 :          2 :     ib->n_remotes = n;
     507         [ +  + ]:          3 :     for (i = 0; i < n; i++) {
     508                 :          1 :         ib->remotes[i].remote_addr = addresses[i];
     509                 :            :     }
     510                 :            : 
     511                 :            :     /* Force refresh in next call to in_band_run(). */
     512                 :          2 :     ib->next_remote_refresh = TIME_MIN;
     513                 :            : }
     514                 :            : 
     515                 :            : /* Sets the OpenFlow queue used by flows set up by 'ib' to 'queue_id'.  If
     516                 :            :  * 'queue_id' is negative, 'ib' will not set any queue (which is also the
     517                 :            :  * default). */
     518                 :            : void
     519                 :          1 : in_band_set_queue(struct in_band *ib, int queue_id)
     520                 :            : {
     521                 :          1 :     ib->queue_id = queue_id;
     522                 :          1 : }
     523                 :            : 

Generated by: LCOV version 1.12