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>
12 #include <linux/rtnetlink.h>
16 #include "vport-generic.h"
18 struct device_config {
21 unsigned char eth_addr[ETH_ALEN];
30 /* Protected by RTNL lock. */
31 char peer_name[IFNAMSIZ];
32 struct hlist_node hash_node;
34 struct vport __rcu *peer;
35 struct device_config __rcu *devconf;
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 device_config *c = container_of(rcu, struct device_config, rcu);
56 static void assign_config_rcu(struct vport *vport,
57 struct device_config *new_config)
59 struct patch_vport *patch_vport = patch_vport_priv(vport);
60 struct device_config *old_config;
62 old_config = rtnl_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 *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 int set_config(struct vport *vport, const void *config)
90 struct patch_vport *patch_vport = patch_vport_priv(vport);
91 char peer_name[IFNAMSIZ];
93 strlcpy(peer_name, config, IFNAMSIZ);
95 if (!strcmp(patch_vport->name, peer_name))
98 strcpy(patch_vport->peer_name, peer_name);
103 static struct vport *patch_create(const struct vport_parms *parms)
106 struct patch_vport *patch_vport;
109 vport = vport_alloc(sizeof(struct patch_vport), &patch_vport_ops, parms);
111 err = PTR_ERR(vport);
115 patch_vport = patch_vport_priv(vport);
117 strcpy(patch_vport->name, parms->name);
119 err = set_config(vport, parms->config);
121 goto error_free_vport;
123 patch_vport->devconf = kmalloc(sizeof(struct device_config), GFP_KERNEL);
124 if (!patch_vport->devconf) {
126 goto error_free_vport;
129 vport_gen_rand_ether_addr(patch_vport->devconf->eth_addr);
131 /* Make the default MTU fairly large so that it doesn't become the
132 * bottleneck on systems using jumbo frames. */
133 patch_vport->devconf->mtu = 65535;
135 hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
137 rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
138 update_peers(patch_vport->name, vport);
148 static int patch_modify(struct vport *vport, struct odp_port *port)
150 int err = set_config(vport, port->config);
152 struct patch_vport *patch_vport = patch_vport_priv(vport);
154 hlist_del(&patch_vport->hash_node);
155 rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
156 hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
161 static void free_port_rcu(struct rcu_head *rcu)
163 struct patch_vport *patch_vport = container_of(rcu,
164 struct patch_vport, rcu);
166 kfree(patch_vport->devconf);
167 vport_free(vport_from_priv(patch_vport));
170 static int patch_destroy(struct vport *vport)
172 struct patch_vport *patch_vport = patch_vport_priv(vport);
174 update_peers(patch_vport->name, NULL);
175 hlist_del(&patch_vport->hash_node);
176 call_rcu(&patch_vport->rcu, free_port_rcu);
181 static void update_peers(const char *name, struct vport *vport)
183 struct hlist_head *bucket = hash_bucket(name);
184 struct patch_vport *peer_vport;
185 struct hlist_node *node;
187 hlist_for_each_entry(peer_vport, node, bucket, hash_node)
188 if (!strcmp(peer_vport->peer_name, name))
189 rcu_assign_pointer(peer_vport->peer, vport);
192 static int patch_set_mtu(struct vport *vport, int mtu)
194 struct patch_vport *patch_vport = patch_vport_priv(vport);
195 struct device_config *devconf;
197 devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
202 assign_config_rcu(vport, devconf);
207 static int patch_set_addr(struct vport *vport, const unsigned char *addr)
209 struct patch_vport *patch_vport = patch_vport_priv(vport);
210 struct device_config *devconf;
212 devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
216 memcpy(devconf->eth_addr, addr, ETH_ALEN);
217 assign_config_rcu(vport, devconf);
223 static const char *patch_get_name(const struct vport *vport)
225 const struct patch_vport *patch_vport = patch_vport_priv(vport);
226 return patch_vport->name;
229 static const unsigned char *patch_get_addr(const struct vport *vport)
231 const struct patch_vport *patch_vport = patch_vport_priv(vport);
232 return rcu_dereference_rtnl(patch_vport->devconf)->eth_addr;
235 static int patch_get_mtu(const struct vport *vport)
237 const struct patch_vport *patch_vport = patch_vport_priv(vport);
238 return rcu_dereference_rtnl(patch_vport->devconf)->mtu;
241 static int patch_send(struct vport *vport, struct sk_buff *skb)
243 struct patch_vport *patch_vport = patch_vport_priv(vport);
244 struct vport *peer = rcu_dereference(patch_vport->peer);
245 int skb_len = skb->len;
249 vport_record_error(vport, VPORT_E_TX_DROPPED);
254 vport_receive(peer, skb);
258 const struct vport_ops patch_vport_ops = {
260 .flags = VPORT_F_GEN_STATS,
263 .create = patch_create,
264 .modify = patch_modify,
265 .destroy = patch_destroy,
266 .set_mtu = patch_set_mtu,
267 .set_addr = patch_set_addr,
268 .get_name = patch_get_name,
269 .get_addr = patch_get_addr,
270 .get_dev_flags = vport_gen_get_dev_flags,
271 .is_running = vport_gen_is_running,
272 .get_operstate = vport_gen_get_operstate,
273 .get_mtu = patch_get_mtu,