rename superclass member to up in netdev-tunnel
[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     bool ip_src_flow;
47     bool ip_dst_flow;
48 };
49
50 struct tnl_port {
51     struct hmap_node match_node;
52
53     const struct ofport *ofport;
54     unsigned int netdev_seq;
55     struct tnl_match match;
56 };
57
58 static struct hmap tnl_match_map = HMAP_INITIALIZER(&tnl_match_map);
59
60 /* Returned to callers when their ofport will never be used to receive or send
61  * tunnel traffic. Alternatively, we could ask the caller to delete their
62  * ofport, but this would be unclean in the reconfguration case.  For the first
63  * time, an ofproto provider would have to call ofproto_port_del() on itself.*/
64 static struct tnl_port void_tnl_port;
65
66 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
67 static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(60, 60);
68
69 static struct tnl_port *tnl_find(struct tnl_match *);
70 static struct tnl_port *tnl_find_exact(struct tnl_match *);
71 static uint32_t tnl_hash(struct tnl_match *);
72 static void tnl_match_fmt(const struct tnl_match *, struct ds *);
73 static char *tnl_port_fmt(const struct tnl_port *);
74 static void tnl_port_mod_log(const struct tnl_port *, const char *action);
75 static const char *tnl_port_get_name(const struct tnl_port *);
76
77 static struct tnl_port *
78 tnl_port_add__(const struct ofport *ofport, uint32_t odp_port,
79                bool warn)
80 {
81     const struct netdev_tunnel_config *cfg;
82     struct tnl_port *existing_port;
83     struct tnl_port *tnl_port;
84
85     cfg = netdev_get_tunnel_config(ofport->netdev);
86     ovs_assert(cfg);
87
88     tnl_port = xzalloc(sizeof *tnl_port);
89     tnl_port->ofport = ofport;
90     tnl_port->netdev_seq = netdev_change_seq(tnl_port->ofport->netdev);
91
92     tnl_port->match.in_key = cfg->in_key;
93     tnl_port->match.ip_src = cfg->ip_src;
94     tnl_port->match.ip_dst = cfg->ip_dst;
95     tnl_port->match.ip_src_flow = cfg->ip_src_flow;
96     tnl_port->match.ip_dst_flow = cfg->ip_dst_flow;
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 /* 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 *
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;
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 OVSP_NONE if the output
218  * shouldn't occur. */
219 uint32_t
220 tnl_port_send(const struct tnl_port *tnl_port, struct flow *flow)
221 {
222     const struct netdev_tunnel_config *cfg;
223     char *pre_flow_str = NULL;
224
225     if (tnl_port == &void_tnl_port) {
226         return OVSP_NONE;
227     }
228
229     cfg = netdev_get_tunnel_config(tnl_port->ofport->netdev);
230     ovs_assert(cfg);
231
232     if (!VLOG_DROP_DBG(&dbg_rl)) {
233         pre_flow_str = flow_to_string(flow);
234     }
235
236     if (!cfg->ip_src_flow) {
237         flow->tunnel.ip_src = tnl_port->match.ip_src;
238     }
239     if (!cfg->ip_dst_flow) {
240         flow->tunnel.ip_dst = tnl_port->match.ip_dst;
241     }
242     flow->skb_mark = tnl_port->match.skb_mark;
243
244     if (!cfg->out_key_flow) {
245         flow->tunnel.tun_id = cfg->out_key;
246     }
247
248     if (cfg->ttl_inherit && is_ip_any(flow)) {
249         flow->tunnel.ip_ttl = flow->nw_ttl;
250     } else {
251         flow->tunnel.ip_ttl = cfg->ttl;
252     }
253
254     if (cfg->tos_inherit && is_ip_any(flow)) {
255         flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
256     } else {
257         flow->tunnel.ip_tos = cfg->tos;
258     }
259
260     if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
261         flow->tunnel.ip_tos |= IP_ECN_ECT_0;
262     } else {
263         flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
264     }
265
266     flow->tunnel.flags = (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
267         | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
268         | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
269
270     if (pre_flow_str) {
271         char *post_flow_str = flow_to_string(flow);
272         char *tnl_str = tnl_port_fmt(tnl_port);
273         VLOG_DBG("flow sent\n"
274                  "%s"
275                  " pre: %s\n"
276                  "post: %s",
277                  tnl_str, pre_flow_str, post_flow_str);
278         free(tnl_str);
279         free(pre_flow_str);
280         free(post_flow_str);
281     }
282
283     return tnl_port->match.odp_port;
284 }
285
286 static uint32_t
287 tnl_hash(struct tnl_match *match)
288 {
289     BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
290     return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
291 }
292
293 static struct tnl_port *
294 tnl_find_exact(struct tnl_match *match)
295 {
296     struct tnl_port *tnl_port;
297
298     HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match),
299                              &tnl_match_map) {
300         if (!memcmp(match, &tnl_port->match, sizeof *match)) {
301             return tnl_port;
302         }
303     }
304     return NULL;
305 }
306
307 static struct tnl_port *
308 tnl_find(struct tnl_match *match_)
309 {
310     struct tnl_match match = *match_;
311     struct tnl_port *tnl_port;
312
313     /* remote_ip, local_ip, in_key */
314     tnl_port = tnl_find_exact(&match);
315     if (tnl_port) {
316         return tnl_port;
317     }
318
319     /* remote_ip, in_key */
320     match.ip_src = 0;
321     tnl_port = tnl_find_exact(&match);
322     if (tnl_port) {
323         return tnl_port;
324     }
325     match.ip_src = match_->ip_src;
326
327     /* remote_ip, local_ip */
328     match.in_key = 0;
329     match.in_key_flow = true;
330     tnl_port = tnl_find_exact(&match);
331     if (tnl_port) {
332         return tnl_port;
333     }
334
335     /* remote_ip */
336     match.ip_src = 0;
337     tnl_port = tnl_find_exact(&match);
338     if (tnl_port) {
339         return tnl_port;
340     }
341
342     /* Flow-based remote */
343     match.ip_dst = 0;
344     match.ip_dst_flow = true;
345     tnl_port = tnl_find_exact(&match);
346     if (tnl_port) {
347         return tnl_port;
348     }
349
350     /* Flow-based everything */
351     match.ip_src = 0;
352     match.ip_src_flow = true;
353     tnl_port = tnl_find_exact(&match);
354     if (tnl_port) {
355         return tnl_port;
356     }
357
358     return NULL;
359 }
360
361 static void
362 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
363 {
364     if (!match->ip_dst_flow) {
365         ds_put_format(ds, IP_FMT"->"IP_FMT, IP_ARGS(match->ip_src),
366                       IP_ARGS(match->ip_dst));
367     } else if (!match->ip_src_flow) {
368         ds_put_format(ds, IP_FMT"->flow", IP_ARGS(match->ip_src));
369     } else {
370         ds_put_cstr(ds, "flow->flow");
371     }
372
373     if (match->in_key_flow) {
374         ds_put_cstr(ds, ", key=flow");
375     } else {
376         ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
377     }
378
379     ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
380     ds_put_format(ds, ", skb mark=%"PRIu32, match->skb_mark);
381 }
382
383 static void
384 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
385 {
386     if (VLOG_IS_DBG_ENABLED()) {
387         struct ds ds = DS_EMPTY_INITIALIZER;
388
389         tnl_match_fmt(&tnl_port->match, &ds);
390         VLOG_INFO("%s tunnel port %s (%s)", action,
391                   tnl_port_get_name(tnl_port), ds_cstr(&ds));
392         ds_destroy(&ds);
393     }
394 }
395
396 static char *
397 tnl_port_fmt(const struct tnl_port *tnl_port)
398 {
399     const struct netdev_tunnel_config *cfg =
400         netdev_get_tunnel_config(tnl_port->ofport->netdev);
401     struct ds ds = DS_EMPTY_INITIALIZER;
402
403     ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
404                   tnl_port_get_name(tnl_port),
405                   netdev_get_type(tnl_port->ofport->netdev));
406     tnl_match_fmt(&tnl_port->match, &ds);
407
408     if (cfg->out_key != cfg->in_key ||
409         cfg->out_key_present != cfg->in_key_present ||
410         cfg->out_key_flow != cfg->in_key_flow) {
411         ds_put_cstr(&ds, ", out_key=");
412         if (!cfg->out_key_present) {
413             ds_put_cstr(&ds, "none");
414         } else if (cfg->out_key_flow) {
415             ds_put_cstr(&ds, "flow");
416         } else {
417             ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
418         }
419     }
420
421     if (cfg->ttl_inherit) {
422         ds_put_cstr(&ds, ", ttl=inherit");
423     } else {
424         ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
425     }
426
427     if (cfg->tos_inherit) {
428         ds_put_cstr(&ds, ", tos=inherit");
429     } else if (cfg->tos) {
430         ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
431     }
432
433     if (!cfg->dont_fragment) {
434         ds_put_cstr(&ds, ", df=false");
435     }
436
437     if (cfg->csum) {
438         ds_put_cstr(&ds, ", csum=true");
439     }
440
441     ds_put_cstr(&ds, ")\n");
442
443     return ds_steal_cstr(&ds);
444 }
445
446 static const char *
447 tnl_port_get_name(const struct tnl_port *tnl_port)
448 {
449     return netdev_get_name(tnl_port->ofport->netdev);
450 }