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-neigh-cache.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
30 #include "dpif-netdev.h"
31 #include "dynamic-string.h"
35 #include "ovs-thread.h"
37 #include "poll-loop.h"
39 #include "socket-util.h"
41 #include "unaligned.h"
44 #include "openvswitch/vlog.h"
48 #define NEIGH_ENTRY_DEFAULT_IDLE_TIME (15 * 60)
50 struct tnl_neigh_entry {
51 struct cmap_node cmap_node;
54 time_t expires; /* Expiration time. */
55 char br_name[IFNAMSIZ];
58 static struct cmap table;
59 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
62 tnl_neigh_hash(const struct in6_addr *ip)
64 return hash_bytes(ip->s6_addr, 16, 0);
67 static struct tnl_neigh_entry *
68 tnl_neigh_lookup__(const char br_name[IFNAMSIZ], const struct in6_addr *dst)
70 struct tnl_neigh_entry *neigh;
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;
84 tnl_arp_lookup(const char br_name[IFNAMSIZ], ovs_be32 dst,
87 struct tnl_neigh_entry *neigh;
91 in6_addr_set_mapped_ipv4(&dst6, dst);
93 neigh = tnl_neigh_lookup__(br_name, &dst6);
103 tnl_neigh_lookup(const char br_name[IFNAMSIZ], const struct in6_addr *dst,
104 struct eth_addr *mac)
106 struct tnl_neigh_entry *neigh;
109 neigh = tnl_neigh_lookup__(br_name, dst);
118 neigh_entry_free(struct tnl_neigh_entry *neigh)
124 tnl_neigh_delete(struct tnl_neigh_entry *neigh)
126 uint32_t hash = tnl_neigh_hash(&neigh->ip);
127 cmap_remove(&table, &neigh->cmap_node, hash);
128 ovsrcu_postpone(neigh_entry_free, neigh);
132 tnl_neigh_set__(const char name[IFNAMSIZ], const struct in6_addr *dst,
133 const struct eth_addr mac)
135 ovs_mutex_lock(&mutex);
136 struct tnl_neigh_entry *neigh = tnl_neigh_lookup__(name, dst);
138 if (eth_addr_equals(neigh->mac, mac)) {
139 neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
140 ovs_mutex_unlock(&mutex);
143 tnl_neigh_delete(neigh);
144 seq_change(tnl_conf_seq);
147 neigh = xmalloc(sizeof *neigh);
151 neigh->expires = time_now() + NEIGH_ENTRY_DEFAULT_IDLE_TIME;
152 ovs_strlcpy(neigh->br_name, name, sizeof neigh->br_name);
153 cmap_insert(&table, &neigh->cmap_node, tnl_neigh_hash(&neigh->ip));
154 ovs_mutex_unlock(&mutex);
158 tnl_arp_set(const char name[IFNAMSIZ], ovs_be32 dst,
159 const struct eth_addr mac)
161 struct in6_addr dst6;
163 in6_addr_set_mapped_ipv4(&dst6, dst);
164 tnl_neigh_set__(name, &dst6, mac);
168 tnl_arp_snoop(const struct flow *flow, struct flow_wildcards *wc,
169 const char name[IFNAMSIZ])
171 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
175 /* Exact Match on all ARP flows. */
176 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
177 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
178 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
180 tnl_arp_set(name, flow->nw_src, flow->arp_sha);
185 tnl_nd_snoop(const struct flow *flow, struct flow_wildcards *wc,
186 const char name[IFNAMSIZ])
188 if (flow->dl_type != htons(ETH_TYPE_IPV6) ||
189 flow->nw_proto != IPPROTO_ICMPV6 ||
190 flow->tp_dst != htons(0) ||
191 flow->tp_src != htons(ND_NEIGHBOR_ADVERT)) {
195 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
196 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
197 memset(&wc->masks.nd_target, 0xff, sizeof wc->masks.nd_target);
198 memset(&wc->masks.arp_tha, 0xff, sizeof wc->masks.arp_tha);
200 tnl_neigh_set__(name, &flow->nd_target, flow->arp_tha);
205 tnl_neigh_snoop(const struct flow *flow, struct flow_wildcards *wc,
206 const char name[IFNAMSIZ])
209 res = tnl_arp_snoop(flow, wc, name);
213 return tnl_nd_snoop(flow, wc, name);
217 tnl_neigh_cache_run(void)
219 struct tnl_neigh_entry *neigh;
220 bool changed = false;
222 ovs_mutex_lock(&mutex);
223 CMAP_FOR_EACH(neigh, cmap_node, &table) {
224 if (neigh->expires <= time_now()) {
225 tnl_neigh_delete(neigh);
229 ovs_mutex_unlock(&mutex);
232 seq_change(tnl_conf_seq);
237 tnl_neigh_cache_flush(struct unixctl_conn *conn, int argc OVS_UNUSED,
238 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
240 struct tnl_neigh_entry *neigh;
241 bool changed = false;
243 ovs_mutex_lock(&mutex);
244 CMAP_FOR_EACH(neigh, cmap_node, &table) {
245 tnl_neigh_delete(neigh);
248 ovs_mutex_unlock(&mutex);
250 seq_change(tnl_conf_seq);
252 unixctl_command_reply(conn, "OK");
256 lookup_any(const char *host_name, struct in6_addr *address)
258 if (addr_is_ipv6(host_name)) {
259 return lookup_ipv6(host_name, address);
263 r = lookup_ip(host_name, &ip);
265 in6_addr_set_mapped_ipv4(address, ip.s_addr);
273 tnl_neigh_cache_add(struct unixctl_conn *conn, int argc OVS_UNUSED,
274 const char *argv[], void *aux OVS_UNUSED)
276 const char *br_name = argv[1];
280 if (lookup_any(argv[2], &ip6) != 0) {
281 unixctl_command_reply_error(conn, "bad IP address");
285 if (!eth_addr_from_string(argv[3], &mac)) {
286 unixctl_command_reply_error(conn, "bad MAC address");
290 tnl_neigh_set__(br_name, &ip6, mac);
291 unixctl_command_reply(conn, "OK");
295 tnl_neigh_cache_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
296 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
298 struct ds ds = DS_EMPTY_INITIALIZER;
299 struct tnl_neigh_entry *neigh;
301 ds_put_cstr(&ds, "IP MAC Bridge\n");
302 ds_put_cstr(&ds, "==========================================================================\n");
303 ovs_mutex_lock(&mutex);
304 CMAP_FOR_EACH(neigh, cmap_node, &table) {
305 int start_len, need_ws;
307 start_len = ds.length;
308 ipv6_format_mapped(&neigh->ip, &ds);
310 need_ws = INET6_ADDRSTRLEN - (ds.length - start_len);
311 ds_put_char_multiple(&ds, ' ', need_ws);
313 ds_put_format(&ds, ETH_ADDR_FMT" %s\n",
314 ETH_ADDR_ARGS(neigh->mac), neigh->br_name);
317 ovs_mutex_unlock(&mutex);
318 unixctl_command_reply(conn, ds_cstr(&ds));
323 tnl_neigh_cache_init(void)
327 unixctl_command_register("tnl/arp/show", "", 0, 0, tnl_neigh_cache_show, NULL);
328 unixctl_command_register("tnl/arp/set", "BRIDGE IP MAC", 3, 3, tnl_neigh_cache_add, NULL);
329 unixctl_command_register("tnl/arp/flush", "", 0, 0, tnl_neigh_cache_flush, NULL);
330 unixctl_command_register("tnl/neigh/show", "", 0, 0, tnl_neigh_cache_show, NULL);
331 unixctl_command_register("tnl/neigh/set", "BRIDGE IP MAC", 3, 3, tnl_neigh_cache_add, NULL);
332 unixctl_command_register("tnl/neigh/flush", "", 0, 0, tnl_neigh_cache_flush, NULL);