datapath: Merge vport "attach" into "create" and "detach" into "destroy".
[sliver-openvswitch.git] / datapath / vport-patch.c
1 /*
2  * Copyright (c) 2010 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include <linux/dcache.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12
13 #include "datapath.h"
14 #include "vport.h"
15 #include "vport-generic.h"
16
17 struct device_config {
18         struct rcu_head rcu;
19
20         unsigned char eth_addr[ETH_ALEN];
21         unsigned int mtu;
22 };
23
24 struct patch_vport {
25         char name[IFNAMSIZ];
26
27         /* Protected by RTNL lock. */
28         char peer_name[IFNAMSIZ];
29         struct hlist_node hash_node;
30
31         /* Protected by RCU. */
32         struct vport *peer;
33
34         /* Protected by RCU. */
35         struct device_config *devconf;
36 };
37
38 /* Protected by RTNL lock. */
39 static struct hlist_head *peer_table;
40 #define PEER_HASH_BUCKETS 256
41
42 static void update_peers(const char *name, struct vport *);
43
44 static inline struct patch_vport *patch_vport_priv(const struct vport *vport)
45 {
46         return vport_priv(vport);
47 }
48
49 /* RCU callback. */
50 static void free_config(struct rcu_head *rcu)
51 {
52         struct device_config *c = container_of(rcu, struct device_config, rcu);
53         kfree(c);
54 }
55
56 static void assign_config_rcu(struct vport *vport,
57                               struct device_config *new_config)
58 {
59         struct patch_vport *patch_vport = patch_vport_priv(vport);
60         struct device_config *old_config;
61
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);
65 }
66
67 static struct hlist_head *hash_bucket(const char *name)
68 {
69         unsigned int hash = full_name_hash(name, strlen(name));
70         return &peer_table[hash & (PEER_HASH_BUCKETS - 1)];
71 }
72
73 static int patch_init(void)
74 {
75         peer_table = kzalloc(PEER_HASH_BUCKETS * sizeof(struct hlist_head),
76                             GFP_KERNEL);
77         if (!peer_table)
78                 return -ENOMEM;
79
80         return 0;
81 }
82
83 static void patch_exit(void)
84 {
85         kfree(peer_table);
86 }
87
88 static int set_config(struct vport *vport, const void *config)
89 {
90         struct patch_vport *patch_vport = patch_vport_priv(vport);
91         char peer_name[IFNAMSIZ];
92
93         strlcpy(peer_name, config, IFNAMSIZ);
94
95         if (!strcmp(patch_vport->name, peer_name))
96                 return -EINVAL;
97
98         strcpy(patch_vport->peer_name, peer_name);
99
100         return 0;
101 }
102
103 static struct vport *patch_create(const struct vport_parms *parms)
104 {
105         struct vport *vport;
106         struct patch_vport *patch_vport;
107         int err;
108
109         vport = vport_alloc(sizeof(struct patch_vport), &patch_vport_ops, parms);
110         if (IS_ERR(vport)) {
111                 err = PTR_ERR(vport);
112                 goto error;
113         }
114
115         patch_vport = patch_vport_priv(vport);
116
117         strcpy(patch_vport->name, parms->name);
118
119         err = set_config(vport, parms->config);
120         if (err)
121                 goto error_free_vport;
122
123         patch_vport->devconf = kmalloc(sizeof(struct device_config), GFP_KERNEL);
124         if (!patch_vport->devconf) {
125                 err = -ENOMEM;
126                 goto error_free_vport;
127         }
128
129         vport_gen_rand_ether_addr(patch_vport->devconf->eth_addr);
130
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;
134
135         hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
136
137         rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
138         update_peers(patch_vport->name, vport);
139
140         return vport;
141
142 error_free_vport:
143         vport_free(vport);
144 error:
145         return ERR_PTR(err);
146 }
147
148 static int patch_modify(struct vport *vport, struct odp_port *port)
149 {
150         int err = set_config(vport, port->config);
151         if (!err) {
152                 struct patch_vport *patch_vport = patch_vport_priv(vport);
153
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));
157         }
158         return err;
159 }
160
161 static int patch_destroy(struct vport *vport)
162 {
163         struct patch_vport *patch_vport = patch_vport_priv(vport);
164
165         update_peers(patch_vport->name, NULL);
166         rcu_assign_pointer(patch_vport->peer, NULL);
167
168         hlist_del(&patch_vport->hash_node);
169
170         synchronize_rcu();
171
172         kfree(patch_vport->devconf);
173         vport_free(vport);
174
175         return 0;
176 }
177
178 static void update_peers(const char *name, struct vport *vport)
179 {
180         struct hlist_head *bucket = hash_bucket(name);
181         struct patch_vport *peer_vport;
182         struct hlist_node *node;
183
184         hlist_for_each_entry(peer_vport, node, bucket, hash_node)
185                 if (!strcmp(peer_vport->peer_name, name))
186                         rcu_assign_pointer(peer_vport->peer, vport);
187 }
188
189 static int patch_set_mtu(struct vport *vport, int mtu)
190 {
191         struct patch_vport *patch_vport = patch_vport_priv(vport);
192         struct device_config *devconf;
193
194         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
195         if (!devconf)
196                 return -ENOMEM;
197
198         devconf->mtu = mtu;
199         assign_config_rcu(vport, devconf);
200
201         return 0;
202 }
203
204 static int patch_set_addr(struct vport *vport, const unsigned char *addr)
205 {
206         struct patch_vport *patch_vport = patch_vport_priv(vport);
207         struct device_config *devconf;
208
209         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
210         if (!devconf)
211                 return -ENOMEM;
212
213         memcpy(devconf->eth_addr, addr, ETH_ALEN);
214         assign_config_rcu(vport, devconf);
215
216         return 0;
217 }
218
219
220 static const char *patch_get_name(const struct vport *vport)
221 {
222         const struct patch_vport *patch_vport = patch_vport_priv(vport);
223         return patch_vport->name;
224 }
225
226 static const unsigned char *patch_get_addr(const struct vport *vport)
227 {
228         const struct patch_vport *patch_vport = patch_vport_priv(vport);
229         return rcu_dereference(patch_vport->devconf)->eth_addr;
230 }
231
232 static int patch_get_mtu(const struct vport *vport)
233 {
234         const struct patch_vport *patch_vport = patch_vport_priv(vport);
235         return rcu_dereference(patch_vport->devconf)->mtu;
236 }
237
238 static int patch_send(struct vport *vport, struct sk_buff *skb)
239 {
240         struct patch_vport *patch_vport = patch_vport_priv(vport);
241         struct vport *peer = rcu_dereference(patch_vport->peer);
242         int skb_len = skb->len;
243
244         if (!peer) {
245                 kfree_skb(skb);
246                 vport_record_error(vport, VPORT_E_TX_DROPPED);
247
248                 return 0;
249         }
250
251         vport_receive(peer, skb);
252         return skb_len;
253 }
254
255 const struct vport_ops patch_vport_ops = {
256         .type           = "patch",
257         .flags          = VPORT_F_GEN_STATS,
258         .init           = patch_init,
259         .exit           = patch_exit,
260         .create         = patch_create,
261         .modify         = patch_modify,
262         .destroy        = patch_destroy,
263         .set_mtu        = patch_set_mtu,
264         .set_addr       = patch_set_addr,
265         .get_name       = patch_get_name,
266         .get_addr       = patch_get_addr,
267         .get_dev_flags  = vport_gen_get_dev_flags,
268         .is_running     = vport_gen_is_running,
269         .get_operstate  = vport_gen_get_operstate,
270         .get_mtu        = patch_get_mtu,
271         .send           = patch_send,
272 };