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);
34 /* skb mark used for IPsec tunnel packets */
49 struct hmap_node ofport_node;
50 struct hmap_node match_node;
52 const struct ofport_dpif *ofport;
53 unsigned int netdev_seq;
54 struct netdev *netdev;
56 struct tnl_match match;
59 static struct ovs_rwlock rwlock = OVS_RWLOCK_INITIALIZER;
61 static struct hmap tnl_match_map__ = HMAP_INITIALIZER(&tnl_match_map__);
62 static struct hmap *tnl_match_map OVS_GUARDED_BY(rwlock) = &tnl_match_map__;
64 static struct hmap ofport_map__ = HMAP_INITIALIZER(&ofport_map__);
65 static struct hmap *ofport_map OVS_GUARDED_BY(rwlock) = &ofport_map__;
67 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
68 static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(60, 60);
70 static struct tnl_port *tnl_find(struct tnl_match *) OVS_REQ_RDLOCK(rwlock);
71 static struct tnl_port *tnl_find_exact(struct tnl_match *)
72 OVS_REQ_RDLOCK(rwlock);
73 static struct tnl_port *tnl_find_ofport(const struct ofport_dpif *)
74 OVS_REQ_RDLOCK(rwlock);
76 static uint32_t tnl_hash(struct tnl_match *);
77 static void tnl_match_fmt(const struct tnl_match *, struct ds *);
78 static char *tnl_port_fmt(const struct tnl_port *) OVS_REQ_RDLOCK(rwlock);
79 static void tnl_port_mod_log(const struct tnl_port *, const char *action)
80 OVS_REQ_RDLOCK(rwlock);
81 static const char *tnl_port_get_name(const struct tnl_port *)
82 OVS_REQ_RDLOCK(rwlock);
83 static void tnl_port_del__(const struct ofport_dpif *) OVS_REQ_WRLOCK(rwlock);
86 tnl_port_add__(const struct ofport_dpif *ofport, const struct netdev *netdev,
87 odp_port_t odp_port, bool warn)
88 OVS_REQ_WRLOCK(rwlock)
90 const struct netdev_tunnel_config *cfg;
91 struct tnl_port *existing_port;
92 struct tnl_port *tnl_port;
94 cfg = netdev_get_tunnel_config(netdev);
97 tnl_port = xzalloc(sizeof *tnl_port);
98 tnl_port->ofport = ofport;
99 tnl_port->netdev = netdev_ref(netdev);
100 tnl_port->netdev_seq = netdev_change_seq(tnl_port->netdev);
102 tnl_port->match.in_key = cfg->in_key;
103 tnl_port->match.ip_src = cfg->ip_src;
104 tnl_port->match.ip_dst = cfg->ip_dst;
105 tnl_port->match.ip_src_flow = cfg->ip_src_flow;
106 tnl_port->match.ip_dst_flow = cfg->ip_dst_flow;
107 tnl_port->match.pkt_mark = cfg->ipsec ? IPSEC_MARK : 0;
108 tnl_port->match.in_key_flow = cfg->in_key_flow;
109 tnl_port->match.odp_port = odp_port;
111 existing_port = tnl_find_exact(&tnl_port->match);
114 struct ds ds = DS_EMPTY_INITIALIZER;
115 tnl_match_fmt(&tnl_port->match, &ds);
116 VLOG_WARN("%s: attempting to add tunnel port with same config as "
117 "port '%s' (%s)", tnl_port_get_name(tnl_port),
118 tnl_port_get_name(existing_port), ds_cstr(&ds));
121 netdev_close(tnl_port->netdev);
126 hmap_insert(ofport_map, &tnl_port->ofport_node, hash_pointer(ofport, 0));
127 hmap_insert(tnl_match_map, &tnl_port->match_node,
128 tnl_hash(&tnl_port->match));
129 tnl_port_mod_log(tnl_port, "adding");
133 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
134 * must be added before they can be used by the module. 'ofport' must be a
137 tnl_port_add(const struct ofport_dpif *ofport, const struct netdev *netdev,
138 odp_port_t odp_port) OVS_EXCLUDED(rwlock)
140 ovs_rwlock_wrlock(&rwlock);
141 tnl_port_add__(ofport, netdev, odp_port, true);
142 ovs_rwlock_unlock(&rwlock);
145 /* Checks if the tunnel represented by 'ofport' reconfiguration due to changes
146 * in its netdev_tunnel_config. If it does, returns true. Otherwise, returns
147 * false. 'ofport' and 'odp_port' should be the same as would be passed to
150 tnl_port_reconfigure(const struct ofport_dpif *ofport,
151 const struct netdev *netdev, odp_port_t odp_port)
154 struct tnl_port *tnl_port;
155 bool changed = false;
157 ovs_rwlock_wrlock(&rwlock);
158 tnl_port = tnl_find_ofport(ofport);
160 changed = tnl_port_add__(ofport, netdev, odp_port, false);
161 } else if (tnl_port->netdev != netdev
162 || tnl_port->match.odp_port != odp_port
163 || tnl_port->netdev_seq != netdev_change_seq(netdev)) {
164 VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
165 tnl_port_del__(ofport);
166 tnl_port_add__(ofport, netdev, odp_port, true);
169 ovs_rwlock_unlock(&rwlock);
174 tnl_port_del__(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(rwlock)
176 struct tnl_port *tnl_port;
182 tnl_port = tnl_find_ofport(ofport);
184 tnl_port_mod_log(tnl_port, "removing");
185 hmap_remove(tnl_match_map, &tnl_port->match_node);
186 hmap_remove(ofport_map, &tnl_port->ofport_node);
187 netdev_close(tnl_port->netdev);
192 /* Removes 'ofport' from the module. */
194 tnl_port_del(const struct ofport_dpif *ofport) OVS_EXCLUDED(rwlock)
196 ovs_rwlock_wrlock(&rwlock);
197 tnl_port_del__(ofport);
198 ovs_rwlock_unlock(&rwlock);
201 /* Looks in the table of tunnels for a tunnel matching the metadata in 'flow'.
202 * Returns the 'ofport' corresponding to the new in_port, or a null pointer if
205 * Callers should verify that 'flow' needs to be received by calling
206 * tnl_port_should_receive() before this function. */
207 const struct ofport_dpif *
208 tnl_port_receive(const struct flow *flow) OVS_EXCLUDED(rwlock)
210 char *pre_flow_str = NULL;
211 const struct ofport_dpif *ofport;
212 struct tnl_port *tnl_port;
213 struct tnl_match match;
215 memset(&match, 0, sizeof match);
216 match.odp_port = flow->in_port.odp_port;
217 match.ip_src = flow->tunnel.ip_dst;
218 match.ip_dst = flow->tunnel.ip_src;
219 match.in_key = flow->tunnel.tun_id;
220 match.pkt_mark = flow->pkt_mark;
222 ovs_rwlock_rdlock(&rwlock);
223 tnl_port = tnl_find(&match);
224 ofport = tnl_port ? tnl_port->ofport : NULL;
226 struct ds ds = DS_EMPTY_INITIALIZER;
228 tnl_match_fmt(&match, &ds);
229 VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", ds_cstr(&ds));
234 if (!VLOG_DROP_DBG(&dbg_rl)) {
235 pre_flow_str = flow_to_string(flow);
239 char *post_flow_str = flow_to_string(flow);
240 char *tnl_str = tnl_port_fmt(tnl_port);
241 VLOG_DBG("flow received\n"
245 tnl_str, pre_flow_str, post_flow_str);
252 ovs_rwlock_unlock(&rwlock);
257 tnl_ecn_ok(const struct flow *base_flow, struct flow *flow)
259 if (is_ip_any(base_flow)
260 && (flow->tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
261 if ((base_flow->nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
262 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
263 " but is not ECN capable");
266 /* Set the ECN CE value in the tunneled packet. */
267 flow->nw_tos |= IP_ECN_CE;
274 /* Should be called at the beginning of action translation to initialize
275 * wildcards and perform any actions based on receiving on tunnel port.
277 * Returns false if the packet must be dropped. */
279 tnl_xlate_init(const struct flow *base_flow, struct flow *flow,
280 struct flow_wildcards *wc)
282 if (tnl_port_should_receive(flow)) {
283 memset(&wc->masks.tunnel, 0xff, sizeof wc->masks.tunnel);
284 memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
286 if (!tnl_ecn_ok(base_flow, flow)) {
290 flow->pkt_mark &= ~IPSEC_MARK;
296 /* Given that 'flow' should be output to the ofport corresponding to
297 * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
298 * port that the output should happen on. May return ODPP_NONE if the output
299 * shouldn't occur. */
301 tnl_port_send(const struct ofport_dpif *ofport, struct flow *flow,
302 struct flow_wildcards *wc) OVS_EXCLUDED(rwlock)
304 const struct netdev_tunnel_config *cfg;
305 struct tnl_port *tnl_port;
306 char *pre_flow_str = NULL;
309 ovs_rwlock_rdlock(&rwlock);
310 tnl_port = tnl_find_ofport(ofport);
311 out_port = tnl_port ? tnl_port->match.odp_port : ODPP_NONE;
316 cfg = netdev_get_tunnel_config(tnl_port->netdev);
319 if (!VLOG_DROP_DBG(&dbg_rl)) {
320 pre_flow_str = flow_to_string(flow);
323 if (!cfg->ip_src_flow) {
324 flow->tunnel.ip_src = tnl_port->match.ip_src;
326 if (!cfg->ip_dst_flow) {
327 flow->tunnel.ip_dst = tnl_port->match.ip_dst;
329 flow->pkt_mark = tnl_port->match.pkt_mark;
331 if (!cfg->out_key_flow) {
332 flow->tunnel.tun_id = cfg->out_key;
335 if (cfg->ttl_inherit && is_ip_any(flow)) {
336 wc->masks.nw_ttl = 0xff;
337 flow->tunnel.ip_ttl = flow->nw_ttl;
339 flow->tunnel.ip_ttl = cfg->ttl;
342 if (cfg->tos_inherit && is_ip_any(flow)) {
343 wc->masks.nw_tos = 0xff;
344 flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
346 flow->tunnel.ip_tos = cfg->tos;
349 /* ECN fields are always inherited. */
350 if (is_ip_any(flow)) {
351 wc->masks.nw_tos |= IP_ECN_MASK;
354 if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
355 flow->tunnel.ip_tos |= IP_ECN_ECT_0;
357 flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
360 flow->tunnel.flags = (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
361 | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
362 | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
365 char *post_flow_str = flow_to_string(flow);
366 char *tnl_str = tnl_port_fmt(tnl_port);
367 VLOG_DBG("flow sent\n"
371 tnl_str, pre_flow_str, post_flow_str);
378 ovs_rwlock_unlock(&rwlock);
383 tnl_hash(struct tnl_match *match)
385 BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
386 return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
389 static struct tnl_port *
390 tnl_find_ofport(const struct ofport_dpif *ofport) OVS_REQ_RDLOCK(rwlock)
392 struct tnl_port *tnl_port;
394 HMAP_FOR_EACH_IN_BUCKET (tnl_port, ofport_node, hash_pointer(ofport, 0),
396 if (tnl_port->ofport == ofport) {
403 static struct tnl_port *
404 tnl_find_exact(struct tnl_match *match) OVS_REQ_RDLOCK(rwlock)
406 struct tnl_port *tnl_port;
408 HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match),
410 if (!memcmp(match, &tnl_port->match, sizeof *match)) {
417 static struct tnl_port *
418 tnl_find(struct tnl_match *match_) OVS_REQ_RDLOCK(rwlock)
420 struct tnl_match match = *match_;
421 struct tnl_port *tnl_port;
423 /* remote_ip, local_ip, in_key */
424 tnl_port = tnl_find_exact(&match);
429 /* remote_ip, in_key */
431 tnl_port = tnl_find_exact(&match);
435 match.ip_src = match_->ip_src;
437 /* remote_ip, local_ip */
439 match.in_key_flow = true;
440 tnl_port = tnl_find_exact(&match);
447 tnl_port = tnl_find_exact(&match);
452 /* Flow-based remote */
454 match.ip_dst_flow = true;
455 tnl_port = tnl_find_exact(&match);
460 /* Flow-based everything */
461 match.ip_src_flow = true;
462 tnl_port = tnl_find_exact(&match);
471 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
472 OVS_REQ_RDLOCK(rwlock)
474 if (!match->ip_dst_flow) {
475 ds_put_format(ds, IP_FMT"->"IP_FMT, IP_ARGS(match->ip_src),
476 IP_ARGS(match->ip_dst));
477 } else if (!match->ip_src_flow) {
478 ds_put_format(ds, IP_FMT"->flow", IP_ARGS(match->ip_src));
480 ds_put_cstr(ds, "flow->flow");
483 if (match->in_key_flow) {
484 ds_put_cstr(ds, ", key=flow");
486 ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
489 ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
490 ds_put_format(ds, ", pkt mark=%"PRIu32, match->pkt_mark);
494 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
495 OVS_REQ_RDLOCK(rwlock)
497 if (VLOG_IS_DBG_ENABLED()) {
498 struct ds ds = DS_EMPTY_INITIALIZER;
500 tnl_match_fmt(&tnl_port->match, &ds);
501 VLOG_INFO("%s tunnel port %s (%s)", action,
502 tnl_port_get_name(tnl_port), ds_cstr(&ds));
508 tnl_port_fmt(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
510 const struct netdev_tunnel_config *cfg =
511 netdev_get_tunnel_config(tnl_port->netdev);
512 struct ds ds = DS_EMPTY_INITIALIZER;
514 ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
515 tnl_port_get_name(tnl_port),
516 netdev_get_type(tnl_port->netdev));
517 tnl_match_fmt(&tnl_port->match, &ds);
519 if (cfg->out_key != cfg->in_key ||
520 cfg->out_key_present != cfg->in_key_present ||
521 cfg->out_key_flow != cfg->in_key_flow) {
522 ds_put_cstr(&ds, ", out_key=");
523 if (!cfg->out_key_present) {
524 ds_put_cstr(&ds, "none");
525 } else if (cfg->out_key_flow) {
526 ds_put_cstr(&ds, "flow");
528 ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
532 if (cfg->ttl_inherit) {
533 ds_put_cstr(&ds, ", ttl=inherit");
535 ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
538 if (cfg->tos_inherit) {
539 ds_put_cstr(&ds, ", tos=inherit");
540 } else if (cfg->tos) {
541 ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
544 if (!cfg->dont_fragment) {
545 ds_put_cstr(&ds, ", df=false");
549 ds_put_cstr(&ds, ", csum=true");
552 ds_put_cstr(&ds, ")\n");
554 return ds_steal_cstr(&ds);
558 tnl_port_get_name(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
560 return netdev_get_name(tnl_port->netdev);