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