2 * Copyright (c) 2010 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>
15 #include "vport-generic.h"
17 struct device_config {
20 unsigned char eth_addr[ETH_ALEN];
27 /* Protected by RTNL lock. */
28 char peer_name[IFNAMSIZ];
29 struct hlist_node hash_node;
31 /* Protected by RCU. */
34 /* Protected by RCU. */
35 struct device_config *devconf;
38 /* Protected by RTNL lock. */
39 static struct hlist_head *peer_table;
40 #define PEER_HASH_BUCKETS 256
42 static inline struct patch_vport *
43 patch_vport_priv(const struct vport *vport)
45 return vport_priv(vport);
50 free_config(struct rcu_head *rcu)
52 struct device_config *c = container_of(rcu, struct device_config, rcu);
57 assign_config_rcu(struct vport *vport, struct device_config *new_config)
59 struct patch_vport *patch_vport = patch_vport_priv(vport);
60 struct device_config *old_config;
62 old_config = rcu_dereference(patch_vport->devconf);
63 rcu_assign_pointer(patch_vport->devconf, new_config);
64 call_rcu(&old_config->rcu, free_config);
67 static struct hlist_head *
68 hash_bucket(const char *name)
70 unsigned int hash = full_name_hash(name, strlen(name));
71 return &peer_table[hash & (PEER_HASH_BUCKETS - 1)];
77 peer_table = kzalloc(PEER_HASH_BUCKETS * sizeof(struct hlist_head),
92 set_config(struct vport *vport, const void __user *uconfig)
94 struct patch_vport *patch_vport = patch_vport_priv(vport);
95 char peer_name[IFNAMSIZ];
98 retval = strncpy_from_user(peer_name, uconfig, IFNAMSIZ);
101 else if (retval >= IFNAMSIZ)
102 return -ENAMETOOLONG;
104 if (!strcmp(patch_vport->name, peer_name))
107 strcpy(patch_vport->peer_name, peer_name);
109 if (vport_get_dp_port(vport)) {
110 hlist_del(&patch_vport->hash_node);
111 rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
112 hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
118 static struct vport *
119 patch_create(const char *name, const void __user *config)
122 struct patch_vport *patch_vport;
125 vport = vport_alloc(sizeof(struct patch_vport), &patch_vport_ops);
127 err = PTR_ERR(vport);
131 patch_vport = patch_vport_priv(vport);
133 strcpy(patch_vport->name, name);
135 err = set_config(vport, config);
137 goto error_free_vport;
139 patch_vport->devconf = kmalloc(sizeof(struct device_config), GFP_KERNEL);
140 if (!patch_vport->devconf) {
142 goto error_free_vport;
145 vport_gen_rand_ether_addr(patch_vport->devconf->eth_addr);
146 patch_vport->devconf->mtu = ETH_DATA_LEN;
157 patch_modify(struct vport *vport, const void __user *config)
159 return set_config(vport, config);
163 patch_destroy(struct vport *vport)
165 struct patch_vport *patch_vport = patch_vport_priv(vport);
167 kfree(patch_vport->devconf);
174 update_peers(const char *name, struct vport *vport)
176 struct hlist_head *bucket = hash_bucket(name);
177 struct patch_vport *peer_vport;
178 struct hlist_node *node;
180 hlist_for_each_entry(peer_vport, node, bucket, hash_node)
181 if (!strcmp(peer_vport->peer_name, name))
182 rcu_assign_pointer(peer_vport->peer, vport);
186 patch_attach(struct vport *vport)
188 struct patch_vport *patch_vport = patch_vport_priv(vport);
190 hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
192 rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
193 update_peers(patch_vport->name, vport);
199 patch_detach(struct vport *vport)
201 struct patch_vport *patch_vport = patch_vport_priv(vport);
203 update_peers(patch_vport->name, NULL);
204 rcu_assign_pointer(patch_vport->peer, NULL);
206 hlist_del(&patch_vport->hash_node);
212 patch_set_mtu(struct vport *vport, int mtu)
214 struct patch_vport *patch_vport = patch_vport_priv(vport);
215 struct device_config *devconf;
217 devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
222 assign_config_rcu(vport, devconf);
228 patch_set_addr(struct vport *vport, const unsigned char *addr)
230 struct patch_vport *patch_vport = patch_vport_priv(vport);
231 struct device_config *devconf;
233 devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
237 memcpy(devconf->eth_addr, addr, ETH_ALEN);
238 assign_config_rcu(vport, devconf);
245 patch_get_name(const struct vport *vport)
247 const struct patch_vport *patch_vport = patch_vport_priv(vport);
248 return patch_vport->name;
251 static const unsigned char *
252 patch_get_addr(const struct vport *vport)
254 const struct patch_vport *patch_vport = patch_vport_priv(vport);
255 return rcu_dereference(patch_vport->devconf)->eth_addr;
259 patch_get_mtu(const struct vport *vport)
261 const struct patch_vport *patch_vport = patch_vport_priv(vport);
262 return rcu_dereference(patch_vport->devconf)->mtu;
266 patch_send(struct vport *vport, struct sk_buff *skb)
268 struct patch_vport *patch_vport = patch_vport_priv(vport);
269 struct vport *peer = rcu_dereference(patch_vport->peer);
270 int skb_len = skb->len;
274 vport_record_error(vport, VPORT_E_TX_DROPPED);
279 vport_receive(peer, skb);
283 struct vport_ops patch_vport_ops = {
285 .flags = VPORT_F_GEN_STATS,
288 .create = patch_create,
289 .modify = patch_modify,
290 .destroy = patch_destroy,
291 .attach = patch_attach,
292 .detach = patch_detach,
293 .set_mtu = patch_set_mtu,
294 .set_addr = patch_set_addr,
295 .get_name = patch_get_name,
296 .get_addr = patch_get_addr,
297 .get_dev_flags = vport_gen_get_dev_flags,
298 .is_running = vport_gen_is_running,
299 .get_operstate = vport_gen_get_operstate,
300 .get_mtu = patch_get_mtu,