13d821ce13249539c35449ef8283aabcd510026e
[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 metadata for packet-outs
36  * Disallow netdevs with names like "gre64_system" to prevent collisions. */
37
38 VLOG_DEFINE_THIS_MODULE(tunnel);
39
40 /* skb mark used for IPsec tunnel packets */
41 #define IPSEC_MARK 1
42
43 struct tnl_match {
44     ovs_be64 in_key;
45     ovs_be32 ip_src;
46     ovs_be32 ip_dst;
47     uint32_t odp_port;
48     uint32_t skb_mark;
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.skb_mark = cfg->ipsec ? IPSEC_MARK : 0;
98     tnl_port->match.in_key_flow = cfg->in_key_flow;
99     tnl_port->match.odp_port = odp_port;
100
101     existing_port = tnl_find_exact(&tnl_port->match);
102     if (existing_port) {
103         if (warn) {
104             struct ds ds = DS_EMPTY_INITIALIZER;
105             tnl_match_fmt(&tnl_port->match, &ds);
106             VLOG_WARN("%s: attempting to add tunnel port with same config as "
107                       "port '%s' (%s)", tnl_port_get_name(tnl_port),
108                       tnl_port_get_name(existing_port), ds_cstr(&ds));
109             ds_destroy(&ds);
110             free(tnl_port);
111         }
112         return &void_tnl_port;
113     }
114
115     hmap_insert(&tnl_match_map, &tnl_port->match_node,
116                 tnl_hash(&tnl_port->match));
117     tnl_port_mod_log(tnl_port, "adding");
118     return tnl_port;
119 }
120
121 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
122  * must be added before they can be used by the module. 'ofport' must be a
123  * tunnel. */
124 struct tnl_port *
125 tnl_port_add(const struct ofport *ofport, uint32_t odp_port)
126 {
127     return tnl_port_add__(ofport, odp_port, true);
128 }
129
130 /* Checks if the tnl_port pointed to by 'tnl_portp' needs reconfiguration due
131  * to changes in its netdev_tunnel_config.  If it does, updates 'tnl_portp' to
132  * point to a new tnl_port and returns true.  Otherwise, returns false.
133  * 'ofport' and 'odp_port' should be the same as would be passed to
134  * tnl_port_add(). */
135 bool
136 tnl_port_reconfigure(const struct ofport *ofport, uint32_t odp_port,
137                      struct tnl_port **tnl_portp)
138 {
139     struct tnl_port *tnl_port = *tnl_portp;
140
141     if (tnl_port == &void_tnl_port) {
142         *tnl_portp = tnl_port_add__(ofport, odp_port, false);
143         return *tnl_portp != &void_tnl_port;
144     } else if (tnl_port->ofport != ofport
145                || tnl_port->match.odp_port != odp_port
146                || tnl_port->netdev_seq != netdev_change_seq(ofport->netdev)) {
147         VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
148         tnl_port_del(tnl_port);
149         *tnl_portp = tnl_port_add(ofport, odp_port);
150         return true;
151     }
152     return false;
153 }
154
155 /* Removes 'tnl_port' from the module. */
156 void
157 tnl_port_del(struct tnl_port *tnl_port)
158 {
159     if (tnl_port && tnl_port != &void_tnl_port) {
160         tnl_port_mod_log(tnl_port, "removing");
161         hmap_remove(&tnl_match_map, &tnl_port->match_node);
162         free(tnl_port);
163     }
164 }
165
166 /* Transforms 'flow' so that it appears to have been received by a tunnel
167  * OpenFlow port controlled by this module instead of the datapath port it
168  * actually came in on.  Sets 'flow''s in_port to the appropriate OpenFlow port
169  * number.  Returns the 'ofport' corresponding to the new in_port.
170  *
171  * Callers should verify that 'flow' needs to be received by calling
172  * tnl_port_should_receive() before this function.
173  *
174  * Leaves 'flow' untouched and returns null if unsuccessful. */
175 const struct ofport *
176 tnl_port_receive(struct flow *flow)
177 {
178     char *pre_flow_str = NULL;
179     struct tnl_port *tnl_port;
180     struct tnl_match match;
181
182     memset(&match, 0, sizeof match);
183     match.odp_port = flow->in_port;
184     match.ip_src = flow->tunnel.ip_dst;
185     match.ip_dst = flow->tunnel.ip_src;
186     match.in_key = flow->tunnel.tun_id;
187     match.skb_mark = flow->skb_mark;
188
189     tnl_port = tnl_find(&match);
190     if (!tnl_port) {
191         struct ds ds = DS_EMPTY_INITIALIZER;
192
193         tnl_match_fmt(&match, &ds);
194         VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", ds_cstr(&ds));
195         ds_destroy(&ds);
196         return NULL;
197     }
198
199     if (!VLOG_DROP_DBG(&dbg_rl)) {
200         pre_flow_str = flow_to_string(flow);
201     }
202
203     flow->in_port = tnl_port->ofport->ofp_port;
204     memset(&flow->tunnel, 0, sizeof flow->tunnel);
205     flow->tunnel.tun_id = match.in_key;
206
207     if (pre_flow_str) {
208         char *post_flow_str = flow_to_string(flow);
209         char *tnl_str = tnl_port_fmt(tnl_port);
210         VLOG_DBG("flow received\n"
211                  "%s"
212                  " pre: %s\n"
213                  "post: %s",
214                  tnl_str, pre_flow_str, post_flow_str);
215         free(tnl_str);
216         free(pre_flow_str);
217         free(post_flow_str);
218     }
219     return tnl_port->ofport;
220 }
221
222 /* Given that 'flow' should be output to the ofport corresponding to
223  * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
224  * port that the output should happen on.  May return OVSP_NONE if the output
225  * shouldn't occur. */
226 uint32_t
227 tnl_port_send(const struct tnl_port *tnl_port, struct flow *flow)
228 {
229     const struct netdev_tunnel_config *cfg;
230     char *pre_flow_str = NULL;
231
232     if (tnl_port == &void_tnl_port) {
233         return OVSP_NONE;
234     }
235
236     cfg = netdev_get_tunnel_config(tnl_port->ofport->netdev);
237     ovs_assert(cfg);
238
239     if (!VLOG_DROP_DBG(&dbg_rl)) {
240         pre_flow_str = flow_to_string(flow);
241     }
242
243     flow->tunnel.ip_src = tnl_port->match.ip_src;
244     flow->tunnel.ip_dst = tnl_port->match.ip_dst;
245     flow->skb_mark = tnl_port->match.skb_mark;
246
247     if (!cfg->out_key_flow) {
248         flow->tunnel.tun_id = cfg->out_key;
249     }
250
251     if (cfg->ttl_inherit && is_ip_any(flow)) {
252         flow->tunnel.ip_ttl = flow->nw_ttl;
253     } else {
254         flow->tunnel.ip_ttl = cfg->ttl;
255     }
256
257     if (cfg->tos_inherit && is_ip_any(flow)) {
258         flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
259     } else {
260         flow->tunnel.ip_tos = cfg->tos;
261     }
262
263     if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
264         flow->tunnel.ip_tos |= IP_ECN_ECT_0;
265     } else {
266         flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
267     }
268
269     flow->tunnel.flags = (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
270         | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
271         | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
272
273     if (pre_flow_str) {
274         char *post_flow_str = flow_to_string(flow);
275         char *tnl_str = tnl_port_fmt(tnl_port);
276         VLOG_DBG("flow sent\n"
277                  "%s"
278                  " pre: %s\n"
279                  "post: %s",
280                  tnl_str, pre_flow_str, post_flow_str);
281         free(tnl_str);
282         free(pre_flow_str);
283         free(post_flow_str);
284     }
285
286     return tnl_port->match.odp_port;
287 }
288
289 static uint32_t
290 tnl_hash(struct tnl_match *match)
291 {
292     BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
293     return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
294 }
295
296 static struct tnl_port *
297 tnl_find_exact(struct tnl_match *match)
298 {
299     struct tnl_port *tnl_port;
300
301     HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match),
302                              &tnl_match_map) {
303         if (!memcmp(match, &tnl_port->match, sizeof *match)) {
304             return tnl_port;
305         }
306     }
307     return NULL;
308 }
309
310 static struct tnl_port *
311 tnl_find(struct tnl_match *match_)
312 {
313     struct tnl_match match = *match_;
314     struct tnl_port *tnl_port;
315
316     /* remote_ip, local_ip, in_key */
317     tnl_port = tnl_find_exact(&match);
318     if (tnl_port) {
319         return tnl_port;
320     }
321
322     /* remote_ip, in_key */
323     match.ip_src = 0;
324     tnl_port = tnl_find_exact(&match);
325     if (tnl_port) {
326         return tnl_port;
327     }
328     match.ip_src = match_->ip_src;
329
330     /* remote_ip, local_ip */
331     match.in_key = 0;
332     match.in_key_flow = true;
333     tnl_port = tnl_find_exact(&match);
334     if (tnl_port) {
335         return tnl_port;
336     }
337
338     /* remote_ip */
339     match.ip_src = 0;
340     tnl_port = tnl_find_exact(&match);
341     if (tnl_port) {
342         return tnl_port;
343     }
344
345     return NULL;
346 }
347
348 static void
349 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
350 {
351     ds_put_format(ds, IP_FMT"->"IP_FMT, IP_ARGS(match->ip_src),
352                   IP_ARGS(match->ip_dst));
353
354     if (match->in_key_flow) {
355         ds_put_cstr(ds, ", key=flow");
356     } else {
357         ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
358     }
359
360     ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
361     ds_put_format(ds, ", skb mark=%"PRIu32, match->skb_mark);
362 }
363
364 static void
365 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
366 {
367     if (VLOG_IS_DBG_ENABLED()) {
368         struct ds ds = DS_EMPTY_INITIALIZER;
369
370         tnl_match_fmt(&tnl_port->match, &ds);
371         VLOG_INFO("%s tunnel port %s (%s)", action,
372                   tnl_port_get_name(tnl_port), ds_cstr(&ds));
373         ds_destroy(&ds);
374     }
375 }
376
377 static char *
378 tnl_port_fmt(const struct tnl_port *tnl_port)
379 {
380     const struct netdev_tunnel_config *cfg =
381         netdev_get_tunnel_config(tnl_port->ofport->netdev);
382     struct ds ds = DS_EMPTY_INITIALIZER;
383
384     ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
385                   tnl_port_get_name(tnl_port),
386                   netdev_get_type(tnl_port->ofport->netdev));
387     tnl_match_fmt(&tnl_port->match, &ds);
388
389     if (cfg->out_key != cfg->in_key ||
390         cfg->out_key_present != cfg->in_key_present ||
391         cfg->out_key_flow != cfg->in_key_flow) {
392         ds_put_cstr(&ds, ", out_key=");
393         if (!cfg->out_key_present) {
394             ds_put_cstr(&ds, "none");
395         } else if (cfg->out_key_flow) {
396             ds_put_cstr(&ds, "flow");
397         } else {
398             ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
399         }
400     }
401
402     if (cfg->ttl_inherit) {
403         ds_put_cstr(&ds, ", ttl=inherit");
404     } else {
405         ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
406     }
407
408     if (cfg->tos_inherit) {
409         ds_put_cstr(&ds, ", tos=inherit");
410     } else if (cfg->tos) {
411         ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
412     }
413
414     if (!cfg->dont_fragment) {
415         ds_put_cstr(&ds, ", df=false");
416     }
417
418     if (cfg->csum) {
419         ds_put_cstr(&ds, ", csum=true");
420     }
421
422     ds_put_cstr(&ds, ")\n");
423
424     return ds_steal_cstr(&ds);
425 }
426
427 static const char *
428 tnl_port_get_name(const struct tnl_port *tnl_port)
429 {
430     return netdev_get_name(tnl_port->ofport->netdev);
431 }