2 * Copyright (c) 2014 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.
24 #include "dpif-netdev.h"
25 #include "dynamic-string.h"
29 #include "ovs-thread.h"
31 #include "packet-dpif.h"
32 #include "poll-loop.h"
35 #include "tnl-arp-cache.h"
36 #include "unaligned.h"
39 #include "openvswitch/vlog.h"
43 #define ARP_ENTRY_DEFAULT_IDLE_TIME (15 * 60)
45 struct tnl_arp_entry {
46 struct cmap_node cmap_node;
48 uint8_t mac[ETH_ADDR_LEN];
49 time_t expires; /* Expiration time. */
50 char br_name[IFNAMSIZ];
53 static struct cmap table;
54 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
56 static struct tnl_arp_entry *
57 tnl_arp_lookup__(const char br_name[IFNAMSIZ], ovs_be32 dst)
59 struct tnl_arp_entry *arp;
61 CMAP_FOR_EACH_WITH_HASH (arp, cmap_node, (OVS_FORCE uint32_t) dst, &table) {
62 if (arp->ip == dst && !strcmp(arp->br_name, br_name)) {
63 arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
71 tnl_arp_lookup(const char br_name[IFNAMSIZ], ovs_be32 dst,
72 uint8_t mac[ETH_ADDR_LEN])
74 struct tnl_arp_entry *arp;
77 arp = tnl_arp_lookup__(br_name, dst);
79 memcpy(mac, arp->mac, ETH_ADDR_LEN);
87 arp_entry_free(struct tnl_arp_entry *arp)
93 tnl_arp_delete(struct tnl_arp_entry *arp)
95 cmap_remove(&table, &arp->cmap_node, (OVS_FORCE uint32_t) arp->ip);
96 ovsrcu_postpone(arp_entry_free, arp);
100 tnl_arp_snoop(const struct flow *flow, struct flow_wildcards *wc,
101 const char name[IFNAMSIZ])
103 struct tnl_arp_entry *arp;
105 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
109 /* Exact Match on all ARP flows. */
110 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
111 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
112 memset(&wc->masks.arp_sha, 0xff, sizeof wc->masks.arp_sha);
114 ovs_mutex_lock(&mutex);
115 arp = tnl_arp_lookup__(name, flow->nw_src);
117 if (!memcmp(arp->mac, flow->arp_sha, ETH_ADDR_LEN)) {
118 arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
119 ovs_mutex_unlock(&mutex);
123 seq_change(tnl_conf_seq);
126 arp = xmalloc(sizeof *arp);
128 arp->ip = flow->nw_src;
129 memcpy(arp->mac, flow->arp_sha, ETH_ADDR_LEN);
130 arp->expires = time_now() + ARP_ENTRY_DEFAULT_IDLE_TIME;
131 strncpy(arp->br_name, name, IFNAMSIZ);
132 cmap_insert(&table, &arp->cmap_node, (OVS_FORCE uint32_t) arp->ip);
133 ovs_mutex_unlock(&mutex);
138 tnl_arp_cache_run(void)
140 struct tnl_arp_entry *arp;
141 bool changed = false;
143 ovs_mutex_lock(&mutex);
144 CMAP_FOR_EACH(arp, cmap_node, &table) {
145 if (arp->expires <= time_now()) {
150 ovs_mutex_unlock(&mutex);
153 seq_change(tnl_conf_seq);
158 tnl_arp_cache_flush(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
159 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
161 struct tnl_arp_entry *arp;
162 bool changed = false;
164 ovs_mutex_lock(&mutex);
165 CMAP_FOR_EACH(arp, cmap_node, &table) {
169 ovs_mutex_unlock(&mutex);
171 seq_change(tnl_conf_seq);
173 unixctl_command_reply(conn, "OK");
176 #define MAX_IP_ADDR_LEN 17
179 tnl_arp_cache_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
180 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
182 struct ds ds = DS_EMPTY_INITIALIZER;
183 struct tnl_arp_entry *arp;
185 ds_put_cstr(&ds, "IP MAC Bridge\n");
186 ds_put_cstr(&ds, "=============================================\n");
187 ovs_mutex_lock(&mutex);
188 CMAP_FOR_EACH(arp, cmap_node, &table) {
189 int start_len, need_ws;
191 start_len = ds.length;
192 ds_put_format(&ds, IP_FMT, IP_ARGS(arp->ip));
194 need_ws = MAX_IP_ADDR_LEN - (ds.length - start_len);
195 ds_put_char_multiple(&ds, ' ', need_ws);
197 ds_put_format(&ds, ETH_ADDR_FMT" %s\n",
198 ETH_ADDR_ARGS(arp->mac), arp->br_name);
201 ovs_mutex_unlock(&mutex);
202 unixctl_command_reply(conn, ds_cstr(&ds));
207 tnl_arp_cache_init(void)
211 unixctl_command_register("tnl/arp/show", "", 0, 0, tnl_arp_cache_show, NULL);
212 unixctl_command_register("tnl/arp/flush", "", 0, 0, tnl_arp_cache_flush, NULL);