netdev: Globally track port status changes
[cascardo/ovs.git] / ofproto / tunnel.c
1 /* Copyright (c) 2013 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 #include <config.h>
16 #include "tunnel.h"
17
18 #include <errno.h>
19
20 #include "byte-order.h"
21 #include "connectivity.h"
22 #include "dynamic-string.h"
23 #include "hash.h"
24 #include "hmap.h"
25 #include "netdev.h"
26 #include "odp-util.h"
27 #include "packets.h"
28 #include "seq.h"
29 #include "smap.h"
30 #include "socket-util.h"
31 #include "tunnel.h"
32 #include "vlog.h"
33
34 VLOG_DEFINE_THIS_MODULE(tunnel);
35
36 /* skb mark used for IPsec tunnel packets */
37 #define IPSEC_MARK 1
38
39 struct tnl_match {
40     ovs_be64 in_key;
41     ovs_be32 ip_src;
42     ovs_be32 ip_dst;
43     odp_port_t odp_port;
44     uint32_t pkt_mark;
45     bool in_key_flow;
46     bool ip_src_flow;
47     bool ip_dst_flow;
48 };
49
50 struct tnl_port {
51     struct hmap_node ofport_node;
52     struct hmap_node match_node;
53
54     const struct ofport_dpif *ofport;
55     unsigned int change_seq;
56     struct netdev *netdev;
57
58     struct tnl_match match;
59 };
60
61 static struct ovs_rwlock rwlock = OVS_RWLOCK_INITIALIZER;
62
63 static struct hmap tnl_match_map__ = HMAP_INITIALIZER(&tnl_match_map__);
64 static struct hmap *tnl_match_map OVS_GUARDED_BY(rwlock) = &tnl_match_map__;
65
66 static struct hmap ofport_map__ = HMAP_INITIALIZER(&ofport_map__);
67 static struct hmap *ofport_map OVS_GUARDED_BY(rwlock) = &ofport_map__;
68
69 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
70 static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(60, 60);
71
72 static struct tnl_port *tnl_find(const struct flow *) OVS_REQ_RDLOCK(rwlock);
73 static struct tnl_port *tnl_find_exact(struct tnl_match *)
74     OVS_REQ_RDLOCK(rwlock);
75 static struct tnl_port *tnl_find_ofport(const struct ofport_dpif *)
76     OVS_REQ_RDLOCK(rwlock);
77
78 static uint32_t tnl_hash(struct tnl_match *);
79 static void tnl_match_fmt(const struct tnl_match *, struct ds *);
80 static char *tnl_port_fmt(const struct tnl_port *) OVS_REQ_RDLOCK(rwlock);
81 static void tnl_port_mod_log(const struct tnl_port *, const char *action)
82     OVS_REQ_RDLOCK(rwlock);
83 static const char *tnl_port_get_name(const struct tnl_port *)
84     OVS_REQ_RDLOCK(rwlock);
85 static void tnl_port_del__(const struct ofport_dpif *) OVS_REQ_WRLOCK(rwlock);
86
87 static bool
88 tnl_port_add__(const struct ofport_dpif *ofport, const struct netdev *netdev,
89                odp_port_t odp_port, bool warn)
90     OVS_REQ_WRLOCK(rwlock)
91 {
92     const struct netdev_tunnel_config *cfg;
93     struct tnl_port *existing_port;
94     struct tnl_port *tnl_port;
95
96     cfg = netdev_get_tunnel_config(netdev);
97     ovs_assert(cfg);
98
99     tnl_port = xzalloc(sizeof *tnl_port);
100     tnl_port->ofport = ofport;
101     tnl_port->netdev = netdev_ref(netdev);
102     tnl_port->change_seq = seq_read(connectivity_seq_get());
103
104     tnl_port->match.in_key = cfg->in_key;
105     tnl_port->match.ip_src = cfg->ip_src;
106     tnl_port->match.ip_dst = cfg->ip_dst;
107     tnl_port->match.ip_src_flow = cfg->ip_src_flow;
108     tnl_port->match.ip_dst_flow = cfg->ip_dst_flow;
109     tnl_port->match.pkt_mark = cfg->ipsec ? IPSEC_MARK : 0;
110     tnl_port->match.in_key_flow = cfg->in_key_flow;
111     tnl_port->match.odp_port = odp_port;
112
113     existing_port = tnl_find_exact(&tnl_port->match);
114     if (existing_port) {
115         if (warn) {
116             struct ds ds = DS_EMPTY_INITIALIZER;
117             tnl_match_fmt(&tnl_port->match, &ds);
118             VLOG_WARN("%s: attempting to add tunnel port with same config as "
119                       "port '%s' (%s)", tnl_port_get_name(tnl_port),
120                       tnl_port_get_name(existing_port), ds_cstr(&ds));
121             ds_destroy(&ds);
122             free(tnl_port);
123         }
124         return false;
125     }
126
127     hmap_insert(ofport_map, &tnl_port->ofport_node, hash_pointer(ofport, 0));
128     hmap_insert(tnl_match_map, &tnl_port->match_node,
129                 tnl_hash(&tnl_port->match));
130     tnl_port_mod_log(tnl_port, "adding");
131     return true;
132 }
133
134 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
135  * must be added before they can be used by the module. 'ofport' must be a
136  * tunnel. */
137 void
138 tnl_port_add(const struct ofport_dpif *ofport, const struct netdev *netdev,
139              odp_port_t odp_port) OVS_EXCLUDED(rwlock)
140 {
141     ovs_rwlock_wrlock(&rwlock);
142     tnl_port_add__(ofport, netdev, odp_port, true);
143     ovs_rwlock_unlock(&rwlock);
144 }
145
146 /* Checks if the tunnel represented by 'ofport' reconfiguration due to changes
147  * in its netdev_tunnel_config.  If it does, returns true. Otherwise, returns
148  * false.  'ofport' and 'odp_port' should be the same as would be passed to
149  * tnl_port_add(). */
150 bool
151 tnl_port_reconfigure(const struct ofport_dpif *ofport,
152                      const struct netdev *netdev, odp_port_t odp_port)
153     OVS_EXCLUDED(rwlock)
154 {
155     struct tnl_port *tnl_port;
156     bool changed = false;
157
158     ovs_rwlock_wrlock(&rwlock);
159     tnl_port = tnl_find_ofport(ofport);
160     if (!tnl_port) {
161         changed = tnl_port_add__(ofport, netdev, odp_port, false);
162     } else if (tnl_port->netdev != netdev
163                || tnl_port->match.odp_port != odp_port
164                || tnl_port->change_seq != seq_read(connectivity_seq_get())) {
165         VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
166         tnl_port_del__(ofport);
167         tnl_port_add__(ofport, netdev, odp_port, true);
168         changed = true;
169     }
170     ovs_rwlock_unlock(&rwlock);
171     return changed;
172 }
173
174 static void
175 tnl_port_del__(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(rwlock)
176 {
177     struct tnl_port *tnl_port;
178
179     if (!ofport) {
180         return;
181     }
182
183     tnl_port = tnl_find_ofport(ofport);
184     if (tnl_port) {
185         tnl_port_mod_log(tnl_port, "removing");
186         hmap_remove(tnl_match_map, &tnl_port->match_node);
187         hmap_remove(ofport_map, &tnl_port->ofport_node);
188         netdev_close(tnl_port->netdev);
189         free(tnl_port);
190     }
191 }
192
193 /* Removes 'ofport' from the module. */
194 void
195 tnl_port_del(const struct ofport_dpif *ofport) OVS_EXCLUDED(rwlock)
196 {
197     ovs_rwlock_wrlock(&rwlock);
198     tnl_port_del__(ofport);
199     ovs_rwlock_unlock(&rwlock);
200 }
201
202 /* Looks in the table of tunnels for a tunnel matching the metadata in 'flow'.
203  * Returns the 'ofport' corresponding to the new in_port, or a null pointer if
204  * none is found.
205  *
206  * Callers should verify that 'flow' needs to be received by calling
207  * tnl_port_should_receive() before this function. */
208 const struct ofport_dpif *
209 tnl_port_receive(const struct flow *flow) OVS_EXCLUDED(rwlock)
210 {
211     char *pre_flow_str = NULL;
212     const struct ofport_dpif *ofport;
213     struct tnl_port *tnl_port;
214
215     ovs_rwlock_rdlock(&rwlock);
216     tnl_port = tnl_find(flow);
217     ofport = tnl_port ? tnl_port->ofport : NULL;
218     if (!tnl_port) {
219         char *flow_str = flow_to_string(flow);
220
221         VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", flow_str);
222         free(flow_str);
223         goto out;
224     }
225
226     if (!VLOG_DROP_DBG(&dbg_rl)) {
227         pre_flow_str = flow_to_string(flow);
228     }
229
230     if (pre_flow_str) {
231         char *post_flow_str = flow_to_string(flow);
232         char *tnl_str = tnl_port_fmt(tnl_port);
233         VLOG_DBG("flow received\n"
234                  "%s"
235                  " pre: %s\n"
236                  "post: %s",
237                  tnl_str, pre_flow_str, post_flow_str);
238         free(tnl_str);
239         free(pre_flow_str);
240         free(post_flow_str);
241     }
242
243 out:
244     ovs_rwlock_unlock(&rwlock);
245     return ofport;
246 }
247
248 static bool
249 tnl_ecn_ok(const struct flow *base_flow, struct flow *flow)
250 {
251     if (is_ip_any(base_flow)
252         && (flow->tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
253         if ((base_flow->nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
254             VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
255                          " but is not ECN capable");
256             return false;
257         } else {
258             /* Set the ECN CE value in the tunneled packet. */
259             flow->nw_tos |= IP_ECN_CE;
260         }
261     }
262
263     return true;
264 }
265
266 /* Should be called at the beginning of action translation to initialize
267  * wildcards and perform any actions based on receiving on tunnel port.
268  *
269  * Returns false if the packet must be dropped. */
270 bool
271 tnl_xlate_init(const struct flow *base_flow, struct flow *flow,
272                struct flow_wildcards *wc)
273 {
274     if (tnl_port_should_receive(flow)) {
275         memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
276         memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
277
278         if (!tnl_ecn_ok(base_flow, flow)) {
279             return false;
280         }
281
282         flow->pkt_mark &= ~IPSEC_MARK;
283     }
284
285     return true;
286 }
287
288 /* Given that 'flow' should be output to the ofport corresponding to
289  * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
290  * port that the output should happen on.  May return ODPP_NONE if the output
291  * shouldn't occur. */
292 odp_port_t
293 tnl_port_send(const struct ofport_dpif *ofport, struct flow *flow,
294               struct flow_wildcards *wc) OVS_EXCLUDED(rwlock)
295 {
296     const struct netdev_tunnel_config *cfg;
297     struct tnl_port *tnl_port;
298     char *pre_flow_str = NULL;
299     odp_port_t out_port;
300
301     ovs_rwlock_rdlock(&rwlock);
302     tnl_port = tnl_find_ofport(ofport);
303     out_port = tnl_port ? tnl_port->match.odp_port : ODPP_NONE;
304     if (!tnl_port) {
305         goto out;
306     }
307
308     cfg = netdev_get_tunnel_config(tnl_port->netdev);
309     ovs_assert(cfg);
310
311     if (!VLOG_DROP_DBG(&dbg_rl)) {
312         pre_flow_str = flow_to_string(flow);
313     }
314
315     if (!cfg->ip_src_flow) {
316         flow->tunnel.ip_src = tnl_port->match.ip_src;
317     }
318     if (!cfg->ip_dst_flow) {
319         flow->tunnel.ip_dst = tnl_port->match.ip_dst;
320     }
321     flow->pkt_mark = tnl_port->match.pkt_mark;
322
323     if (!cfg->out_key_flow) {
324         flow->tunnel.tun_id = cfg->out_key;
325     }
326
327     if (cfg->ttl_inherit && is_ip_any(flow)) {
328         wc->masks.nw_ttl = 0xff;
329         flow->tunnel.ip_ttl = flow->nw_ttl;
330     } else {
331         flow->tunnel.ip_ttl = cfg->ttl;
332     }
333
334     if (cfg->tos_inherit && is_ip_any(flow)) {
335         wc->masks.nw_tos = 0xff;
336         flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
337     } else {
338         flow->tunnel.ip_tos = cfg->tos;
339     }
340
341     /* ECN fields are always inherited. */
342     if (is_ip_any(flow)) {
343         wc->masks.nw_tos |= IP_ECN_MASK;
344     }
345
346     if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
347         flow->tunnel.ip_tos |= IP_ECN_ECT_0;
348     } else {
349         flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
350     }
351
352     flow->tunnel.flags = (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
353         | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
354         | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
355
356     if (pre_flow_str) {
357         char *post_flow_str = flow_to_string(flow);
358         char *tnl_str = tnl_port_fmt(tnl_port);
359         VLOG_DBG("flow sent\n"
360                  "%s"
361                  " pre: %s\n"
362                  "post: %s",
363                  tnl_str, pre_flow_str, post_flow_str);
364         free(tnl_str);
365         free(pre_flow_str);
366         free(post_flow_str);
367     }
368
369 out:
370     ovs_rwlock_unlock(&rwlock);
371     return out_port;
372 }
373
374 static uint32_t
375 tnl_hash(struct tnl_match *match)
376 {
377     BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
378     return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
379 }
380
381 static struct tnl_port *
382 tnl_find_ofport(const struct ofport_dpif *ofport) OVS_REQ_RDLOCK(rwlock)
383 {
384     struct tnl_port *tnl_port;
385
386     HMAP_FOR_EACH_IN_BUCKET (tnl_port, ofport_node, hash_pointer(ofport, 0),
387                              ofport_map) {
388         if (tnl_port->ofport == ofport) {
389             return tnl_port;
390         }
391     }
392     return NULL;
393 }
394
395 static struct tnl_port *
396 tnl_find_exact(struct tnl_match *match) OVS_REQ_RDLOCK(rwlock)
397 {
398     struct tnl_port *tnl_port;
399
400     HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match),
401                              tnl_match_map) {
402         if (!memcmp(match, &tnl_port->match, sizeof *match)) {
403             return tnl_port;
404         }
405     }
406     return NULL;
407 }
408
409 /* Returns the tnl_port that is the best match for the tunnel data in 'flow',
410  * or NULL if no tnl_port matches 'flow'. */
411 static struct tnl_port *
412 tnl_find(const struct flow *flow) OVS_REQ_RDLOCK(rwlock)
413 {
414     enum ip_src_type {
415         IP_SRC_CFG,             /* ip_src must equal configured address. */
416         IP_SRC_ANY,             /* Any ip_src is acceptable. */
417         IP_SRC_FLOW             /* ip_src is handled in flow table. */
418     };
419
420     struct tnl_match_pattern {
421         bool in_key_flow;
422         bool ip_dst_flow;
423         enum ip_src_type ip_src;
424     };
425
426     static const struct tnl_match_pattern patterns[] = {
427         { false, false, IP_SRC_CFG },  /* remote_ip, local_ip, in_key. */
428         { false, false, IP_SRC_ANY },  /* remote_ip, in_key. */
429         { true,  false, IP_SRC_CFG },  /* remote_ip, local_ip. */
430         { true,  false, IP_SRC_ANY },  /* remote_ip. */
431         { true,  true,  IP_SRC_ANY },  /* Flow-based remote. */
432         { true,  true,  IP_SRC_FLOW }, /* Flow-based everything. */
433     };
434
435     const struct tnl_match_pattern *p;
436     struct tnl_match match;
437
438     memset(&match, 0, sizeof match);
439     match.odp_port = flow->in_port.odp_port;
440     match.pkt_mark = flow->pkt_mark;
441
442     for (p = patterns; p < &patterns[ARRAY_SIZE(patterns)]; p++) {
443         struct tnl_port *tnl_port;
444
445         match.in_key_flow = p->in_key_flow;
446         match.in_key = p->in_key_flow ? 0 : flow->tunnel.tun_id;
447
448         match.ip_dst_flow = p->ip_dst_flow;
449         match.ip_dst = p->ip_dst_flow ? 0 : flow->tunnel.ip_src;
450
451         match.ip_src_flow = p->ip_src == IP_SRC_FLOW;
452         match.ip_src = p->ip_src == IP_SRC_CFG ? flow->tunnel.ip_dst : 0;
453
454         tnl_port = tnl_find_exact(&match);
455         if (tnl_port) {
456             return tnl_port;
457         }
458     }
459
460     return NULL;
461 }
462
463 static void
464 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
465     OVS_REQ_RDLOCK(rwlock)
466 {
467     if (!match->ip_dst_flow) {
468         ds_put_format(ds, IP_FMT"->"IP_FMT, IP_ARGS(match->ip_src),
469                       IP_ARGS(match->ip_dst));
470     } else if (!match->ip_src_flow) {
471         ds_put_format(ds, IP_FMT"->flow", IP_ARGS(match->ip_src));
472     } else {
473         ds_put_cstr(ds, "flow->flow");
474     }
475
476     if (match->in_key_flow) {
477         ds_put_cstr(ds, ", key=flow");
478     } else {
479         ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
480     }
481
482     ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
483     ds_put_format(ds, ", pkt mark=%"PRIu32, match->pkt_mark);
484 }
485
486 static void
487 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
488     OVS_REQ_RDLOCK(rwlock)
489 {
490     if (VLOG_IS_DBG_ENABLED()) {
491         struct ds ds = DS_EMPTY_INITIALIZER;
492
493         tnl_match_fmt(&tnl_port->match, &ds);
494         VLOG_INFO("%s tunnel port %s (%s)", action,
495                   tnl_port_get_name(tnl_port), ds_cstr(&ds));
496         ds_destroy(&ds);
497     }
498 }
499
500 static char *
501 tnl_port_fmt(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
502 {
503     const struct netdev_tunnel_config *cfg =
504         netdev_get_tunnel_config(tnl_port->netdev);
505     struct ds ds = DS_EMPTY_INITIALIZER;
506
507     ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
508                   tnl_port_get_name(tnl_port),
509                   netdev_get_type(tnl_port->netdev));
510     tnl_match_fmt(&tnl_port->match, &ds);
511
512     if (cfg->out_key != cfg->in_key ||
513         cfg->out_key_present != cfg->in_key_present ||
514         cfg->out_key_flow != cfg->in_key_flow) {
515         ds_put_cstr(&ds, ", out_key=");
516         if (!cfg->out_key_present) {
517             ds_put_cstr(&ds, "none");
518         } else if (cfg->out_key_flow) {
519             ds_put_cstr(&ds, "flow");
520         } else {
521             ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
522         }
523     }
524
525     if (cfg->ttl_inherit) {
526         ds_put_cstr(&ds, ", ttl=inherit");
527     } else {
528         ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
529     }
530
531     if (cfg->tos_inherit) {
532         ds_put_cstr(&ds, ", tos=inherit");
533     } else if (cfg->tos) {
534         ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
535     }
536
537     if (!cfg->dont_fragment) {
538         ds_put_cstr(&ds, ", df=false");
539     }
540
541     if (cfg->csum) {
542         ds_put_cstr(&ds, ", csum=true");
543     }
544
545     ds_put_cstr(&ds, ")\n");
546
547     return ds_steal_cstr(&ds);
548 }
549
550 static const char *
551 tnl_port_get_name(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
552 {
553     return netdev_get_name(tnl_port->netdev);
554 }