]> git.proxmox.com Git - mirror_ovs.git/blob - lib/tnl-neigh-cache.c
1ded169315e56bd6c1e6e90378bdf6e3b098c775
[mirror_ovs.git] / lib / tnl-neigh-cache.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
19 #include "tnl-neigh-cache.h"
20
21 #include <inttypes.h>
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
25 #include <stdlib.h>
26
27 #include "bitmap.h"
28 #include "cmap.h"
29 #include "coverage.h"
30 #include "dpif-netdev.h"
31 #include "dynamic-string.h"
32 #include "errno.h"
33 #include "flow.h"
34 #include "netdev.h"
35 #include "ovs-thread.h"
36 #include "packets.h"
37 #include "poll-loop.h"
38 #include "seq.h"
39 #include "socket-util.h"
40 #include "timeval.h"
41 #include "unaligned.h"
42 #include "unixctl.h"
43 #include "util.h"
44 #include "openvswitch/vlog.h"
45
46
47 /* In seconds */
48 #define NEIGH_ENTRY_DEFAULT_IDLE_TIME (15 * 60)
49
50 struct tnl_neigh_entry {
51 struct cmap_node cmap_node;
52 struct in6_addr ip;
53 struct eth_addr mac;
54 time_t expires; /* Expiration time. */
55 char br_name[IFNAMSIZ];
56 };
57
58 static struct cmap table;
59 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
60
61 static uint32_t
62 tnl_neigh_hash(const struct in6_addr *ip)
63 {
64 return hash_bytes(ip->s6_addr, 16, 0);
65 }
66
67 static struct tnl_neigh_entry *
68 tnl_neigh_lookup__(const char br_name[IFNAMSIZ], const struct in6_addr *dst)
69 {
70 struct tnl_neigh_entry *neigh;
71 uint32_t hash;
72
73 hash = tnl_neigh_hash(dst);
74 CMAP_FOR_EACH_WITH_HASH (neigh, cmap_node, hash, &table) {
75 if (ipv6_addr_equals(&neigh->ip, dst) && !strcmp(neigh->br_name, br_name)) {
76 neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
77 return neigh;
78 }
79 }
80 return NULL;
81 }
82
83 int
84 tnl_arp_lookup(const char br_name[IFNAMSIZ], ovs_be32 dst,
85 struct eth_addr *mac)
86 {
87 struct tnl_neigh_entry *neigh;
88 int res = ENOENT;
89 struct in6_addr dst6 = in6_addr_mapped_ipv4(dst);
90
91 neigh = tnl_neigh_lookup__(br_name, &dst6);
92 if (neigh) {
93 *mac = neigh->mac;
94 res = 0;
95 }
96
97 return res;
98 }
99
100 int
101 tnl_neigh_lookup(const char br_name[IFNAMSIZ], const struct in6_addr *dst,
102 struct eth_addr *mac)
103 {
104 struct tnl_neigh_entry *neigh;
105 int res = ENOENT;
106
107 neigh = tnl_neigh_lookup__(br_name, dst);
108 if (neigh) {
109 *mac = neigh->mac;
110 res = 0;
111 }
112 return res;
113 }
114
115 static void
116 neigh_entry_free(struct tnl_neigh_entry *neigh)
117 {
118 free(neigh);
119 }
120
121 static void
122 tnl_neigh_delete(struct tnl_neigh_entry *neigh)
123 {
124 uint32_t hash = tnl_neigh_hash(&neigh->ip);
125 cmap_remove(&table, &neigh->cmap_node, hash);
126 ovsrcu_postpone(neigh_entry_free, neigh);
127 }
128
129 static void
130 tnl_neigh_set__(const char name[IFNAMSIZ], const struct in6_addr *dst,
131 const struct eth_addr mac)
132 {
133 ovs_mutex_lock(&mutex);
134 struct tnl_neigh_entry *neigh = tnl_neigh_lookup__(name, dst);
135 if (neigh) {
136 if (eth_addr_equals(neigh->mac, mac)) {
137 neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
138 ovs_mutex_unlock(&mutex);
139 return;
140 }
141 tnl_neigh_delete(neigh);
142 seq_change(tnl_conf_seq);
143 }
144
145 neigh = xmalloc(sizeof *neigh);
146
147 neigh->ip = *dst;
148 neigh->mac = mac;
149 neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
150 ovs_strlcpy(neigh->br_name, name, sizeof neigh->br_name);
151 cmap_insert(&table, &neigh->cmap_node, tnl_neigh_hash(&neigh->ip));
152 ovs_mutex_unlock(&mutex);
153 }
154
155 static void
156 tnl_arp_set(const char name[IFNAMSIZ], ovs_be32 dst,
157 const struct eth_addr mac)
158 {
159 struct in6_addr dst6 = in6_addr_mapped_ipv4(dst);
160 tnl_neigh_set__(name, &dst6, mac);
161 }
162
163 static int
164 tnl_arp_snoop(const struct flow *flow, struct flow_wildcards *wc,
165 const char name[IFNAMSIZ])
166 {
167 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
168 return EINVAL;
169 }
170
171 /* Exact Match on all ARP flows. */
172 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
173 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
174 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
175
176 tnl_arp_set(name, flow->nw_src, flow->arp_sha);
177 return 0;
178 }
179
180 static int
181 tnl_nd_snoop(const struct flow *flow, struct flow_wildcards *wc,
182 const char name[IFNAMSIZ])
183 {
184 if (flow->dl_type != htons(ETH_TYPE_IPV6) ||
185 flow->nw_proto != IPPROTO_ICMPV6 ||
186 flow->tp_dst != htons(0) ||
187 flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
188 return EINVAL;
189 }
190
191 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
192 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
193 memset(&wc->masks.nd_target, 0xff, sizeof wc->masks.nd_target);
194 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
195
196 tnl_neigh_set__(name, &flow->nd_target, flow->arp_tha);
197 return 0;
198 }
199
200 int
201 tnl_neigh_snoop(const struct flow *flow, struct flow_wildcards *wc,
202 const char name[IFNAMSIZ])
203 {
204 int res;
205 res = tnl_arp_snoop(flow, wc, name);
206 if (res != EINVAL) {
207 return res;
208 }
209 return tnl_nd_snoop(flow, wc, name);
210 }
211
212 void
213 tnl_neigh_cache_run(void)
214 {
215 struct tnl_neigh_entry *neigh;
216 bool changed = false;
217
218 ovs_mutex_lock(&mutex);
219 CMAP_FOR_EACH(neigh, cmap_node, &table) {
220 if (neigh->expires <= time_now()) {
221 tnl_neigh_delete(neigh);
222 changed = true;
223 }
224 }
225 ovs_mutex_unlock(&mutex);
226
227 if (changed) {
228 seq_change(tnl_conf_seq);
229 }
230 }
231
232 static void
233 tnl_neigh_cache_flush(struct unixctl_conn *conn, int argc OVS_UNUSED,
234 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
235 {
236 struct tnl_neigh_entry *neigh;
237 bool changed = false;
238
239 ovs_mutex_lock(&mutex);
240 CMAP_FOR_EACH(neigh, cmap_node, &table) {
241 tnl_neigh_delete(neigh);
242 changed = true;
243 }
244 ovs_mutex_unlock(&mutex);
245 if (changed) {
246 seq_change(tnl_conf_seq);
247 }
248 unixctl_command_reply(conn, "OK");
249 }
250
251 static int
252 lookup_any(const char *host_name, struct in6_addr *address)
253 {
254 if (addr_is_ipv6(host_name)) {
255 return lookup_ipv6(host_name, address);
256 } else {
257 int r;
258 struct in_addr ip;
259 r = lookup_ip(host_name, &ip);
260 if (r == 0) {
261 in6_addr_set_mapped_ipv4(address, ip.s_addr);
262 }
263 return r;
264 }
265 return ENOENT;
266 }
267
268 static void
269 tnl_neigh_cache_add(struct unixctl_conn *conn, int argc OVS_UNUSED,
270 const char *argv[], void *aux OVS_UNUSED)
271 {
272 const char *br_name = argv[1];
273 struct eth_addr mac;
274 struct in6_addr ip6;
275
276 if (lookup_any(argv[2], &ip6) != 0) {
277 unixctl_command_reply_error(conn, "bad IP address");
278 return;
279 }
280
281 if (!eth_addr_from_string(argv[3], &mac)) {
282 unixctl_command_reply_error(conn, "bad MAC address");
283 return;
284 }
285
286 tnl_neigh_set__(br_name, &ip6, mac);
287 unixctl_command_reply(conn, "OK");
288 }
289
290 static void
291 tnl_neigh_cache_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
292 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
293 {
294 struct ds ds = DS_EMPTY_INITIALIZER;
295 struct tnl_neigh_entry *neigh;
296
297 ds_put_cstr(&ds, "IP MAC Bridge\n");
298 ds_put_cstr(&ds, "==========================================================================\n");
299 ovs_mutex_lock(&mutex);
300 CMAP_FOR_EACH(neigh, cmap_node, &table) {
301 int start_len, need_ws;
302
303 start_len = ds.length;
304 ipv6_format_mapped(&neigh->ip, &ds);
305
306 need_ws = INET6_ADDRSTRLEN - (ds.length - start_len);
307 ds_put_char_multiple(&ds, ' ', need_ws);
308
309 ds_put_format(&ds, ETH_ADDR_FMT" %s\n",
310 ETH_ADDR_ARGS(neigh->mac), neigh->br_name);
311
312 }
313 ovs_mutex_unlock(&mutex);
314 unixctl_command_reply(conn, ds_cstr(&ds));
315 ds_destroy(&ds);
316 }
317
318 void
319 tnl_neigh_cache_init(void)
320 {
321 cmap_init(&table);
322
323 unixctl_command_register("tnl/arp/show", "", 0, 0, tnl_neigh_cache_show, NULL);
324 unixctl_command_register("tnl/arp/set", "BRIDGE IP MAC", 3, 3, tnl_neigh_cache_add, NULL);
325 unixctl_command_register("tnl/arp/flush", "", 0, 0, tnl_neigh_cache_flush, NULL);
326 unixctl_command_register("tnl/neigh/show", "", 0, 0, tnl_neigh_cache_show, NULL);
327 unixctl_command_register("tnl/neigh/set", "BRIDGE IP MAC", 3, 3, tnl_neigh_cache_add, NULL);
328 unixctl_command_register("tnl/neigh/flush", "", 0, 0, tnl_neigh_cache_flush, NULL);
329 }