ovs-vsctl: Simplify code.
[cascardo/ovs.git] / lib / tnl-ports.c
1 /*
2  * Copyright (c) 2014, 2015 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include <stddef.h>
19 #include <stdint.h>
20
21 #include "classifier.h"
22 #include "dynamic-string.h"
23 #include "hash.h"
24 #include "ofpbuf.h"
25 #include "ovs-thread.h"
26 #include "odp-util.h"
27 #include "tnl-arp-cache.h"
28 #include "tnl-ports.h"
29 #include "ovs-thread.h"
30 #include "unixctl.h"
31 #include "util.h"
32
33 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
34 static struct classifier cls;   /* Tunnel ports. */
35
36 struct tnl_port_in {
37     struct cls_rule cr;
38     odp_port_t portno;
39     struct ovs_refcount ref_cnt;
40     char dev_name[IFNAMSIZ];
41 };
42
43 static struct tnl_port_in *
44 tnl_port_cast(const struct cls_rule *cr)
45 {
46     BUILD_ASSERT_DECL(offsetof(struct tnl_port_in, cr) == 0);
47
48     return CONTAINER_OF(cr, struct tnl_port_in, cr);
49 }
50
51 static void
52 tnl_port_free(struct tnl_port_in *p)
53 {
54     cls_rule_destroy(&p->cr);
55     free(p);
56 }
57
58 static void
59 tnl_port_init_flow(struct flow *flow, ovs_be32 ip_dst, ovs_be16 udp_port)
60 {
61     memset(flow, 0, sizeof *flow);
62     flow->dl_type = htons(ETH_TYPE_IP);
63     if (udp_port) {
64         flow->nw_proto = IPPROTO_UDP;
65     } else {
66         flow->nw_proto = IPPROTO_GRE;
67     }
68     flow->tp_dst = udp_port;
69     /* When matching on incoming flow from remove tnl end point,
70      * our dst ip address is source ip for them. */
71     flow->nw_src = ip_dst;
72 }
73
74 void
75 tnl_port_map_insert(odp_port_t port, ovs_be32 ip_dst, ovs_be16 udp_port,
76                     const char dev_name[])
77 {
78     const struct cls_rule *cr;
79     struct tnl_port_in *p;
80     struct match match;
81
82     memset(&match, 0, sizeof match);
83     tnl_port_init_flow(&match.flow, ip_dst, udp_port);
84
85     ovs_mutex_lock(&mutex);
86     do {
87         cr = classifier_lookup(&cls, CLS_MAX_VERSION, &match.flow, NULL);
88         p = tnl_port_cast(cr);
89         /* Try again if the rule was released before we get the reference. */
90     } while (p && !ovs_refcount_try_ref_rcu(&p->ref_cnt));
91
92     if (!p) {
93         p = xzalloc(sizeof *p);
94         p->portno = port;
95
96         match.wc.masks.dl_type = OVS_BE16_MAX;
97         match.wc.masks.nw_proto = 0xff;
98         match.wc.masks.nw_frag = 0xff;      /* XXX: No fragments support. */
99         match.wc.masks.tp_dst = OVS_BE16_MAX;
100         match.wc.masks.nw_src = OVS_BE32_MAX;
101
102         cls_rule_init(&p->cr, &match, 0, CLS_MIN_VERSION); /* Priority == 0. */
103         ovs_refcount_init(&p->ref_cnt);
104         ovs_strlcpy(p->dev_name, dev_name, sizeof p->dev_name);
105
106         classifier_insert(&cls, &p->cr, NULL, 0);
107     }
108     ovs_mutex_unlock(&mutex);
109 }
110
111 static void
112 tnl_port_unref(const struct cls_rule *cr)
113 {
114     struct tnl_port_in *p = tnl_port_cast(cr);
115
116     if (cr && ovs_refcount_unref_relaxed(&p->ref_cnt) == 1) {
117         ovs_mutex_lock(&mutex);
118         if (classifier_remove(&cls, cr)) {
119             ovsrcu_postpone(tnl_port_free, p);
120         }
121         ovs_mutex_unlock(&mutex);
122     }
123 }
124
125 void
126 tnl_port_map_delete(ovs_be32 ip_dst, ovs_be16 udp_port)
127 {
128     const struct cls_rule *cr;
129     struct flow flow;
130
131     tnl_port_init_flow(&flow, ip_dst, udp_port);
132
133     cr = classifier_lookup(&cls, CLS_MAX_VERSION, &flow, NULL);
134     tnl_port_unref(cr);
135 }
136
137 /* 'flow' is non-const to allow for temporary modifications during the lookup.
138  * Any changes are restored before returning. */
139 odp_port_t
140 tnl_port_map_lookup(struct flow *flow, struct flow_wildcards *wc)
141 {
142     const struct cls_rule *cr = classifier_lookup(&cls, CLS_MAX_VERSION, flow,
143                                                   wc);
144
145     return (cr) ? tnl_port_cast(cr)->portno : ODPP_NONE;
146 }
147
148 static void
149 tnl_port_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
150               const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
151 {
152     struct ds ds = DS_EMPTY_INITIALIZER;
153     const struct tnl_port_in *p;
154
155     ds_put_format(&ds, "Listening ports:\n");
156     CLS_FOR_EACH(p, cr, &cls) {
157         struct odputil_keybuf keybuf;
158         struct odputil_keybuf maskbuf;
159         struct flow flow;
160         const struct nlattr *key, *mask;
161         size_t key_len, mask_len;
162         struct flow_wildcards wc;
163         struct ofpbuf buf;
164         struct odp_flow_key_parms odp_parms = {
165             .flow = &flow,
166             .mask = &wc.masks,
167         };
168
169         ds_put_format(&ds, "%s (%"PRIu32") : ", p->dev_name, p->portno);
170         minimask_expand(&p->cr.match.mask, &wc);
171         miniflow_expand(&p->cr.match.flow, &flow);
172
173         /* Key. */
174         odp_parms.odp_in_port = flow.in_port.odp_port;
175         odp_parms.recirc = true;
176         ofpbuf_use_stack(&buf, &keybuf, sizeof keybuf);
177         odp_flow_key_from_flow(&odp_parms, &buf);
178         key = buf.data;
179         key_len = buf.size;
180
181         /* mask*/
182         odp_parms.odp_in_port = wc.masks.in_port.odp_port;
183         odp_parms.recirc = false;
184         ofpbuf_use_stack(&buf, &maskbuf, sizeof maskbuf);
185         odp_flow_key_from_mask(&odp_parms, &buf);
186         mask = buf.data;
187         mask_len = buf.size;
188
189         /* build string. */
190         odp_flow_format(key, key_len, mask, mask_len, NULL, &ds, false);
191         ds_put_format(&ds, "\n");
192     }
193     unixctl_command_reply(conn, ds_cstr(&ds));
194     ds_destroy(&ds);
195 }
196
197 void
198 tnl_port_map_init(void)
199 {
200     classifier_init(&cls, flow_segment_u64s);
201     unixctl_command_register("tnl/ports/show", "", 0, 0, tnl_port_show, NULL);
202 }