1 /* Copyright (c) 2013 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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. */
20 #include "byte-order.h"
21 #include "dynamic-string.h"
28 #include "socket-util.h"
32 VLOG_DEFINE_THIS_MODULE(tunnel);
46 struct hmap_node ofport_node;
47 struct hmap_node match_node;
49 const struct ofport_dpif *ofport;
50 unsigned int netdev_seq;
51 struct netdev *netdev;
53 struct tnl_match match;
56 static struct ovs_rwlock rwlock = OVS_RWLOCK_INITIALIZER;
58 static struct hmap tnl_match_map__ = HMAP_INITIALIZER(&tnl_match_map__);
59 static struct hmap *tnl_match_map OVS_GUARDED_BY(rwlock) = &tnl_match_map__;
61 static struct hmap ofport_map__ = HMAP_INITIALIZER(&ofport_map__);
62 static struct hmap *ofport_map OVS_GUARDED_BY(rwlock) = &ofport_map__;
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);
67 static struct tnl_port *tnl_find(struct tnl_match *) OVS_REQ_RDLOCK(rwlock);
68 static struct tnl_port *tnl_find_exact(struct tnl_match *)
69 OVS_REQ_RDLOCK(rwlock);
70 static struct tnl_port *tnl_find_ofport(const struct ofport_dpif *)
71 OVS_REQ_RDLOCK(rwlock);
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 *) OVS_REQ_RDLOCK(rwlock);
76 static void tnl_port_mod_log(const struct tnl_port *, const char *action)
77 OVS_REQ_RDLOCK(rwlock);
78 static const char *tnl_port_get_name(const struct tnl_port *)
79 OVS_REQ_RDLOCK(rwlock);
80 static void tnl_port_del__(const struct ofport_dpif *) OVS_REQ_WRLOCK(rwlock);
83 tnl_port_add__(const struct ofport_dpif *ofport, const struct netdev *netdev,
84 odp_port_t odp_port, bool warn)
85 OVS_REQ_WRLOCK(rwlock)
87 const struct netdev_tunnel_config *cfg;
88 struct tnl_port *existing_port;
89 struct tnl_port *tnl_port;
91 cfg = netdev_get_tunnel_config(netdev);
94 tnl_port = xzalloc(sizeof *tnl_port);
95 tnl_port->ofport = ofport;
96 tnl_port->netdev = netdev_ref(netdev);
97 tnl_port->netdev_seq = netdev_change_seq(tnl_port->netdev);
99 tnl_port->match.in_key = cfg->in_key;
100 tnl_port->match.ip_src = cfg->ip_src;
101 tnl_port->match.ip_dst = cfg->ip_dst;
102 tnl_port->match.ip_src_flow = cfg->ip_src_flow;
103 tnl_port->match.ip_dst_flow = cfg->ip_dst_flow;
104 tnl_port->match.pkt_mark = cfg->ipsec ? IPSEC_MARK : 0;
105 tnl_port->match.in_key_flow = cfg->in_key_flow;
106 tnl_port->match.odp_port = odp_port;
108 existing_port = tnl_find_exact(&tnl_port->match);
111 struct ds ds = DS_EMPTY_INITIALIZER;
112 tnl_match_fmt(&tnl_port->match, &ds);
113 VLOG_WARN("%s: attempting to add tunnel port with same config as "
114 "port '%s' (%s)", tnl_port_get_name(tnl_port),
115 tnl_port_get_name(existing_port), ds_cstr(&ds));
122 hmap_insert(ofport_map, &tnl_port->ofport_node, hash_pointer(ofport, 0));
123 hmap_insert(tnl_match_map, &tnl_port->match_node,
124 tnl_hash(&tnl_port->match));
125 tnl_port_mod_log(tnl_port, "adding");
129 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
130 * must be added before they can be used by the module. 'ofport' must be a
133 tnl_port_add(const struct ofport_dpif *ofport, const struct netdev *netdev,
134 odp_port_t odp_port) OVS_EXCLUDED(rwlock)
136 ovs_rwlock_wrlock(&rwlock);
137 tnl_port_add__(ofport, netdev, odp_port, true);
138 ovs_rwlock_unlock(&rwlock);
141 /* Checks if the tunnel represented by 'ofport' reconfiguration due to changes
142 * in its netdev_tunnel_config. If it does, returns true. Otherwise, returns
143 * false. 'ofport' and 'odp_port' should be the same as would be passed to
146 tnl_port_reconfigure(const struct ofport_dpif *ofport,
147 const struct netdev *netdev, odp_port_t odp_port)
150 struct tnl_port *tnl_port;
151 bool changed = false;
153 ovs_rwlock_wrlock(&rwlock);
154 tnl_port = tnl_find_ofport(ofport);
156 changed = tnl_port_add__(ofport, netdev, odp_port, false);
157 } else if (tnl_port->netdev != netdev
158 || tnl_port->match.odp_port != odp_port
159 || tnl_port->netdev_seq != netdev_change_seq(netdev)) {
160 VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
161 tnl_port_del__(ofport);
162 tnl_port_add__(ofport, netdev, odp_port, true);
165 ovs_rwlock_unlock(&rwlock);
170 tnl_port_del__(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(rwlock)
172 struct tnl_port *tnl_port;
178 tnl_port = tnl_find_ofport(ofport);
180 tnl_port_mod_log(tnl_port, "removing");
181 hmap_remove(tnl_match_map, &tnl_port->match_node);
182 hmap_remove(ofport_map, &tnl_port->ofport_node);
183 netdev_close(tnl_port->netdev);
188 /* Removes 'ofport' from the module. */
190 tnl_port_del(const struct ofport_dpif *ofport) OVS_EXCLUDED(rwlock)
192 ovs_rwlock_wrlock(&rwlock);
193 tnl_port_del__(ofport);
194 ovs_rwlock_unlock(&rwlock);
197 /* Looks in the table of tunnels for a tunnel matching the metadata in 'flow'.
198 * Returns the 'ofport' corresponding to the new in_port, or a null pointer if
201 * Callers should verify that 'flow' needs to be received by calling
202 * tnl_port_should_receive() before this function. */
203 const struct ofport_dpif *
204 tnl_port_receive(const struct flow *flow) OVS_EXCLUDED(rwlock)
206 char *pre_flow_str = NULL;
207 const struct ofport_dpif *ofport;
208 struct tnl_port *tnl_port;
209 struct tnl_match match;
211 memset(&match, 0, sizeof match);
212 match.odp_port = flow->in_port.odp_port;
213 match.ip_src = flow->tunnel.ip_dst;
214 match.ip_dst = flow->tunnel.ip_src;
215 match.in_key = flow->tunnel.tun_id;
216 match.pkt_mark = flow->pkt_mark;
218 ovs_rwlock_rdlock(&rwlock);
219 tnl_port = tnl_find(&match);
220 ofport = tnl_port ? tnl_port->ofport : NULL;
222 struct ds ds = DS_EMPTY_INITIALIZER;
224 tnl_match_fmt(&match, &ds);
225 VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", ds_cstr(&ds));
230 if (!VLOG_DROP_DBG(&dbg_rl)) {
231 pre_flow_str = flow_to_string(flow);
235 char *post_flow_str = flow_to_string(flow);
236 char *tnl_str = tnl_port_fmt(tnl_port);
237 VLOG_DBG("flow received\n"
241 tnl_str, pre_flow_str, post_flow_str);
248 ovs_rwlock_unlock(&rwlock);
253 tnl_ecn_ok(const struct flow *base_flow, struct flow *flow)
255 if (is_ip_any(base_flow)
256 && (flow->tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
257 if ((base_flow->nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
258 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
259 " but is not ECN capable");
262 /* Set the ECN CE value in the tunneled packet. */
263 flow->nw_tos |= IP_ECN_CE;
270 /* Should be called at the beginning of action translation to initialize
271 * wildcards and perform any actions based on receiving on tunnel port.
273 * Returns false if the packet must be dropped. */
275 tnl_xlate_init(const struct flow *base_flow, struct flow *flow,
276 struct flow_wildcards *wc)
278 if (tnl_port_should_receive(flow)) {
279 memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
280 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
282 if (!tnl_ecn_ok(base_flow, flow)) {
290 /* Given that 'flow' should be output to the ofport corresponding to
291 * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
292 * port that the output should happen on. May return ODPP_NONE if the output
293 * shouldn't occur. */
295 tnl_port_send(const struct ofport_dpif *ofport, struct flow *flow,
296 struct flow_wildcards *wc) OVS_EXCLUDED(rwlock)
298 const struct netdev_tunnel_config *cfg;
299 struct tnl_port *tnl_port;
300 char *pre_flow_str = NULL;
303 ovs_rwlock_rdlock(&rwlock);
304 tnl_port = tnl_find_ofport(ofport);
305 out_port = tnl_port ? tnl_port->match.odp_port : ODPP_NONE;
310 cfg = netdev_get_tunnel_config(tnl_port->netdev);
313 if (!VLOG_DROP_DBG(&dbg_rl)) {
314 pre_flow_str = flow_to_string(flow);
317 if (!cfg->ip_src_flow) {
318 flow->tunnel.ip_src = tnl_port->match.ip_src;
320 if (!cfg->ip_dst_flow) {
321 flow->tunnel.ip_dst = tnl_port->match.ip_dst;
323 flow->pkt_mark = tnl_port->match.pkt_mark;
325 if (!cfg->out_key_flow) {
326 flow->tunnel.tun_id = cfg->out_key;
329 if (cfg->ttl_inherit && is_ip_any(flow)) {
330 wc->masks.nw_ttl = 0xff;
331 flow->tunnel.ip_ttl = flow->nw_ttl;
333 flow->tunnel.ip_ttl = cfg->ttl;
336 if (cfg->tos_inherit && is_ip_any(flow)) {
337 wc->masks.nw_tos = 0xff;
338 flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
340 flow->tunnel.ip_tos = cfg->tos;
343 /* ECN fields are always inherited. */
344 if (is_ip_any(flow)) {
345 wc->masks.nw_tos |= IP_ECN_MASK;
348 if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
349 flow->tunnel.ip_tos |= IP_ECN_ECT_0;
351 flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
354 flow->tunnel.flags = (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
355 | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
356 | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
359 char *post_flow_str = flow_to_string(flow);
360 char *tnl_str = tnl_port_fmt(tnl_port);
361 VLOG_DBG("flow sent\n"
365 tnl_str, pre_flow_str, post_flow_str);
372 ovs_rwlock_unlock(&rwlock);
377 tnl_hash(struct tnl_match *match)
379 BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
380 return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
383 static struct tnl_port *
384 tnl_find_ofport(const struct ofport_dpif *ofport) OVS_REQ_RDLOCK(rwlock)
386 struct tnl_port *tnl_port;
388 HMAP_FOR_EACH_IN_BUCKET (tnl_port, ofport_node, hash_pointer(ofport, 0),
390 if (tnl_port->ofport == ofport) {
397 static struct tnl_port *
398 tnl_find_exact(struct tnl_match *match) OVS_REQ_RDLOCK(rwlock)
400 struct tnl_port *tnl_port;
402 HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match),
404 if (!memcmp(match, &tnl_port->match, sizeof *match)) {
411 static struct tnl_port *
412 tnl_find(struct tnl_match *match_) OVS_REQ_RDLOCK(rwlock)
414 struct tnl_match match = *match_;
415 struct tnl_port *tnl_port;
417 /* remote_ip, local_ip, in_key */
418 tnl_port = tnl_find_exact(&match);
423 /* remote_ip, in_key */
425 tnl_port = tnl_find_exact(&match);
429 match.ip_src = match_->ip_src;
431 /* remote_ip, local_ip */
433 match.in_key_flow = true;
434 tnl_port = tnl_find_exact(&match);
441 tnl_port = tnl_find_exact(&match);
446 /* Flow-based remote */
448 match.ip_dst_flow = true;
449 tnl_port = tnl_find_exact(&match);
454 /* Flow-based everything */
456 match.ip_src_flow = true;
457 tnl_port = tnl_find_exact(&match);
466 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
467 OVS_REQ_RDLOCK(rwlock)
469 if (!match->ip_dst_flow) {
470 ds_put_format(ds, IP_FMT"->"IP_FMT, IP_ARGS(match->ip_src),
471 IP_ARGS(match->ip_dst));
472 } else if (!match->ip_src_flow) {
473 ds_put_format(ds, IP_FMT"->flow", IP_ARGS(match->ip_src));
475 ds_put_cstr(ds, "flow->flow");
478 if (match->in_key_flow) {
479 ds_put_cstr(ds, ", key=flow");
481 ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
484 ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
485 ds_put_format(ds, ", pkt mark=%"PRIu32, match->pkt_mark);
489 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
490 OVS_REQ_RDLOCK(rwlock)
492 if (VLOG_IS_DBG_ENABLED()) {
493 struct ds ds = DS_EMPTY_INITIALIZER;
495 tnl_match_fmt(&tnl_port->match, &ds);
496 VLOG_INFO("%s tunnel port %s (%s)", action,
497 tnl_port_get_name(tnl_port), ds_cstr(&ds));
503 tnl_port_fmt(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
505 const struct netdev_tunnel_config *cfg =
506 netdev_get_tunnel_config(tnl_port->netdev);
507 struct ds ds = DS_EMPTY_INITIALIZER;
509 ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
510 tnl_port_get_name(tnl_port),
511 netdev_get_type(tnl_port->netdev));
512 tnl_match_fmt(&tnl_port->match, &ds);
514 if (cfg->out_key != cfg->in_key ||
515 cfg->out_key_present != cfg->in_key_present ||
516 cfg->out_key_flow != cfg->in_key_flow) {
517 ds_put_cstr(&ds, ", out_key=");
518 if (!cfg->out_key_present) {
519 ds_put_cstr(&ds, "none");
520 } else if (cfg->out_key_flow) {
521 ds_put_cstr(&ds, "flow");
523 ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
527 if (cfg->ttl_inherit) {
528 ds_put_cstr(&ds, ", ttl=inherit");
530 ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
533 if (cfg->tos_inherit) {
534 ds_put_cstr(&ds, ", tos=inherit");
535 } else if (cfg->tos) {
536 ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
539 if (!cfg->dont_fragment) {
540 ds_put_cstr(&ds, ", df=false");
544 ds_put_cstr(&ds, ", csum=true");
547 ds_put_cstr(&ds, ")\n");
549 return ds_steal_cstr(&ds);
553 tnl_port_get_name(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
555 return netdev_get_name(tnl_port->netdev);