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 match_node;
48 const struct ofport_dpif *ofport;
49 unsigned int netdev_seq;
50 struct netdev *netdev;
52 struct tnl_match match;
55 static struct hmap tnl_match_map = HMAP_INITIALIZER(&tnl_match_map);
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;
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);
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 *);
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)
78 const struct netdev_tunnel_config *cfg;
79 struct tnl_port *existing_port;
80 struct tnl_port *tnl_port;
82 cfg = netdev_get_tunnel_config(netdev);
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);
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;
99 existing_port = tnl_find_exact(&tnl_port->match);
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));
110 return &void_tnl_port;
113 hmap_insert(&tnl_match_map, &tnl_port->match_node,
114 tnl_hash(&tnl_port->match));
115 tnl_port_mod_log(tnl_port, "adding");
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
123 tnl_port_add(const struct ofport_dpif *ofport, const struct netdev *netdev,
126 return tnl_port_add__(ofport, netdev, odp_port, true);
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
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)
139 struct tnl_port *tnl_port = *tnl_portp;
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);
156 /* Removes 'tnl_port' from the module. */
158 tnl_port_del(struct tnl_port *tnl_port)
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);
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
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)
177 char *pre_flow_str = NULL;
178 struct tnl_port *tnl_port;
179 struct tnl_match match;
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;
188 tnl_port = tnl_find(&match);
190 struct ds ds = DS_EMPTY_INITIALIZER;
192 tnl_match_fmt(&match, &ds);
193 VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", ds_cstr(&ds));
198 if (!VLOG_DROP_DBG(&dbg_rl)) {
199 pre_flow_str = flow_to_string(flow);
203 char *post_flow_str = flow_to_string(flow);
204 char *tnl_str = tnl_port_fmt(tnl_port);
205 VLOG_DBG("flow received\n"
209 tnl_str, pre_flow_str, post_flow_str);
214 return tnl_port->ofport;
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. */
222 tnl_port_send(const struct tnl_port *tnl_port, struct flow *flow,
223 struct flow_wildcards *wc)
225 const struct netdev_tunnel_config *cfg;
226 char *pre_flow_str = NULL;
228 if (tnl_port == &void_tnl_port) {
232 cfg = netdev_get_tunnel_config(tnl_port->netdev);
235 if (!VLOG_DROP_DBG(&dbg_rl)) {
236 pre_flow_str = flow_to_string(flow);
239 if (!cfg->ip_src_flow) {
240 flow->tunnel.ip_src = tnl_port->match.ip_src;
242 if (!cfg->ip_dst_flow) {
243 flow->tunnel.ip_dst = tnl_port->match.ip_dst;
245 flow->skb_mark = tnl_port->match.skb_mark;
247 if (!cfg->out_key_flow) {
248 flow->tunnel.tun_id = cfg->out_key;
251 if (cfg->ttl_inherit && is_ip_any(flow)) {
252 wc->masks.nw_ttl = 0xff;
253 flow->tunnel.ip_ttl = flow->nw_ttl;
255 flow->tunnel.ip_ttl = cfg->ttl;
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;
262 flow->tunnel.ip_tos = cfg->tos;
265 /* ECN fields are always inherited. */
266 if (is_ip_any(flow)) {
267 wc->masks.nw_tos |= IP_ECN_MASK;
270 if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
271 flow->tunnel.ip_tos |= IP_ECN_ECT_0;
273 flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
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);
281 char *post_flow_str = flow_to_string(flow);
282 char *tnl_str = tnl_port_fmt(tnl_port);
283 VLOG_DBG("flow sent\n"
287 tnl_str, pre_flow_str, post_flow_str);
293 return tnl_port->match.odp_port;
297 tnl_hash(struct tnl_match *match)
299 BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
300 return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
303 static struct tnl_port *
304 tnl_find_exact(struct tnl_match *match)
306 struct tnl_port *tnl_port;
308 HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match),
310 if (!memcmp(match, &tnl_port->match, sizeof *match)) {
317 static struct tnl_port *
318 tnl_find(struct tnl_match *match_)
320 struct tnl_match match = *match_;
321 struct tnl_port *tnl_port;
323 /* remote_ip, local_ip, in_key */
324 tnl_port = tnl_find_exact(&match);
329 /* remote_ip, in_key */
331 tnl_port = tnl_find_exact(&match);
335 match.ip_src = match_->ip_src;
337 /* remote_ip, local_ip */
339 match.in_key_flow = true;
340 tnl_port = tnl_find_exact(&match);
347 tnl_port = tnl_find_exact(&match);
352 /* Flow-based remote */
354 match.ip_dst_flow = true;
355 tnl_port = tnl_find_exact(&match);
360 /* Flow-based everything */
362 match.ip_src_flow = true;
363 tnl_port = tnl_find_exact(&match);
372 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
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));
380 ds_put_cstr(ds, "flow->flow");
383 if (match->in_key_flow) {
384 ds_put_cstr(ds, ", key=flow");
386 ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
389 ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
390 ds_put_format(ds, ", skb mark=%"PRIu32, match->skb_mark);
394 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
396 if (VLOG_IS_DBG_ENABLED()) {
397 struct ds ds = DS_EMPTY_INITIALIZER;
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));
407 tnl_port_fmt(const struct tnl_port *tnl_port)
409 const struct netdev_tunnel_config *cfg =
410 netdev_get_tunnel_config(tnl_port->netdev);
411 struct ds ds = DS_EMPTY_INITIALIZER;
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);
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");
427 ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
431 if (cfg->ttl_inherit) {
432 ds_put_cstr(&ds, ", ttl=inherit");
434 ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
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);
443 if (!cfg->dont_fragment) {
444 ds_put_cstr(&ds, ", df=false");
448 ds_put_cstr(&ds, ", csum=true");
451 ds_put_cstr(&ds, ")\n");
453 return ds_steal_cstr(&ds);
457 tnl_port_get_name(const struct tnl_port *tnl_port)
459 return netdev_get_name(tnl_port->netdev);