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