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 struct vport_ops patch_vport_ops
;
40 /* Protected by RTNL lock. */
41 static struct hlist_head
*peer_table
;
42 #define PEER_HASH_BUCKETS 256
44 static inline struct patch_vport
*
45 patch_vport_priv(const struct vport
*vport
)
47 return vport_priv(vport
);
52 free_config(struct rcu_head
*rcu
)
54 struct device_config
*c
= container_of(rcu
, struct device_config
, rcu
);
59 assign_config_rcu(struct vport
*vport
, struct device_config
*new_config
)
61 struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
62 struct device_config
*old_config
;
64 old_config
= rcu_dereference(patch_vport
->devconf
);
65 rcu_assign_pointer(patch_vport
->devconf
, new_config
);
66 call_rcu(&old_config
->rcu
, free_config
);
69 static struct hlist_head
*
70 hash_bucket(const char *name
)
72 unsigned int hash
= full_name_hash(name
, strlen(name
));
73 return &peer_table
[hash
& (PEER_HASH_BUCKETS
- 1)];
79 peer_table
= kzalloc(PEER_HASH_BUCKETS
* sizeof(struct hlist_head
),
94 set_config(struct vport
*vport
, const void __user
*uconfig
)
96 struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
97 char peer_name
[IFNAMSIZ
];
100 retval
= strncpy_from_user(peer_name
, uconfig
, IFNAMSIZ
);
103 else if (retval
>= IFNAMSIZ
)
104 return -ENAMETOOLONG
;
106 if (!strcmp(patch_vport
->name
, peer_name
))
109 strcpy(patch_vport
->peer_name
, peer_name
);
111 if (vport_get_dp_port(vport
)) {
112 hlist_del(&patch_vport
->hash_node
);
113 rcu_assign_pointer(patch_vport
->peer
, vport_locate(patch_vport
->peer_name
));
114 hlist_add_head(&patch_vport
->hash_node
, hash_bucket(patch_vport
->peer_name
));
120 static struct vport
*
121 patch_create(const char *name
, const void __user
*config
)
124 struct patch_vport
*patch_vport
;
127 vport
= vport_alloc(sizeof(struct patch_vport
), &patch_vport_ops
);
129 err
= PTR_ERR(vport
);
133 patch_vport
= patch_vport_priv(vport
);
135 strcpy(patch_vport
->name
, name
);
137 err
= set_config(vport
, config
);
139 goto error_free_vport
;
141 patch_vport
->devconf
= kmalloc(sizeof(struct device_config
), GFP_KERNEL
);
142 if (!patch_vport
->devconf
) {
144 goto error_free_vport
;
147 vport_gen_rand_ether_addr(patch_vport
->devconf
->eth_addr
);
148 patch_vport
->devconf
->mtu
= ETH_DATA_LEN
;
159 patch_modify(struct vport
*vport
, const void __user
*config
)
161 return set_config(vport
, config
);
165 patch_destroy(struct vport
*vport
)
167 struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
169 kfree(patch_vport
->devconf
);
176 update_peers(const char *name
, struct vport
*vport
)
178 struct hlist_head
*bucket
= hash_bucket(name
);
179 struct patch_vport
*peer_vport
;
180 struct hlist_node
*node
;
182 hlist_for_each_entry(peer_vport
, node
, bucket
, hash_node
)
183 if (!strcmp(peer_vport
->peer_name
, name
))
184 rcu_assign_pointer(peer_vport
->peer
, vport
);
188 patch_attach(struct vport
*vport
)
190 struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
192 hlist_add_head(&patch_vport
->hash_node
, hash_bucket(patch_vport
->peer_name
));
194 rcu_assign_pointer(patch_vport
->peer
, vport_locate(patch_vport
->peer_name
));
195 update_peers(patch_vport
->name
, vport
);
201 patch_detach(struct vport
*vport
)
203 struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
205 update_peers(patch_vport
->name
, NULL
);
206 rcu_assign_pointer(patch_vport
->peer
, NULL
);
208 hlist_del(&patch_vport
->hash_node
);
214 patch_set_mtu(struct vport
*vport
, int mtu
)
216 struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
217 struct device_config
*devconf
;
219 devconf
= kmemdup(patch_vport
->devconf
, sizeof(struct device_config
), GFP_KERNEL
);
224 assign_config_rcu(vport
, devconf
);
230 patch_set_addr(struct vport
*vport
, const unsigned char *addr
)
232 struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
233 struct device_config
*devconf
;
235 devconf
= kmemdup(patch_vport
->devconf
, sizeof(struct device_config
), GFP_KERNEL
);
239 memcpy(devconf
->eth_addr
, addr
, ETH_ALEN
);
240 assign_config_rcu(vport
, devconf
);
247 patch_get_name(const struct vport
*vport
)
249 const struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
250 return patch_vport
->name
;
253 static const unsigned char *
254 patch_get_addr(const struct vport
*vport
)
256 const struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
257 return rcu_dereference(patch_vport
->devconf
)->eth_addr
;
261 patch_get_mtu(const struct vport
*vport
)
263 const struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
264 return rcu_dereference(patch_vport
->devconf
)->mtu
;
268 patch_send(struct vport
*vport
, struct sk_buff
*skb
)
270 struct patch_vport
*patch_vport
= patch_vport_priv(vport
);
271 struct vport
*peer
= rcu_dereference(patch_vport
->peer
);
272 int skb_len
= skb
->len
;
276 vport_record_error(vport
, VPORT_E_TX_DROPPED
);
281 vport_receive(peer
, skb
);
285 struct vport_ops patch_vport_ops
= {
287 .flags
= VPORT_F_GEN_STATS
,
290 .create
= patch_create
,
291 .modify
= patch_modify
,
292 .destroy
= patch_destroy
,
293 .attach
= patch_attach
,
294 .detach
= patch_detach
,
295 .set_mtu
= patch_set_mtu
,
296 .set_addr
= patch_set_addr
,
297 .get_name
= patch_get_name
,
298 .get_addr
= patch_get_addr
,
299 .get_dev_flags
= vport_gen_get_dev_flags
,
300 .is_running
= vport_gen_is_running
,
301 .get_operstate
= vport_gen_get_operstate
,
302 .get_mtu
= patch_get_mtu
,