2 * Copyright (c) 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "tnl-ports.h"
24 #include "classifier.h"
25 #include "dynamic-string.h"
29 #include "ovs-thread.h"
31 #include "tnl-arp-cache.h"
32 #include "ovs-thread.h"
36 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
37 static struct classifier cls; /* Tunnel ports. */
43 struct in6_addr addr6;
46 char dev_name[IFNAMSIZ];
49 static struct ovs_list addr_list;
54 char dev_name[IFNAMSIZ];
58 static struct ovs_list port_list;
63 struct ovs_refcount ref_cnt;
64 char dev_name[IFNAMSIZ];
67 static struct tnl_port_in *
68 tnl_port_cast(const struct cls_rule *cr)
70 BUILD_ASSERT_DECL(offsetof(struct tnl_port_in, cr) == 0);
72 return CONTAINER_OF(cr, struct tnl_port_in, cr);
76 tnl_port_free(struct tnl_port_in *p)
78 cls_rule_destroy(&p->cr);
83 tnl_port_init_flow(struct flow *flow, struct eth_addr mac,
84 struct in6_addr *addr, ovs_be16 udp_port)
86 memset(flow, 0, sizeof *flow);
89 if (IN6_IS_ADDR_V4MAPPED(addr)) {
90 flow->dl_type = htons(ETH_TYPE_IP);
91 flow->nw_dst = in6_addr_get_mapped_ipv4(addr);
93 flow->dl_type = htons(ETH_TYPE_IPV6);
94 flow->ipv6_dst = *addr;
98 flow->nw_proto = IPPROTO_UDP;
100 flow->nw_proto = IPPROTO_GRE;
102 flow->tp_dst = udp_port;
106 map_insert(odp_port_t port, struct eth_addr mac, struct in6_addr *addr,
107 ovs_be16 udp_port, const char dev_name[])
109 const struct cls_rule *cr;
110 struct tnl_port_in *p;
113 memset(&match, 0, sizeof match);
114 tnl_port_init_flow(&match.flow, mac, addr, udp_port);
117 cr = classifier_lookup(&cls, CLS_MAX_VERSION, &match.flow, NULL);
118 p = tnl_port_cast(cr);
119 /* Try again if the rule was released before we get the reference. */
120 } while (p && !ovs_refcount_try_ref_rcu(&p->ref_cnt));
123 p = xzalloc(sizeof *p);
126 match.wc.masks.dl_type = OVS_BE16_MAX;
127 match.wc.masks.nw_proto = 0xff;
128 match.wc.masks.nw_frag = 0xff; /* XXX: No fragments support. */
129 match.wc.masks.tp_dst = OVS_BE16_MAX;
130 if (IN6_IS_ADDR_V4MAPPED(addr)) {
131 match.wc.masks.nw_dst = OVS_BE32_MAX;
133 match.wc.masks.ipv6_dst = in6addr_exact;
135 match.wc.masks.vlan_tci = OVS_BE16_MAX;
136 memset(&match.wc.masks.dl_dst, 0xff, sizeof (struct eth_addr));
138 cls_rule_init(&p->cr, &match, 0); /* Priority == 0. */
139 ovs_refcount_init(&p->ref_cnt);
140 ovs_strlcpy(p->dev_name, dev_name, sizeof p->dev_name);
142 classifier_insert(&cls, &p->cr, CLS_MIN_VERSION, NULL, 0);
147 tnl_port_map_insert(odp_port_t port,
148 ovs_be16 udp_port, const char dev_name[])
151 struct ip_device *ip_dev;
153 ovs_mutex_lock(&mutex);
154 LIST_FOR_EACH(p, node, &port_list) {
155 if (udp_port == p->udp_port) {
160 p = xzalloc(sizeof *p);
162 p->udp_port = udp_port;
163 ovs_strlcpy(p->dev_name, dev_name, sizeof p->dev_name);
164 list_insert(&port_list, &p->node);
166 LIST_FOR_EACH(ip_dev, node, &addr_list) {
167 if (ip_dev->addr4 != INADDR_ANY) {
168 struct in6_addr addr4;
169 in6_addr_set_mapped_ipv4(&addr4, ip_dev->addr4);
170 map_insert(p->port, ip_dev->mac, &addr4,
171 p->udp_port, p->dev_name);
173 if (ipv6_addr_is_set(&ip_dev->addr6)) {
174 map_insert(p->port, ip_dev->mac, &ip_dev->addr6,
175 p->udp_port, p->dev_name);
180 ovs_mutex_unlock(&mutex);
184 tnl_port_unref(const struct cls_rule *cr)
186 struct tnl_port_in *p = tnl_port_cast(cr);
188 if (cr && ovs_refcount_unref_relaxed(&p->ref_cnt) == 1) {
189 if (classifier_remove(&cls, cr)) {
190 ovsrcu_postpone(tnl_port_free, p);
196 map_delete(struct eth_addr mac, struct in6_addr *addr, ovs_be16 udp_port)
198 const struct cls_rule *cr;
201 tnl_port_init_flow(&flow, mac, addr, udp_port);
203 cr = classifier_lookup(&cls, CLS_MAX_VERSION, &flow, NULL);
208 tnl_port_map_delete(ovs_be16 udp_port)
210 struct tnl_port *p, *next;
211 struct ip_device *ip_dev;
214 ovs_mutex_lock(&mutex);
215 LIST_FOR_EACH_SAFE(p, next, node, &port_list) {
216 if (p->udp_port == udp_port) {
217 list_remove(&p->node);
226 LIST_FOR_EACH(ip_dev, node, &addr_list) {
227 if (ip_dev->addr4 != INADDR_ANY) {
228 struct in6_addr addr4;
229 in6_addr_set_mapped_ipv4(&addr4, ip_dev->addr4);
230 map_delete(ip_dev->mac, &addr4, udp_port);
232 if (ipv6_addr_is_set(&ip_dev->addr6)) {
233 map_delete(ip_dev->mac, &ip_dev->addr6, udp_port);
239 ovs_mutex_unlock(&mutex);
242 /* 'flow' is non-const to allow for temporary modifications during the lookup.
243 * Any changes are restored before returning. */
245 tnl_port_map_lookup(struct flow *flow, struct flow_wildcards *wc)
247 const struct cls_rule *cr = classifier_lookup(&cls, CLS_MAX_VERSION, flow,
250 return (cr) ? tnl_port_cast(cr)->portno : ODPP_NONE;
254 tnl_port_show_v(struct ds *ds)
256 const struct tnl_port_in *p;
258 CLS_FOR_EACH(p, cr, &cls) {
259 struct odputil_keybuf keybuf;
260 struct odputil_keybuf maskbuf;
262 const struct nlattr *key, *mask;
263 size_t key_len, mask_len;
264 struct flow_wildcards wc;
266 struct odp_flow_key_parms odp_parms = {
271 ds_put_format(ds, "%s (%"PRIu32") : ", p->dev_name, p->portno);
272 minimask_expand(p->cr.match.mask, &wc);
273 miniflow_expand(p->cr.match.flow, &flow);
276 odp_parms.odp_in_port = flow.in_port.odp_port;
277 odp_parms.support.recirc = true;
278 ofpbuf_use_stack(&buf, &keybuf, sizeof keybuf);
279 odp_flow_key_from_flow(&odp_parms, &buf);
284 odp_parms.odp_in_port = wc.masks.in_port.odp_port;
285 odp_parms.support.recirc = false;
286 ofpbuf_use_stack(&buf, &maskbuf, sizeof maskbuf);
287 odp_flow_key_from_mask(&odp_parms, &buf);
292 odp_flow_format(key, key_len, mask, mask_len, NULL, ds, false);
293 ds_put_format(ds, "\n");
298 tnl_port_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
299 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
301 struct ds ds = DS_EMPTY_INITIALIZER;
304 ds_put_format(&ds, "Listening ports:\n");
305 ovs_mutex_lock(&mutex);
307 if (!strcasecmp(argv[1], "-v")) {
308 tnl_port_show_v(&ds);
313 LIST_FOR_EACH(p, node, &port_list) {
314 ds_put_format(&ds, "%s (%"PRIu32")\n", p->dev_name, p->port);
318 ovs_mutex_unlock(&mutex);
319 unixctl_command_reply(conn, ds_cstr(&ds));
324 map_insert_ipdev(struct ip_device *ip_dev)
328 LIST_FOR_EACH(p, node, &port_list) {
329 if (ip_dev->addr4 != INADDR_ANY) {
330 struct in6_addr addr4;
331 in6_addr_set_mapped_ipv4(&addr4, ip_dev->addr4);
332 map_insert(p->port, ip_dev->mac, &addr4,
333 p->udp_port, p->dev_name);
335 if (ipv6_addr_is_set(&ip_dev->addr6)) {
336 map_insert(p->port, ip_dev->mac, &ip_dev->addr6,
337 p->udp_port, p->dev_name);
343 insert_ipdev(const char dev_name[])
345 struct ip_device *ip_dev;
346 enum netdev_flags flags;
351 error = netdev_open(dev_name, NULL, &dev);
356 error = netdev_get_flags(dev, &flags);
357 if (error || (flags & NETDEV_LOOPBACK)) {
362 ip_dev = xzalloc(sizeof *ip_dev);
364 ip_dev->change_seq = netdev_get_change_seq(dev);
365 error = netdev_get_etheraddr(ip_dev->dev, &ip_dev->mac);
370 error4 = netdev_get_in4(ip_dev->dev, (struct in_addr *)&ip_dev->addr4, NULL);
371 error6 = netdev_get_in6(ip_dev->dev, &ip_dev->addr6);
372 if (error4 && error6) {
376 ovs_strlcpy(ip_dev->dev_name, netdev_get_name(dev), sizeof ip_dev->dev_name);
378 list_insert(&addr_list, &ip_dev->node);
379 map_insert_ipdev(ip_dev);
383 delete_ipdev(struct ip_device *ip_dev)
387 LIST_FOR_EACH(p, node, &port_list) {
388 if (ip_dev->addr4 != INADDR_ANY) {
389 struct in6_addr addr4;
390 in6_addr_set_mapped_ipv4(&addr4, ip_dev->addr4);
391 map_delete(ip_dev->mac, &addr4, p->udp_port);
393 if (ipv6_addr_is_set(&ip_dev->addr6)) {
394 map_delete(ip_dev->mac, &ip_dev->addr6, p->udp_port);
398 list_remove(&ip_dev->node);
399 netdev_close(ip_dev->dev);
404 tnl_port_map_insert_ipdev(const char dev_name[])
406 struct ip_device *ip_dev, *next;
408 ovs_mutex_lock(&mutex);
410 LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
411 if (!strcmp(netdev_get_name(ip_dev->dev), dev_name)) {
412 if (ip_dev->change_seq == netdev_get_change_seq(ip_dev->dev)) {
415 /* Address changed. */
416 delete_ipdev(ip_dev);
420 insert_ipdev(dev_name);
423 ovs_mutex_unlock(&mutex);
427 tnl_port_map_delete_ipdev(const char dev_name[])
429 struct ip_device *ip_dev, *next;
431 ovs_mutex_lock(&mutex);
432 LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
433 if (!strcmp(netdev_get_name(ip_dev->dev), dev_name)) {
434 delete_ipdev(ip_dev);
437 ovs_mutex_unlock(&mutex);
441 tnl_port_map_run(void)
443 struct ip_device *ip_dev, *next;
445 ovs_mutex_lock(&mutex);
446 LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
447 char dev_name[IFNAMSIZ];
449 if (ip_dev->change_seq == netdev_get_change_seq(ip_dev->dev)) {
453 /* Address changed. */
454 ovs_strlcpy(dev_name, ip_dev->dev_name, sizeof dev_name);
455 delete_ipdev(ip_dev);
456 insert_ipdev(dev_name);
458 ovs_mutex_unlock(&mutex);
462 tnl_port_map_init(void)
464 classifier_init(&cls, flow_segment_u64s);
465 list_init(&addr_list);
466 list_init(&port_list);
467 unixctl_command_register("tnl/ports/show", "-v", 0, 1, tnl_port_show, NULL);