1 /* Copyright (c) 2013, 2014 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 "connectivity.h"
22 #include "dynamic-string.h"
30 #include "socket-util.h"
34 VLOG_DEFINE_THIS_MODULE(tunnel);
36 /* skb mark used for IPsec tunnel packets */
51 struct hmap_node ofport_node;
52 struct hmap_node match_node;
54 const struct ofport_dpif *ofport;
55 unsigned int change_seq;
56 struct netdev *netdev;
58 struct tnl_match match;
61 static struct ovs_rwlock rwlock = OVS_RWLOCK_INITIALIZER;
63 static struct hmap tnl_match_map__ = HMAP_INITIALIZER(&tnl_match_map__);
64 static struct hmap *tnl_match_map OVS_GUARDED_BY(rwlock) = &tnl_match_map__;
66 static struct hmap ofport_map__ = HMAP_INITIALIZER(&ofport_map__);
67 static struct hmap *ofport_map OVS_GUARDED_BY(rwlock) = &ofport_map__;
69 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
70 static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(60, 60);
72 static struct tnl_port *tnl_find(const struct flow *) OVS_REQ_RDLOCK(rwlock);
73 static struct tnl_port *tnl_find_exact(struct tnl_match *)
74 OVS_REQ_RDLOCK(rwlock);
75 static struct tnl_port *tnl_find_ofport(const struct ofport_dpif *)
76 OVS_REQ_RDLOCK(rwlock);
78 static uint32_t tnl_hash(struct tnl_match *);
79 static void tnl_match_fmt(const struct tnl_match *, struct ds *);
80 static char *tnl_port_fmt(const struct tnl_port *) OVS_REQ_RDLOCK(rwlock);
81 static void tnl_port_mod_log(const struct tnl_port *, const char *action)
82 OVS_REQ_RDLOCK(rwlock);
83 static const char *tnl_port_get_name(const struct tnl_port *)
84 OVS_REQ_RDLOCK(rwlock);
85 static void tnl_port_del__(const struct ofport_dpif *) OVS_REQ_WRLOCK(rwlock);
88 tnl_port_add__(const struct ofport_dpif *ofport, const struct netdev *netdev,
89 odp_port_t odp_port, bool warn)
90 OVS_REQ_WRLOCK(rwlock)
92 const struct netdev_tunnel_config *cfg;
93 struct tnl_port *existing_port;
94 struct tnl_port *tnl_port;
96 cfg = netdev_get_tunnel_config(netdev);
99 tnl_port = xzalloc(sizeof *tnl_port);
100 tnl_port->ofport = ofport;
101 tnl_port->netdev = netdev_ref(netdev);
102 tnl_port->change_seq = seq_read(connectivity_seq_get());
104 tnl_port->match.in_key = cfg->in_key;
105 tnl_port->match.ip_src = cfg->ip_src;
106 tnl_port->match.ip_dst = cfg->ip_dst;
107 tnl_port->match.ip_src_flow = cfg->ip_src_flow;
108 tnl_port->match.ip_dst_flow = cfg->ip_dst_flow;
109 tnl_port->match.pkt_mark = cfg->ipsec ? IPSEC_MARK : 0;
110 tnl_port->match.in_key_flow = cfg->in_key_flow;
111 tnl_port->match.odp_port = odp_port;
113 existing_port = tnl_find_exact(&tnl_port->match);
116 struct ds ds = DS_EMPTY_INITIALIZER;
117 tnl_match_fmt(&tnl_port->match, &ds);
118 VLOG_WARN("%s: attempting to add tunnel port with same config as "
119 "port '%s' (%s)", tnl_port_get_name(tnl_port),
120 tnl_port_get_name(existing_port), ds_cstr(&ds));
123 netdev_close(tnl_port->netdev);
128 hmap_insert(ofport_map, &tnl_port->ofport_node, hash_pointer(ofport, 0));
129 hmap_insert(tnl_match_map, &tnl_port->match_node,
130 tnl_hash(&tnl_port->match));
131 tnl_port_mod_log(tnl_port, "adding");
135 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
136 * must be added before they can be used by the module. 'ofport' must be a
139 tnl_port_add(const struct ofport_dpif *ofport, const struct netdev *netdev,
140 odp_port_t odp_port) OVS_EXCLUDED(rwlock)
142 ovs_rwlock_wrlock(&rwlock);
143 tnl_port_add__(ofport, netdev, odp_port, true);
144 ovs_rwlock_unlock(&rwlock);
147 /* Checks if the tunnel represented by 'ofport' reconfiguration due to changes
148 * in its netdev_tunnel_config. If it does, returns true. Otherwise, returns
149 * false. 'ofport' and 'odp_port' should be the same as would be passed to
152 tnl_port_reconfigure(const struct ofport_dpif *ofport,
153 const struct netdev *netdev, odp_port_t odp_port)
156 struct tnl_port *tnl_port;
157 bool changed = false;
159 ovs_rwlock_wrlock(&rwlock);
160 tnl_port = tnl_find_ofport(ofport);
162 changed = tnl_port_add__(ofport, netdev, odp_port, false);
163 } else if (tnl_port->netdev != netdev
164 || tnl_port->match.odp_port != odp_port
165 || tnl_port->change_seq != seq_read(connectivity_seq_get())) {
166 VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
167 tnl_port_del__(ofport);
168 tnl_port_add__(ofport, netdev, odp_port, true);
171 ovs_rwlock_unlock(&rwlock);
176 tnl_port_del__(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(rwlock)
178 struct tnl_port *tnl_port;
184 tnl_port = tnl_find_ofport(ofport);
186 tnl_port_mod_log(tnl_port, "removing");
187 hmap_remove(tnl_match_map, &tnl_port->match_node);
188 hmap_remove(ofport_map, &tnl_port->ofport_node);
189 netdev_close(tnl_port->netdev);
194 /* Removes 'ofport' from the module. */
196 tnl_port_del(const struct ofport_dpif *ofport) OVS_EXCLUDED(rwlock)
198 ovs_rwlock_wrlock(&rwlock);
199 tnl_port_del__(ofport);
200 ovs_rwlock_unlock(&rwlock);
203 /* Looks in the table of tunnels for a tunnel matching the metadata in 'flow'.
204 * Returns the 'ofport' corresponding to the new in_port, or a null pointer if
207 * Callers should verify that 'flow' needs to be received by calling
208 * tnl_port_should_receive() before this function. */
209 const struct ofport_dpif *
210 tnl_port_receive(const struct flow *flow) OVS_EXCLUDED(rwlock)
212 char *pre_flow_str = NULL;
213 const struct ofport_dpif *ofport;
214 struct tnl_port *tnl_port;
216 ovs_rwlock_rdlock(&rwlock);
217 tnl_port = tnl_find(flow);
218 ofport = tnl_port ? tnl_port->ofport : NULL;
220 char *flow_str = flow_to_string(flow);
222 VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", flow_str);
227 if (!VLOG_DROP_DBG(&dbg_rl)) {
228 pre_flow_str = flow_to_string(flow);
232 char *post_flow_str = flow_to_string(flow);
233 char *tnl_str = tnl_port_fmt(tnl_port);
234 VLOG_DBG("flow received\n"
238 tnl_str, pre_flow_str, post_flow_str);
245 ovs_rwlock_unlock(&rwlock);
250 tnl_ecn_ok(const struct flow *base_flow, struct flow *flow)
252 if (is_ip_any(base_flow)
253 && (flow->tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
254 if ((base_flow->nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
255 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
256 " but is not ECN capable");
259 /* Set the ECN CE value in the tunneled packet. */
260 flow->nw_tos |= IP_ECN_CE;
267 /* Should be called at the beginning of action translation to initialize
268 * wildcards and perform any actions based on receiving on tunnel port.
270 * Returns false if the packet must be dropped. */
272 tnl_xlate_init(const struct flow *base_flow, struct flow *flow,
273 struct flow_wildcards *wc)
275 if (tnl_port_should_receive(flow)) {
276 wc->masks.tunnel.tun_id = OVS_BE64_MAX;
277 wc->masks.tunnel.ip_src = OVS_BE32_MAX;
278 wc->masks.tunnel.ip_dst = OVS_BE32_MAX;
279 wc->masks.tunnel.flags = (FLOW_TNL_F_DONT_FRAGMENT |
282 wc->masks.tunnel.ip_tos = UINT8_MAX;
283 wc->masks.tunnel.ip_ttl = UINT8_MAX;
285 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
287 if (!tnl_ecn_ok(base_flow, flow)) {
291 flow->pkt_mark &= ~IPSEC_MARK;
297 /* Given that 'flow' should be output to the ofport corresponding to
298 * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
299 * port that the output should happen on. May return ODPP_NONE if the output
300 * shouldn't occur. */
302 tnl_port_send(const struct ofport_dpif *ofport, struct flow *flow,
303 struct flow_wildcards *wc) OVS_EXCLUDED(rwlock)
305 const struct netdev_tunnel_config *cfg;
306 struct tnl_port *tnl_port;
307 char *pre_flow_str = NULL;
310 ovs_rwlock_rdlock(&rwlock);
311 tnl_port = tnl_find_ofport(ofport);
312 out_port = tnl_port ? tnl_port->match.odp_port : ODPP_NONE;
317 cfg = netdev_get_tunnel_config(tnl_port->netdev);
320 if (!VLOG_DROP_DBG(&dbg_rl)) {
321 pre_flow_str = flow_to_string(flow);
324 if (!cfg->ip_src_flow) {
325 flow->tunnel.ip_src = tnl_port->match.ip_src;
327 if (!cfg->ip_dst_flow) {
328 flow->tunnel.ip_dst = tnl_port->match.ip_dst;
330 flow->pkt_mark = tnl_port->match.pkt_mark;
332 if (!cfg->out_key_flow) {
333 flow->tunnel.tun_id = cfg->out_key;
336 if (cfg->ttl_inherit && is_ip_any(flow)) {
337 wc->masks.nw_ttl = 0xff;
338 flow->tunnel.ip_ttl = flow->nw_ttl;
340 flow->tunnel.ip_ttl = cfg->ttl;
343 if (cfg->tos_inherit && is_ip_any(flow)) {
344 wc->masks.nw_tos = 0xff;
345 flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
347 flow->tunnel.ip_tos = cfg->tos;
350 /* ECN fields are always inherited. */
351 if (is_ip_any(flow)) {
352 wc->masks.nw_tos |= IP_ECN_MASK;
355 if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
356 flow->tunnel.ip_tos |= IP_ECN_ECT_0;
358 flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
361 flow->tunnel.flags = (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
362 | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
363 | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
366 char *post_flow_str = flow_to_string(flow);
367 char *tnl_str = tnl_port_fmt(tnl_port);
368 VLOG_DBG("flow sent\n"
372 tnl_str, pre_flow_str, post_flow_str);
379 ovs_rwlock_unlock(&rwlock);
384 tnl_hash(struct tnl_match *match)
386 BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
387 return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
390 static struct tnl_port *
391 tnl_find_ofport(const struct ofport_dpif *ofport) OVS_REQ_RDLOCK(rwlock)
393 struct tnl_port *tnl_port;
395 HMAP_FOR_EACH_IN_BUCKET (tnl_port, ofport_node, hash_pointer(ofport, 0),
397 if (tnl_port->ofport == ofport) {
404 static struct tnl_port *
405 tnl_find_exact(struct tnl_match *match) OVS_REQ_RDLOCK(rwlock)
407 struct tnl_port *tnl_port;
409 HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match),
411 if (!memcmp(match, &tnl_port->match, sizeof *match)) {
418 /* Returns the tnl_port that is the best match for the tunnel data in 'flow',
419 * or NULL if no tnl_port matches 'flow'. */
420 static struct tnl_port *
421 tnl_find(const struct flow *flow) OVS_REQ_RDLOCK(rwlock)
424 IP_SRC_CFG, /* ip_src must equal configured address. */
425 IP_SRC_ANY, /* Any ip_src is acceptable. */
426 IP_SRC_FLOW /* ip_src is handled in flow table. */
429 struct tnl_match_pattern {
432 enum ip_src_type ip_src;
435 static const struct tnl_match_pattern patterns[] = {
436 { false, false, IP_SRC_CFG }, /* remote_ip, local_ip, in_key. */
437 { false, false, IP_SRC_ANY }, /* remote_ip, in_key. */
438 { true, false, IP_SRC_CFG }, /* remote_ip, local_ip. */
439 { true, false, IP_SRC_ANY }, /* remote_ip. */
440 { true, true, IP_SRC_ANY }, /* Flow-based remote. */
441 { true, true, IP_SRC_FLOW }, /* Flow-based everything. */
444 const struct tnl_match_pattern *p;
445 struct tnl_match match;
447 memset(&match, 0, sizeof match);
448 match.odp_port = flow->in_port.odp_port;
449 match.pkt_mark = flow->pkt_mark;
451 for (p = patterns; p < &patterns[ARRAY_SIZE(patterns)]; p++) {
452 struct tnl_port *tnl_port;
454 match.in_key_flow = p->in_key_flow;
455 match.in_key = p->in_key_flow ? 0 : flow->tunnel.tun_id;
457 match.ip_dst_flow = p->ip_dst_flow;
458 match.ip_dst = p->ip_dst_flow ? 0 : flow->tunnel.ip_src;
460 match.ip_src_flow = p->ip_src == IP_SRC_FLOW;
461 match.ip_src = p->ip_src == IP_SRC_CFG ? flow->tunnel.ip_dst : 0;
463 tnl_port = tnl_find_exact(&match);
473 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
474 OVS_REQ_RDLOCK(rwlock)
476 if (!match->ip_dst_flow) {
477 ds_put_format(ds, IP_FMT"->"IP_FMT, IP_ARGS(match->ip_src),
478 IP_ARGS(match->ip_dst));
479 } else if (!match->ip_src_flow) {
480 ds_put_format(ds, IP_FMT"->flow", IP_ARGS(match->ip_src));
482 ds_put_cstr(ds, "flow->flow");
485 if (match->in_key_flow) {
486 ds_put_cstr(ds, ", key=flow");
488 ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
491 ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
492 ds_put_format(ds, ", pkt mark=%"PRIu32, match->pkt_mark);
496 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
497 OVS_REQ_RDLOCK(rwlock)
499 if (VLOG_IS_DBG_ENABLED()) {
500 struct ds ds = DS_EMPTY_INITIALIZER;
502 tnl_match_fmt(&tnl_port->match, &ds);
503 VLOG_INFO("%s tunnel port %s (%s)", action,
504 tnl_port_get_name(tnl_port), ds_cstr(&ds));
510 tnl_port_fmt(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
512 const struct netdev_tunnel_config *cfg =
513 netdev_get_tunnel_config(tnl_port->netdev);
514 struct ds ds = DS_EMPTY_INITIALIZER;
516 ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
517 tnl_port_get_name(tnl_port),
518 netdev_get_type(tnl_port->netdev));
519 tnl_match_fmt(&tnl_port->match, &ds);
521 if (cfg->out_key != cfg->in_key ||
522 cfg->out_key_present != cfg->in_key_present ||
523 cfg->out_key_flow != cfg->in_key_flow) {
524 ds_put_cstr(&ds, ", out_key=");
525 if (!cfg->out_key_present) {
526 ds_put_cstr(&ds, "none");
527 } else if (cfg->out_key_flow) {
528 ds_put_cstr(&ds, "flow");
530 ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
534 if (cfg->ttl_inherit) {
535 ds_put_cstr(&ds, ", ttl=inherit");
537 ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
540 if (cfg->tos_inherit) {
541 ds_put_cstr(&ds, ", tos=inherit");
542 } else if (cfg->tos) {
543 ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
546 if (!cfg->dont_fragment) {
547 ds_put_cstr(&ds, ", df=false");
551 ds_put_cstr(&ds, ", csum=true");
554 ds_put_cstr(&ds, ")\n");
556 return ds_steal_cstr(&ds);
560 tnl_port_get_name(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
562 return netdev_get_name(tnl_port->netdev);