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