tunnel: Hide 'struct tnl_port' internally.
[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 struct tnl_match {
35     ovs_be64 in_key;
36     ovs_be32 ip_src;
37     ovs_be32 ip_dst;
38     odp_port_t odp_port;
39     uint32_t skb_mark;
40     bool in_key_flow;
41     bool ip_src_flow;
42     bool ip_dst_flow;
43 };
44
45 struct tnl_port {
46     struct hmap_node ofport_node;
47     struct hmap_node match_node;
48
49     const struct ofport_dpif *ofport;
50     unsigned int netdev_seq;
51     struct netdev *netdev;
52
53     struct tnl_match match;
54 };
55
56 static struct hmap tnl_match_map = HMAP_INITIALIZER(&tnl_match_map);
57 static struct hmap ofport_map = HMAP_INITIALIZER(&ofport_map);
58
59 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
60 static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(60, 60);
61
62 static struct tnl_port *tnl_find(struct tnl_match *);
63 static struct tnl_port *tnl_find_exact(struct tnl_match *);
64 static struct tnl_port *tnl_find_ofport(const struct ofport_dpif *);
65
66 static uint32_t tnl_hash(struct tnl_match *);
67 static void tnl_match_fmt(const struct tnl_match *, struct ds *);
68 static char *tnl_port_fmt(const struct tnl_port *);
69 static void tnl_port_mod_log(const struct tnl_port *, const char *action);
70 static const char *tnl_port_get_name(const struct tnl_port *);
71
72 static bool
73 tnl_port_add__(const struct ofport_dpif *ofport, const struct netdev *netdev,
74                odp_port_t odp_port, bool warn)
75 {
76     const struct netdev_tunnel_config *cfg;
77     struct tnl_port *existing_port;
78     struct tnl_port *tnl_port;
79
80     cfg = netdev_get_tunnel_config(netdev);
81     ovs_assert(cfg);
82
83     tnl_port = xzalloc(sizeof *tnl_port);
84     tnl_port->ofport = ofport;
85     tnl_port->netdev = netdev_ref(netdev);
86     tnl_port->netdev_seq = netdev_change_seq(tnl_port->netdev);
87
88     tnl_port->match.in_key = cfg->in_key;
89     tnl_port->match.ip_src = cfg->ip_src;
90     tnl_port->match.ip_dst = cfg->ip_dst;
91     tnl_port->match.ip_src_flow = cfg->ip_src_flow;
92     tnl_port->match.ip_dst_flow = cfg->ip_dst_flow;
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 false;
109     }
110
111     hmap_insert(&ofport_map, &tnl_port->ofport_node, hash_pointer(ofport, 0));
112     hmap_insert(&tnl_match_map, &tnl_port->match_node,
113                 tnl_hash(&tnl_port->match));
114     tnl_port_mod_log(tnl_port, "adding");
115     return true;
116 }
117
118 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
119  * must be added before they can be used by the module. 'ofport' must be a
120  * tunnel. */
121 void
122 tnl_port_add(const struct ofport_dpif *ofport, const struct netdev *netdev,
123              odp_port_t odp_port)
124 {
125     tnl_port_add__(ofport, netdev, odp_port, true);
126 }
127
128 /* Checks if the tunnel represented by 'ofport' reconfiguration due to changes
129  * in its netdev_tunnel_config.  If it does, returns true. Otherwise, returns
130  * false.  'ofport' and 'odp_port' should be the same as would be passed to
131  * tnl_port_add(). */
132 bool
133 tnl_port_reconfigure(const struct ofport_dpif *ofport,
134                      const struct netdev *netdev, odp_port_t odp_port)
135 {
136     struct tnl_port *tnl_port = tnl_find_ofport(ofport);
137
138     if (!tnl_port) {
139         return tnl_port_add__(ofport, netdev, odp_port, false);
140     } else if (tnl_port->netdev != netdev
141                || tnl_port->match.odp_port != odp_port
142                || tnl_port->netdev_seq != netdev_change_seq(netdev)) {
143         VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
144         tnl_port_del(ofport);
145         tnl_port_add(ofport, netdev, odp_port);
146         return true;
147     }
148     return false;
149 }
150
151 /* Removes 'ofport' from the module. */
152 void
153 tnl_port_del(const struct ofport_dpif *ofport)
154 {
155     struct tnl_port *tnl_port = ofport ? tnl_find_ofport(ofport) : NULL;
156
157     if (tnl_port) {
158         tnl_port_mod_log(tnl_port, "removing");
159         hmap_remove(&tnl_match_map, &tnl_port->match_node);
160         hmap_remove(&ofport_map, &tnl_port->ofport_node);
161         netdev_close(tnl_port->netdev);
162         free(tnl_port);
163     }
164 }
165
166 /* Looks in the table of tunnels for a tunnel matching the metadata in 'flow'.
167  * Returns the 'ofport' corresponding to the new in_port, or a null pointer if
168  * none is found.
169  *
170  * Callers should verify that 'flow' needs to be received by calling
171  * tnl_port_should_receive() before this function. */
172 const struct ofport_dpif *
173 tnl_port_receive(const struct flow *flow)
174 {
175     char *pre_flow_str = NULL;
176     struct tnl_port *tnl_port;
177     struct tnl_match match;
178
179     memset(&match, 0, sizeof match);
180     match.odp_port = flow->in_port.odp_port;
181     match.ip_src = flow->tunnel.ip_dst;
182     match.ip_dst = flow->tunnel.ip_src;
183     match.in_key = flow->tunnel.tun_id;
184     match.skb_mark = flow->skb_mark;
185
186     tnl_port = tnl_find(&match);
187     if (!tnl_port) {
188         struct ds ds = DS_EMPTY_INITIALIZER;
189
190         tnl_match_fmt(&match, &ds);
191         VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", ds_cstr(&ds));
192         ds_destroy(&ds);
193         return NULL;
194     }
195
196     if (!VLOG_DROP_DBG(&dbg_rl)) {
197         pre_flow_str = flow_to_string(flow);
198     }
199
200     if (pre_flow_str) {
201         char *post_flow_str = flow_to_string(flow);
202         char *tnl_str = tnl_port_fmt(tnl_port);
203         VLOG_DBG("flow received\n"
204                  "%s"
205                  " pre: %s\n"
206                  "post: %s",
207                  tnl_str, pre_flow_str, post_flow_str);
208         free(tnl_str);
209         free(pre_flow_str);
210         free(post_flow_str);
211     }
212     return tnl_port->ofport;
213 }
214
215 /* Given that 'flow' should be output to the ofport corresponding to
216  * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
217  * port that the output should happen on.  May return ODPP_NONE if the output
218  * shouldn't occur. */
219 odp_port_t
220 tnl_port_send(const struct ofport_dpif *ofport, struct flow *flow,
221               struct flow_wildcards *wc)
222 {
223     struct tnl_port *tnl_port = tnl_find_ofport(ofport);
224     const struct netdev_tunnel_config *cfg;
225     char *pre_flow_str = NULL;
226
227     if (!tnl_port) {
228         return ODPP_NONE;
229     }
230
231     cfg = netdev_get_tunnel_config(tnl_port->netdev);
232     ovs_assert(cfg);
233
234     if (!VLOG_DROP_DBG(&dbg_rl)) {
235         pre_flow_str = flow_to_string(flow);
236     }
237
238     if (!cfg->ip_src_flow) {
239         flow->tunnel.ip_src = tnl_port->match.ip_src;
240     }
241     if (!cfg->ip_dst_flow) {
242         flow->tunnel.ip_dst = tnl_port->match.ip_dst;
243     }
244     flow->skb_mark = tnl_port->match.skb_mark;
245
246     if (!cfg->out_key_flow) {
247         flow->tunnel.tun_id = cfg->out_key;
248     }
249
250     if (cfg->ttl_inherit && is_ip_any(flow)) {
251         wc->masks.nw_ttl = 0xff;
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         wc->masks.nw_tos = 0xff;
259         flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
260     } else {
261         flow->tunnel.ip_tos = cfg->tos;
262     }
263
264     /* ECN fields are always inherited. */
265     if (is_ip_any(flow)) {
266         wc->masks.nw_tos |= IP_ECN_MASK;
267     }
268
269     if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
270         flow->tunnel.ip_tos |= IP_ECN_ECT_0;
271     } else {
272         flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
273     }
274
275     flow->tunnel.flags = (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
276         | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
277         | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
278
279     if (pre_flow_str) {
280         char *post_flow_str = flow_to_string(flow);
281         char *tnl_str = tnl_port_fmt(tnl_port);
282         VLOG_DBG("flow sent\n"
283                  "%s"
284                  " pre: %s\n"
285                  "post: %s",
286                  tnl_str, pre_flow_str, post_flow_str);
287         free(tnl_str);
288         free(pre_flow_str);
289         free(post_flow_str);
290     }
291
292     return tnl_port->match.odp_port;
293 }
294
295 static uint32_t
296 tnl_hash(struct tnl_match *match)
297 {
298     BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
299     return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
300 }
301
302 static struct tnl_port *
303 tnl_find_ofport(const struct ofport_dpif *ofport)
304 {
305     struct tnl_port *tnl_port;
306
307     HMAP_FOR_EACH_IN_BUCKET (tnl_port, ofport_node, hash_pointer(ofport, 0),
308                              &ofport_map) {
309         if (tnl_port->ofport == ofport) {
310             return tnl_port;
311         }
312     }
313     return NULL;
314 }
315
316 static struct tnl_port *
317 tnl_find_exact(struct tnl_match *match)
318 {
319     struct tnl_port *tnl_port;
320
321     HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match),
322                              &tnl_match_map) {
323         if (!memcmp(match, &tnl_port->match, sizeof *match)) {
324             return tnl_port;
325         }
326     }
327     return NULL;
328 }
329
330 static struct tnl_port *
331 tnl_find(struct tnl_match *match_)
332 {
333     struct tnl_match match = *match_;
334     struct tnl_port *tnl_port;
335
336     /* remote_ip, local_ip, in_key */
337     tnl_port = tnl_find_exact(&match);
338     if (tnl_port) {
339         return tnl_port;
340     }
341
342     /* remote_ip, in_key */
343     match.ip_src = 0;
344     tnl_port = tnl_find_exact(&match);
345     if (tnl_port) {
346         return tnl_port;
347     }
348     match.ip_src = match_->ip_src;
349
350     /* remote_ip, local_ip */
351     match.in_key = 0;
352     match.in_key_flow = true;
353     tnl_port = tnl_find_exact(&match);
354     if (tnl_port) {
355         return tnl_port;
356     }
357
358     /* remote_ip */
359     match.ip_src = 0;
360     tnl_port = tnl_find_exact(&match);
361     if (tnl_port) {
362         return tnl_port;
363     }
364
365     /* Flow-based remote */
366     match.ip_dst = 0;
367     match.ip_dst_flow = true;
368     tnl_port = tnl_find_exact(&match);
369     if (tnl_port) {
370         return tnl_port;
371     }
372
373     /* Flow-based everything */
374     match.ip_src = 0;
375     match.ip_src_flow = true;
376     tnl_port = tnl_find_exact(&match);
377     if (tnl_port) {
378         return tnl_port;
379     }
380
381     return NULL;
382 }
383
384 static void
385 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
386 {
387     if (!match->ip_dst_flow) {
388         ds_put_format(ds, IP_FMT"->"IP_FMT, IP_ARGS(match->ip_src),
389                       IP_ARGS(match->ip_dst));
390     } else if (!match->ip_src_flow) {
391         ds_put_format(ds, IP_FMT"->flow", IP_ARGS(match->ip_src));
392     } else {
393         ds_put_cstr(ds, "flow->flow");
394     }
395
396     if (match->in_key_flow) {
397         ds_put_cstr(ds, ", key=flow");
398     } else {
399         ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
400     }
401
402     ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
403     ds_put_format(ds, ", skb mark=%"PRIu32, match->skb_mark);
404 }
405
406 static void
407 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
408 {
409     if (VLOG_IS_DBG_ENABLED()) {
410         struct ds ds = DS_EMPTY_INITIALIZER;
411
412         tnl_match_fmt(&tnl_port->match, &ds);
413         VLOG_INFO("%s tunnel port %s (%s)", action,
414                   tnl_port_get_name(tnl_port), ds_cstr(&ds));
415         ds_destroy(&ds);
416     }
417 }
418
419 static char *
420 tnl_port_fmt(const struct tnl_port *tnl_port)
421 {
422     const struct netdev_tunnel_config *cfg =
423         netdev_get_tunnel_config(tnl_port->netdev);
424     struct ds ds = DS_EMPTY_INITIALIZER;
425
426     ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
427                   tnl_port_get_name(tnl_port),
428                   netdev_get_type(tnl_port->netdev));
429     tnl_match_fmt(&tnl_port->match, &ds);
430
431     if (cfg->out_key != cfg->in_key ||
432         cfg->out_key_present != cfg->in_key_present ||
433         cfg->out_key_flow != cfg->in_key_flow) {
434         ds_put_cstr(&ds, ", out_key=");
435         if (!cfg->out_key_present) {
436             ds_put_cstr(&ds, "none");
437         } else if (cfg->out_key_flow) {
438             ds_put_cstr(&ds, "flow");
439         } else {
440             ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
441         }
442     }
443
444     if (cfg->ttl_inherit) {
445         ds_put_cstr(&ds, ", ttl=inherit");
446     } else {
447         ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
448     }
449
450     if (cfg->tos_inherit) {
451         ds_put_cstr(&ds, ", tos=inherit");
452     } else if (cfg->tos) {
453         ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
454     }
455
456     if (!cfg->dont_fragment) {
457         ds_put_cstr(&ds, ", df=false");
458     }
459
460     if (cfg->csum) {
461         ds_put_cstr(&ds, ", csum=true");
462     }
463
464     ds_put_cstr(&ds, ")\n");
465
466     return ds_steal_cstr(&ds);
467 }
468
469 static const char *
470 tnl_port_get_name(const struct tnl_port *tnl_port)
471 {
472     return netdev_get_name(tnl_port->netdev);
473 }