tunnel: Treat in_key=0 the same as a missing in_key.
[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 "ofproto/ofproto-provider.h"
21 #include "byte-order.h"
22 #include "dynamic-string.h"
23 #include "hash.h"
24 #include "hmap.h"
25 #include "netdev-vport.h"
26 #include "odp-util.h"
27 #include "packets.h"
28 #include "smap.h"
29 #include "socket-util.h"
30 #include "tunnel.h"
31 #include "vlog.h"
32
33 /* XXX:
34  *
35  * Ability to generate actions on input for ECN
36  * Ability to generate metadata for packet-outs
37  * IPsec using skb mark.
38  * VXLAN.
39  * Multicast group management (possibly).
40  * Disallow netdevs with names like "gre64_system" to prevent collisions. */
41
42 VLOG_DEFINE_THIS_MODULE(tunnel);
43
44 struct tnl_match {
45     ovs_be64 in_key;
46     ovs_be32 ip_src;
47     ovs_be32 ip_dst;
48     uint32_t odp_port;
49     bool in_key_flow;
50 };
51
52 struct tnl_port {
53     struct hmap_node match_node;
54
55     const struct ofport *ofport;
56     unsigned int netdev_seq;
57     struct tnl_match match;
58 };
59
60 static struct hmap tnl_match_map = HMAP_INITIALIZER(&tnl_match_map);
61
62 /* Returned to callers when their ofport will never be used to receive or send
63  * tunnel traffic. Alternatively, we could ask the caller to delete their
64  * ofport, but this would be unclean in the reconfguration case.  For the first
65  * time, an ofproto provider would have to call ofproto_port_del() on itself.*/
66 static struct tnl_port void_tnl_port;
67
68 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
69 static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(60, 60);
70
71 static struct tnl_port *tnl_find(struct tnl_match *);
72 static struct tnl_port *tnl_find_exact(struct tnl_match *);
73 static uint32_t tnl_hash(struct tnl_match *);
74 static void tnl_match_fmt(const struct tnl_match *, struct ds *);
75 static char *tnl_port_fmt(const struct tnl_port *);
76 static void tnl_port_mod_log(const struct tnl_port *, const char *action);
77 static const char *tnl_port_get_name(const struct tnl_port *);
78
79 static struct tnl_port *
80 tnl_port_add__(const struct ofport *ofport, uint32_t odp_port,
81                bool warn)
82 {
83     const struct netdev_tunnel_config *cfg;
84     struct tnl_port *existing_port;
85     struct tnl_port *tnl_port;
86
87     cfg = netdev_get_tunnel_config(ofport->netdev);
88     ovs_assert(cfg);
89
90     tnl_port = xzalloc(sizeof *tnl_port);
91     tnl_port->ofport = ofport;
92     tnl_port->netdev_seq = netdev_change_seq(tnl_port->ofport->netdev);
93
94     tnl_port->match.in_key = cfg->in_key;
95     tnl_port->match.ip_src = cfg->ip_src;
96     tnl_port->match.ip_dst = cfg->ip_dst;
97     tnl_port->match.in_key_flow = cfg->in_key_flow;
98     tnl_port->match.odp_port = odp_port;
99
100     existing_port = tnl_find_exact(&tnl_port->match);
101     if (existing_port) {
102         if (warn) {
103             struct ds ds = DS_EMPTY_INITIALIZER;
104             tnl_match_fmt(&tnl_port->match, &ds);
105             VLOG_WARN("%s: attempting to add tunnel port with same config as "
106                       "port '%s' (%s)", tnl_port_get_name(tnl_port),
107                       tnl_port_get_name(existing_port), ds_cstr(&ds));
108             ds_destroy(&ds);
109             free(tnl_port);
110         }
111         return &void_tnl_port;
112     }
113
114     hmap_insert(&tnl_match_map, &tnl_port->match_node,
115                 tnl_hash(&tnl_port->match));
116     tnl_port_mod_log(tnl_port, "adding");
117     return tnl_port;
118 }
119
120 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
121  * must be added before they can be used by the module. 'ofport' must be a
122  * tunnel. */
123 struct tnl_port *
124 tnl_port_add(const struct ofport *ofport, uint32_t odp_port)
125 {
126     return tnl_port_add__(ofport, odp_port, true);
127 }
128
129 /* Checks if the tnl_port pointed to by 'tnl_portp' needs reconfiguration due
130  * to changes in its netdev_tunnel_config.  If it does, updates 'tnl_portp' to
131  * point to a new tnl_port and returns true.  Otherwise, returns false.
132  * 'ofport' and 'odp_port' should be the same as would be passed to
133  * tnl_port_add(). */
134 bool
135 tnl_port_reconfigure(const struct ofport *ofport, uint32_t odp_port,
136                      struct tnl_port **tnl_portp)
137 {
138     struct tnl_port *tnl_port = *tnl_portp;
139
140     if (tnl_port == &void_tnl_port) {
141         *tnl_portp = tnl_port_add__(ofport, odp_port, false);
142         return *tnl_portp != &void_tnl_port;
143     } else if (tnl_port->ofport != ofport
144                || tnl_port->match.odp_port != odp_port
145                || tnl_port->netdev_seq != netdev_change_seq(ofport->netdev)) {
146         VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
147         tnl_port_del(tnl_port);
148         *tnl_portp = tnl_port_add(ofport, odp_port);
149         return true;
150     }
151     return false;
152 }
153
154 /* Removes 'tnl_port' from the module. */
155 void
156 tnl_port_del(struct tnl_port *tnl_port)
157 {
158     if (tnl_port && tnl_port != &void_tnl_port) {
159         tnl_port_mod_log(tnl_port, "removing");
160         hmap_remove(&tnl_match_map, &tnl_port->match_node);
161         free(tnl_port);
162     }
163 }
164
165 /* Transforms 'flow' so that it appears to have been received by a tunnel
166  * OpenFlow port controlled by this module instead of the datapath port it
167  * actually came in on.  Sets 'flow''s in_port to the appropriate OpenFlow port
168  * number.  Returns the 'ofport' corresponding to the new in_port.
169  *
170  * Callers should verify that 'flow' needs to be received by calling
171  * tnl_port_should_receive() before this function.
172  *
173  * Leaves 'flow' untouched and returns null if unsuccessful. */
174 const struct ofport *
175 tnl_port_receive(struct flow *flow)
176 {
177     char *pre_flow_str = NULL;
178     struct tnl_port *tnl_port;
179     struct tnl_match match;
180
181     memset(&match, 0, sizeof match);
182     match.odp_port = flow->in_port;
183     match.ip_src = flow->tunnel.ip_dst;
184     match.ip_dst = flow->tunnel.ip_src;
185     match.in_key = flow->tunnel.tun_id;
186
187     tnl_port = tnl_find(&match);
188     if (!tnl_port) {
189         struct ds ds = DS_EMPTY_INITIALIZER;
190
191         tnl_match_fmt(&match, &ds);
192         VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", ds_cstr(&ds));
193         ds_destroy(&ds);
194         return NULL;
195     }
196
197     if (is_ip_any(flow)
198         && ((flow->tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE)
199         && (flow->nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
200         VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE but is not ECN"
201                      " capable");
202         return NULL;
203     }
204
205     if (!VLOG_DROP_DBG(&dbg_rl)) {
206         pre_flow_str = flow_to_string(flow);
207     }
208
209     flow->in_port = tnl_port->ofport->ofp_port;
210     memset(&flow->tunnel, 0, sizeof flow->tunnel);
211     flow->tunnel.tun_id = match.in_key;
212
213     if (pre_flow_str) {
214         char *post_flow_str = flow_to_string(flow);
215         char *tnl_str = tnl_port_fmt(tnl_port);
216         VLOG_DBG("flow received\n"
217                  "%s"
218                  " pre: %s\n"
219                  "post: %s",
220                  tnl_str, pre_flow_str, post_flow_str);
221         free(tnl_str);
222         free(pre_flow_str);
223         free(post_flow_str);
224     }
225     return tnl_port->ofport;
226 }
227
228 /* Given that 'flow' should be output to the ofport corresponding to
229  * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
230  * port that the output should happen on.  May return OVSP_NONE if the output
231  * shouldn't occur. */
232 uint32_t
233 tnl_port_send(const struct tnl_port *tnl_port, struct flow *flow)
234 {
235     const struct netdev_tunnel_config *cfg;
236     char *pre_flow_str = NULL;
237
238     if (tnl_port == &void_tnl_port) {
239         return OVSP_NONE;
240     }
241
242     cfg = netdev_get_tunnel_config(tnl_port->ofport->netdev);
243     ovs_assert(cfg);
244
245     if (!VLOG_DROP_DBG(&dbg_rl)) {
246         pre_flow_str = flow_to_string(flow);
247     }
248
249     flow->tunnel.ip_src = tnl_port->match.ip_src;
250     flow->tunnel.ip_dst = tnl_port->match.ip_dst;
251
252     if (!cfg->out_key_flow) {
253         flow->tunnel.tun_id = cfg->out_key;
254     }
255
256     if (cfg->ttl_inherit && is_ip_any(flow)) {
257         flow->tunnel.ip_ttl = flow->nw_ttl;
258     } else {
259         flow->tunnel.ip_ttl = cfg->ttl;
260     }
261
262     if (cfg->tos_inherit && is_ip_any(flow)) {
263         flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
264     } else {
265         flow->tunnel.ip_tos = cfg->tos;
266     }
267
268     if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
269         flow->tunnel.ip_tos |= IP_ECN_ECT_0;
270     } else {
271         flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
272     }
273
274     flow->tunnel.flags = (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
275         | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
276         | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
277
278     if (pre_flow_str) {
279         char *post_flow_str = flow_to_string(flow);
280         char *tnl_str = tnl_port_fmt(tnl_port);
281         VLOG_DBG("flow sent\n"
282                  "%s"
283                  " pre: %s\n"
284                  "post: %s",
285                  tnl_str, pre_flow_str, post_flow_str);
286         free(tnl_str);
287         free(pre_flow_str);
288         free(post_flow_str);
289     }
290
291     return tnl_port->match.odp_port;
292 }
293
294 static uint32_t
295 tnl_hash(struct tnl_match *match)
296 {
297     BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
298     return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
299 }
300
301 static struct tnl_port *
302 tnl_find_exact(struct tnl_match *match)
303 {
304     struct tnl_port *tnl_port;
305
306     HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match),
307                              &tnl_match_map) {
308         if (!memcmp(match, &tnl_port->match, sizeof *match)) {
309             return tnl_port;
310         }
311     }
312     return NULL;
313 }
314
315 static struct tnl_port *
316 tnl_find(struct tnl_match *match_)
317 {
318     struct tnl_match match = *match_;
319     bool is_multicast = ip_is_multicast(match.ip_src);
320     struct tnl_port *tnl_port;
321
322     /* remote_ip, local_ip, in_key */
323     if (!is_multicast) {
324         tnl_port = tnl_find_exact(&match);
325         if (tnl_port) {
326             return tnl_port;
327         }
328     }
329
330     /* remote_ip, in_key */
331     match.ip_src = 0;
332     tnl_port = tnl_find_exact(&match);
333     if (tnl_port) {
334         return tnl_port;
335     }
336     match.ip_src = match_->ip_src;
337
338     /* remote_ip, local_ip */
339     if (!is_multicast) {
340         match.in_key = 0;
341         match.in_key_flow = true;
342         tnl_port = tnl_find_exact(&match);
343         if (tnl_port) {
344             return tnl_port;
345         }
346         match.in_key = match_->in_key;
347         match.in_key_flow = false;
348     }
349
350     /* remote_ip */
351     match.ip_src = 0;
352     match.in_key = 0;
353     match.in_key_flow = true;
354     tnl_port = tnl_find_exact(&match);
355     if (tnl_port) {
356         return tnl_port;
357     }
358     match.ip_src = match_->ip_src;
359     match.in_key = match_->in_key;
360     match.in_key_flow = false;
361
362     if (is_multicast) {
363         match.ip_src = 0;
364         match.ip_dst = match_->ip_src;
365
366         /* multicast remote_ip, in_key */
367         tnl_port = tnl_find_exact(&match);
368         if (tnl_port) {
369             return tnl_port;
370         }
371
372         /* multicast remote_ip */
373         match.in_key = 0;
374         match.in_key_flow = true;
375         tnl_port = tnl_find_exact(&match);
376         if (tnl_port) {
377             return tnl_port;
378         }
379     }
380     return NULL;
381 }
382
383 static void
384 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
385 {
386     ds_put_format(ds, IP_FMT"->"IP_FMT, IP_ARGS(match->ip_src),
387                   IP_ARGS(match->ip_dst));
388
389     if (match->in_key_flow) {
390         ds_put_cstr(ds, ", key=flow");
391     } else {
392         ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
393     }
394
395     ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
396 }
397
398 static void
399 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
400 {
401     if (VLOG_IS_DBG_ENABLED()) {
402         struct ds ds = DS_EMPTY_INITIALIZER;
403
404         tnl_match_fmt(&tnl_port->match, &ds);
405         VLOG_INFO("%s tunnel port %s (%s)", action,
406                   tnl_port_get_name(tnl_port), ds_cstr(&ds));
407         ds_destroy(&ds);
408     }
409 }
410
411 static char *
412 tnl_port_fmt(const struct tnl_port *tnl_port)
413 {
414     const struct netdev_tunnel_config *cfg =
415         netdev_get_tunnel_config(tnl_port->ofport->netdev);
416     struct ds ds = DS_EMPTY_INITIALIZER;
417
418     ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
419                   tnl_port_get_name(tnl_port),
420                   netdev_get_type(tnl_port->ofport->netdev));
421     tnl_match_fmt(&tnl_port->match, &ds);
422
423     if (cfg->out_key != cfg->in_key ||
424         cfg->out_key_present != cfg->in_key_present ||
425         cfg->out_key_flow != cfg->in_key_flow) {
426         ds_put_cstr(&ds, ", out_key=");
427         if (!cfg->out_key_present) {
428             ds_put_cstr(&ds, "none");
429         } else if (cfg->out_key_flow) {
430             ds_put_cstr(&ds, "flow");
431         } else {
432             ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
433         }
434     }
435
436     if (cfg->ttl_inherit) {
437         ds_put_cstr(&ds, ", ttl=inherit");
438     } else {
439         ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
440     }
441
442     if (cfg->tos_inherit) {
443         ds_put_cstr(&ds, ", tos=inherit");
444     } else if (cfg->tos) {
445         ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
446     }
447
448     if (!cfg->dont_fragment) {
449         ds_put_cstr(&ds, ", df=false");
450     }
451
452     if (cfg->csum) {
453         ds_put_cstr(&ds, ", csum=true");
454     }
455
456     ds_put_cstr(&ds, ")\n");
457
458     return ds_steal_cstr(&ds);
459 }
460
461 static const char *
462 tnl_port_get_name(const struct tnl_port *tnl_port)
463 {
464     return netdev_get_name(tnl_port->ofport->netdev);
465 }