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 "ofproto/ofproto-provider.h"
21 #include "byte-order.h"
22 #include "dynamic-string.h"
25 #include "netdev-vport.h"
29 #include "socket-util.h"
35 * Ability to generate metadata for packet-outs
36 * Disallow netdevs with names like "gre64_system" to prevent collisions. */
38 VLOG_DEFINE_THIS_MODULE(tunnel);
50 struct hmap_node match_node;
52 const struct ofport *ofport;
53 unsigned int netdev_seq;
54 struct tnl_match match;
57 static struct hmap tnl_match_map = HMAP_INITIALIZER(&tnl_match_map);
59 /* Returned to callers when their ofport will never be used to receive or send
60 * tunnel traffic. Alternatively, we could ask the caller to delete their
61 * ofport, but this would be unclean in the reconfguration case. For the first
62 * time, an ofproto provider would have to call ofproto_port_del() on itself.*/
63 static struct tnl_port void_tnl_port;
65 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
66 static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(60, 60);
68 static struct tnl_port *tnl_find(struct tnl_match *);
69 static struct tnl_port *tnl_find_exact(struct tnl_match *);
70 static uint32_t tnl_hash(struct tnl_match *);
71 static void tnl_match_fmt(const struct tnl_match *, struct ds *);
72 static char *tnl_port_fmt(const struct tnl_port *);
73 static void tnl_port_mod_log(const struct tnl_port *, const char *action);
74 static const char *tnl_port_get_name(const struct tnl_port *);
76 static struct tnl_port *
77 tnl_port_add__(const struct ofport *ofport, uint32_t odp_port,
80 const struct netdev_tunnel_config *cfg;
81 struct tnl_port *existing_port;
82 struct tnl_port *tnl_port;
84 cfg = netdev_get_tunnel_config(ofport->netdev);
87 tnl_port = xzalloc(sizeof *tnl_port);
88 tnl_port->ofport = ofport;
89 tnl_port->netdev_seq = netdev_change_seq(tnl_port->ofport->netdev);
91 tnl_port->match.in_key = cfg->in_key;
92 tnl_port->match.ip_src = cfg->ip_src;
93 tnl_port->match.ip_dst = cfg->ip_dst;
94 tnl_port->match.skb_mark = cfg->ipsec ? IPSEC_MARK : 0;
95 tnl_port->match.in_key_flow = cfg->in_key_flow;
96 tnl_port->match.odp_port = odp_port;
98 existing_port = tnl_find_exact(&tnl_port->match);
101 struct ds ds = DS_EMPTY_INITIALIZER;
102 tnl_match_fmt(&tnl_port->match, &ds);
103 VLOG_WARN("%s: attempting to add tunnel port with same config as "
104 "port '%s' (%s)", tnl_port_get_name(tnl_port),
105 tnl_port_get_name(existing_port), ds_cstr(&ds));
109 return &void_tnl_port;
112 hmap_insert(&tnl_match_map, &tnl_port->match_node,
113 tnl_hash(&tnl_port->match));
114 tnl_port_mod_log(tnl_port, "adding");
118 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
119 * must be added before they can be used by the module. 'ofport' must be a
122 tnl_port_add(const struct ofport *ofport, uint32_t odp_port)
124 return tnl_port_add__(ofport, odp_port, true);
127 /* Checks if the tnl_port pointed to by 'tnl_portp' needs reconfiguration due
128 * to changes in its netdev_tunnel_config. If it does, updates 'tnl_portp' to
129 * point to a new tnl_port and returns true. Otherwise, returns false.
130 * 'ofport' and 'odp_port' should be the same as would be passed to
133 tnl_port_reconfigure(const struct ofport *ofport, uint32_t odp_port,
134 struct tnl_port **tnl_portp)
136 struct tnl_port *tnl_port = *tnl_portp;
138 if (tnl_port == &void_tnl_port) {
139 *tnl_portp = tnl_port_add__(ofport, odp_port, false);
140 return *tnl_portp != &void_tnl_port;
141 } else if (tnl_port->ofport != ofport
142 || tnl_port->match.odp_port != odp_port
143 || tnl_port->netdev_seq != netdev_change_seq(ofport->netdev)) {
144 VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
145 tnl_port_del(tnl_port);
146 *tnl_portp = tnl_port_add(ofport, odp_port);
152 /* Removes 'tnl_port' from the module. */
154 tnl_port_del(struct tnl_port *tnl_port)
156 if (tnl_port && tnl_port != &void_tnl_port) {
157 tnl_port_mod_log(tnl_port, "removing");
158 hmap_remove(&tnl_match_map, &tnl_port->match_node);
163 /* Transforms 'flow' so that it appears to have been received by a tunnel
164 * OpenFlow port controlled by this module instead of the datapath port it
165 * actually came in on. Sets 'flow''s in_port to the appropriate OpenFlow port
166 * number. Returns the 'ofport' corresponding to the new in_port.
168 * Callers should verify that 'flow' needs to be received by calling
169 * tnl_port_should_receive() before this function.
171 * Leaves 'flow' untouched and returns null if unsuccessful. */
172 const struct ofport *
173 tnl_port_receive(struct flow *flow)
175 char *pre_flow_str = NULL;
176 struct tnl_port *tnl_port;
177 struct tnl_match match;
179 memset(&match, 0, sizeof match);
180 match.odp_port = flow->in_port;
181 match.ip_src = flow->tunnel.ip_dst;
182 match.ip_dst = flow->tunnel.ip_src;
183 match.in_key = flow->tunnel.tun_id;
184 match.skb_mark = flow->skb_mark;
186 tnl_port = tnl_find(&match);
188 struct ds ds = DS_EMPTY_INITIALIZER;
190 tnl_match_fmt(&match, &ds);
191 VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", ds_cstr(&ds));
196 if (!VLOG_DROP_DBG(&dbg_rl)) {
197 pre_flow_str = flow_to_string(flow);
200 flow->in_port = tnl_port->ofport->ofp_port;
201 memset(&flow->tunnel, 0, sizeof flow->tunnel);
202 flow->tunnel.tun_id = match.in_key;
205 char *post_flow_str = flow_to_string(flow);
206 char *tnl_str = tnl_port_fmt(tnl_port);
207 VLOG_DBG("flow received\n"
211 tnl_str, pre_flow_str, post_flow_str);
216 return tnl_port->ofport;
219 /* Given that 'flow' should be output to the ofport corresponding to
220 * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
221 * port that the output should happen on. May return OVSP_NONE if the output
222 * shouldn't occur. */
224 tnl_port_send(const struct tnl_port *tnl_port, struct flow *flow)
226 const struct netdev_tunnel_config *cfg;
227 char *pre_flow_str = NULL;
229 if (tnl_port == &void_tnl_port) {
233 cfg = netdev_get_tunnel_config(tnl_port->ofport->netdev);
236 if (!VLOG_DROP_DBG(&dbg_rl)) {
237 pre_flow_str = flow_to_string(flow);
240 flow->tunnel.ip_src = tnl_port->match.ip_src;
241 flow->tunnel.ip_dst = tnl_port->match.ip_dst;
242 flow->skb_mark = tnl_port->match.skb_mark;
244 if (!cfg->out_key_flow) {
245 flow->tunnel.tun_id = cfg->out_key;
248 if (cfg->ttl_inherit && is_ip_any(flow)) {
249 flow->tunnel.ip_ttl = flow->nw_ttl;
251 flow->tunnel.ip_ttl = cfg->ttl;
254 if (cfg->tos_inherit && is_ip_any(flow)) {
255 flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
257 flow->tunnel.ip_tos = cfg->tos;
260 if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
261 flow->tunnel.ip_tos |= IP_ECN_ECT_0;
263 flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
266 flow->tunnel.flags = (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
267 | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
268 | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
271 char *post_flow_str = flow_to_string(flow);
272 char *tnl_str = tnl_port_fmt(tnl_port);
273 VLOG_DBG("flow sent\n"
277 tnl_str, pre_flow_str, post_flow_str);
283 return tnl_port->match.odp_port;
287 tnl_hash(struct tnl_match *match)
289 BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
290 return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
293 static struct tnl_port *
294 tnl_find_exact(struct tnl_match *match)
296 struct tnl_port *tnl_port;
298 HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match),
300 if (!memcmp(match, &tnl_port->match, sizeof *match)) {
307 static struct tnl_port *
308 tnl_find(struct tnl_match *match_)
310 struct tnl_match match = *match_;
311 struct tnl_port *tnl_port;
313 /* remote_ip, local_ip, in_key */
314 tnl_port = tnl_find_exact(&match);
319 /* remote_ip, in_key */
321 tnl_port = tnl_find_exact(&match);
325 match.ip_src = match_->ip_src;
327 /* remote_ip, local_ip */
329 match.in_key_flow = true;
330 tnl_port = tnl_find_exact(&match);
337 tnl_port = tnl_find_exact(&match);
346 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
348 ds_put_format(ds, IP_FMT"->"IP_FMT, IP_ARGS(match->ip_src),
349 IP_ARGS(match->ip_dst));
351 if (match->in_key_flow) {
352 ds_put_cstr(ds, ", key=flow");
354 ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
357 ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
358 ds_put_format(ds, ", skb mark=%"PRIu32, match->skb_mark);
362 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
364 if (VLOG_IS_DBG_ENABLED()) {
365 struct ds ds = DS_EMPTY_INITIALIZER;
367 tnl_match_fmt(&tnl_port->match, &ds);
368 VLOG_INFO("%s tunnel port %s (%s)", action,
369 tnl_port_get_name(tnl_port), ds_cstr(&ds));
375 tnl_port_fmt(const struct tnl_port *tnl_port)
377 const struct netdev_tunnel_config *cfg =
378 netdev_get_tunnel_config(tnl_port->ofport->netdev);
379 struct ds ds = DS_EMPTY_INITIALIZER;
381 ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
382 tnl_port_get_name(tnl_port),
383 netdev_get_type(tnl_port->ofport->netdev));
384 tnl_match_fmt(&tnl_port->match, &ds);
386 if (cfg->out_key != cfg->in_key ||
387 cfg->out_key_present != cfg->in_key_present ||
388 cfg->out_key_flow != cfg->in_key_flow) {
389 ds_put_cstr(&ds, ", out_key=");
390 if (!cfg->out_key_present) {
391 ds_put_cstr(&ds, "none");
392 } else if (cfg->out_key_flow) {
393 ds_put_cstr(&ds, "flow");
395 ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
399 if (cfg->ttl_inherit) {
400 ds_put_cstr(&ds, ", ttl=inherit");
402 ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
405 if (cfg->tos_inherit) {
406 ds_put_cstr(&ds, ", tos=inherit");
407 } else if (cfg->tos) {
408 ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
411 if (!cfg->dont_fragment) {
412 ds_put_cstr(&ds, ", df=false");
416 ds_put_cstr(&ds, ", csum=true");
419 ds_put_cstr(&ds, ")\n");
421 return ds_steal_cstr(&ds);
425 tnl_port_get_name(const struct tnl_port *tnl_port)
427 return netdev_get_name(tnl_port->ofport->netdev);