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