2 * Copyright (c) 2010, 2011 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 #include <linux/dcache.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/rtnetlink.h>
17 #include "vport-generic.h"
22 char peer_name[IFNAMSIZ];
23 unsigned char eth_addr[ETH_ALEN];
31 /* Protected by RTNL lock. */
32 struct hlist_node hash_node;
34 struct vport __rcu *peer;
35 struct patch_config __rcu *patchconf;
38 /* Protected by RTNL lock. */
39 static struct hlist_head *peer_table;
40 #define PEER_HASH_BUCKETS 256
42 static void update_peers(const char *name, struct vport *);
44 static inline struct patch_vport *patch_vport_priv(const struct vport *vport)
46 return vport_priv(vport);
50 static void free_config(struct rcu_head *rcu)
52 struct patch_config *c = container_of(rcu, struct patch_config, rcu);
56 static void assign_config_rcu(struct vport *vport,
57 struct patch_config *new_config)
59 struct patch_vport *patch_vport = patch_vport_priv(vport);
60 struct patch_config *old_config;
62 old_config = rtnl_dereference(patch_vport->patchconf);
63 rcu_assign_pointer(patch_vport->patchconf, new_config);
64 call_rcu(&old_config->rcu, free_config);
67 static struct hlist_head *hash_bucket(const char *name)
69 unsigned int hash = full_name_hash(name, strlen(name));
70 return &peer_table[hash & (PEER_HASH_BUCKETS - 1)];
73 static int patch_init(void)
75 peer_table = kzalloc(PEER_HASH_BUCKETS * sizeof(struct hlist_head),
83 static void patch_exit(void)
88 static const struct nla_policy patch_policy[ODP_PATCH_ATTR_MAX + 1] = {
89 #ifdef HAVE_NLA_NUL_STRING
90 [ODP_PATCH_ATTR_PEER] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
94 static int patch_set_config(struct vport *vport, const struct nlattr *options,
95 struct patch_config *patchconf)
97 struct patch_vport *patch_vport = patch_vport_priv(vport);
98 struct nlattr *a[ODP_PATCH_ATTR_MAX + 1];
99 const char *peer_name;
105 err = nla_parse_nested(a, ODP_PATCH_ATTR_MAX, options, patch_policy);
109 if (!a[ODP_PATCH_ATTR_PEER] ||
110 CHECK_NUL_STRING(a[ODP_PATCH_ATTR_PEER], IFNAMSIZ - 1))
113 peer_name = nla_data(a[ODP_PATCH_ATTR_PEER]);
114 if (!strcmp(patch_vport->name, peer_name))
117 strcpy(patchconf->peer_name, peer_name);
122 static struct vport *patch_create(const struct vport_parms *parms)
125 struct patch_vport *patch_vport;
126 const char *peer_name;
127 struct patch_config *patchconf;
130 vport = vport_alloc(sizeof(struct patch_vport), &patch_vport_ops, parms);
132 err = PTR_ERR(vport);
136 patch_vport = patch_vport_priv(vport);
138 strcpy(patch_vport->name, parms->name);
140 patchconf = kmalloc(sizeof(struct patch_config), GFP_KERNEL);
143 goto error_free_vport;
146 err = patch_set_config(vport, parms->options, patchconf);
148 goto error_free_patchconf;
150 vport_gen_rand_ether_addr(patchconf->eth_addr);
152 rcu_assign_pointer(patch_vport->patchconf, patchconf);
154 peer_name = patchconf->peer_name;
155 hlist_add_head(&patch_vport->hash_node, hash_bucket(peer_name));
156 rcu_assign_pointer(patch_vport->peer, vport_locate(peer_name));
157 update_peers(patch_vport->name, vport);
161 error_free_patchconf:
169 static void free_port_rcu(struct rcu_head *rcu)
171 struct patch_vport *patch_vport = container_of(rcu,
172 struct patch_vport, rcu);
174 kfree((struct patch_config __force *)patch_vport->patchconf);
175 vport_free(vport_from_priv(patch_vport));
178 static int patch_destroy(struct vport *vport)
180 struct patch_vport *patch_vport = patch_vport_priv(vport);
182 update_peers(patch_vport->name, NULL);
183 hlist_del(&patch_vport->hash_node);
184 call_rcu(&patch_vport->rcu, free_port_rcu);
189 static int patch_set_options(struct vport *vport, struct nlattr *options)
191 struct patch_vport *patch_vport = patch_vport_priv(vport);
192 struct patch_config *patchconf;
195 patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf),
196 sizeof(struct patch_config), GFP_KERNEL);
202 err = patch_set_config(vport, options, patchconf);
206 assign_config_rcu(vport, patchconf);
208 hlist_del(&patch_vport->hash_node);
210 rcu_assign_pointer(patch_vport->peer, vport_locate(patchconf->peer_name));
211 hlist_add_head(&patch_vport->hash_node, hash_bucket(patchconf->peer_name));
221 static void update_peers(const char *name, struct vport *vport)
223 struct hlist_head *bucket = hash_bucket(name);
224 struct patch_vport *peer_vport;
225 struct hlist_node *node;
227 hlist_for_each_entry(peer_vport, node, bucket, hash_node) {
228 const char *peer_name;
230 peer_name = rtnl_dereference(peer_vport->patchconf)->peer_name;
231 if (!strcmp(peer_name, name))
232 rcu_assign_pointer(peer_vport->peer, vport);
236 static int patch_set_addr(struct vport *vport, const unsigned char *addr)
238 struct patch_vport *patch_vport = patch_vport_priv(vport);
239 struct patch_config *patchconf;
241 patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf),
242 sizeof(struct patch_config), GFP_KERNEL);
246 memcpy(patchconf->eth_addr, addr, ETH_ALEN);
247 assign_config_rcu(vport, patchconf);
253 static const char *patch_get_name(const struct vport *vport)
255 const struct patch_vport *patch_vport = patch_vport_priv(vport);
256 return patch_vport->name;
259 static const unsigned char *patch_get_addr(const struct vport *vport)
261 const struct patch_vport *patch_vport = patch_vport_priv(vport);
262 return rcu_dereference_rtnl(patch_vport->patchconf)->eth_addr;
265 static int patch_get_options(const struct vport *vport, struct sk_buff *skb)
267 struct patch_vport *patch_vport = patch_vport_priv(vport);
268 struct patch_config *patchconf = rcu_dereference_rtnl(patch_vport->patchconf);
270 return nla_put_string(skb, ODP_PATCH_ATTR_PEER, patchconf->peer_name);
273 static int patch_send(struct vport *vport, struct sk_buff *skb)
275 struct patch_vport *patch_vport = patch_vport_priv(vport);
276 struct vport *peer = rcu_dereference(patch_vport->peer);
277 int skb_len = skb->len;
281 vport_record_error(vport, VPORT_E_TX_DROPPED);
286 vport_receive(peer, skb);
290 const struct vport_ops patch_vport_ops = {
291 .type = ODP_VPORT_TYPE_PATCH,
292 .flags = VPORT_F_GEN_STATS,
295 .create = patch_create,
296 .destroy = patch_destroy,
297 .set_addr = patch_set_addr,
298 .get_name = patch_get_name,
299 .get_addr = patch_get_addr,
300 .get_options = patch_get_options,
301 .set_options = patch_set_options,
302 .get_dev_flags = vport_gen_get_dev_flags,
303 .is_running = vport_gen_is_running,
304 .get_operstate = vport_gen_get_operstate,