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