datapath: Increase default MTU on patch ports
[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 inline struct patch_vport *patch_vport_priv(const struct vport *vport)
43 {
44         return vport_priv(vport);
45 }
46
47 /* RCU callback. */
48 static void free_config(struct rcu_head *rcu)
49 {
50         struct device_config *c = container_of(rcu, struct device_config, rcu);
51         kfree(c);
52 }
53
54 static void assign_config_rcu(struct vport *vport,
55                               struct device_config *new_config)
56 {
57         struct patch_vport *patch_vport = patch_vport_priv(vport);
58         struct device_config *old_config;
59
60         old_config = rcu_dereference(patch_vport->devconf);
61         rcu_assign_pointer(patch_vport->devconf, new_config);
62         call_rcu(&old_config->rcu, free_config);
63 }
64
65 static struct hlist_head *hash_bucket(const char *name)
66 {
67         unsigned int hash = full_name_hash(name, strlen(name));
68         return &peer_table[hash & (PEER_HASH_BUCKETS - 1)];
69 }
70
71 static int patch_init(void)
72 {
73         peer_table = kzalloc(PEER_HASH_BUCKETS * sizeof(struct hlist_head),
74                             GFP_KERNEL);
75         if (!peer_table)
76                 return -ENOMEM;
77
78         return 0;
79 }
80
81 static void patch_exit(void)
82 {
83         kfree(peer_table);
84 }
85
86 static int set_config(struct vport *vport, const void __user *uconfig)
87 {
88         struct patch_vport *patch_vport = patch_vport_priv(vport);
89         char peer_name[IFNAMSIZ];
90         int retval;
91
92         retval = strncpy_from_user(peer_name, uconfig, IFNAMSIZ);
93         if (retval < 0)
94                 return -EFAULT;
95         else if (retval >= IFNAMSIZ)
96                 return -ENAMETOOLONG;
97
98         if (!strcmp(patch_vport->name, peer_name))
99                 return -EINVAL;
100
101         strcpy(patch_vport->peer_name, peer_name);
102
103         if (vport_get_dp_port(vport)) {
104                 hlist_del(&patch_vport->hash_node);
105                 rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
106                 hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
107         }
108
109         return 0;
110 }
111
112 static struct vport *patch_create(const char *name, const void __user *config)
113 {
114         struct vport *vport;
115         struct patch_vport *patch_vport;
116         int err;
117
118         vport = vport_alloc(sizeof(struct patch_vport), &patch_vport_ops);
119         if (IS_ERR(vport)) {
120                 err = PTR_ERR(vport);
121                 goto error;
122         }
123
124         patch_vport = patch_vport_priv(vport);
125
126         strcpy(patch_vport->name, name);
127
128         err = set_config(vport, config);
129         if (err)
130                 goto error_free_vport;
131
132         patch_vport->devconf = kmalloc(sizeof(struct device_config), GFP_KERNEL);
133         if (!patch_vport->devconf) {
134                 err = -ENOMEM;
135                 goto error_free_vport;
136         }
137
138         vport_gen_rand_ether_addr(patch_vport->devconf->eth_addr);
139
140     /* Make the default MTU fairly large so that it doesn't become the
141      * bottleneck on systems using jumbo frames. */
142         patch_vport->devconf->mtu = 65535;
143
144         return vport;
145
146 error_free_vport:
147         vport_free(vport);
148 error:
149         return ERR_PTR(err);
150 }
151
152 static int patch_modify(struct vport *vport, const void __user *config)
153 {
154         return set_config(vport, config);
155 }
156
157 static int patch_destroy(struct vport *vport)
158 {
159         struct patch_vport *patch_vport = patch_vport_priv(vport);
160
161         kfree(patch_vport->devconf);
162         vport_free(vport);
163
164         return 0;
165 }
166
167 static void update_peers(const char *name, struct vport *vport)
168 {
169         struct hlist_head *bucket = hash_bucket(name);
170         struct patch_vport *peer_vport;
171         struct hlist_node *node;
172
173         hlist_for_each_entry(peer_vport, node, bucket, hash_node)
174                 if (!strcmp(peer_vport->peer_name, name))
175                         rcu_assign_pointer(peer_vport->peer, vport);
176 }
177
178 static int patch_attach(struct vport *vport)
179 {
180         struct patch_vport *patch_vport = patch_vport_priv(vport);
181
182         hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
183
184         rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
185         update_peers(patch_vport->name, vport);
186
187         return 0;
188 }
189
190 static int patch_detach(struct vport *vport)
191 {
192         struct patch_vport *patch_vport = patch_vport_priv(vport);
193
194         update_peers(patch_vport->name, NULL);
195         rcu_assign_pointer(patch_vport->peer, NULL);
196
197         hlist_del(&patch_vport->hash_node);
198
199         return 0;
200 }
201
202 static int patch_set_mtu(struct vport *vport, int mtu)
203 {
204         struct patch_vport *patch_vport = patch_vport_priv(vport);
205         struct device_config *devconf;
206
207         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
208         if (!devconf)
209                 return -ENOMEM;
210
211         devconf->mtu = mtu;
212         assign_config_rcu(vport, devconf);
213
214         return 0;
215 }
216
217 static int patch_set_addr(struct vport *vport, const unsigned char *addr)
218 {
219         struct patch_vport *patch_vport = patch_vport_priv(vport);
220         struct device_config *devconf;
221
222         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
223         if (!devconf)
224                 return -ENOMEM;
225
226         memcpy(devconf->eth_addr, addr, ETH_ALEN);
227         assign_config_rcu(vport, devconf);
228
229         return 0;
230 }
231
232
233 static const char *patch_get_name(const struct vport *vport)
234 {
235         const struct patch_vport *patch_vport = patch_vport_priv(vport);
236         return patch_vport->name;
237 }
238
239 static const unsigned char *patch_get_addr(const struct vport *vport)
240 {
241         const struct patch_vport *patch_vport = patch_vport_priv(vport);
242         return rcu_dereference(patch_vport->devconf)->eth_addr;
243 }
244
245 static int patch_get_mtu(const struct vport *vport)
246 {
247         const struct patch_vport *patch_vport = patch_vport_priv(vport);
248         return rcu_dereference(patch_vport->devconf)->mtu;
249 }
250
251 static int patch_send(struct vport *vport, struct sk_buff *skb)
252 {
253         struct patch_vport *patch_vport = patch_vport_priv(vport);
254         struct vport *peer = rcu_dereference(patch_vport->peer);
255         int skb_len = skb->len;
256
257         if (!peer) {
258                 kfree_skb(skb);
259                 vport_record_error(vport, VPORT_E_TX_DROPPED);
260
261                 return 0;
262         }
263
264         vport_receive(peer, skb);
265         return skb_len;
266 }
267
268 struct vport_ops patch_vport_ops = {
269         .type           = "patch",
270         .flags          = VPORT_F_GEN_STATS,
271         .init           = patch_init,
272         .exit           = patch_exit,
273         .create         = patch_create,
274         .modify         = patch_modify,
275         .destroy        = patch_destroy,
276         .attach         = patch_attach,
277         .detach         = patch_detach,
278         .set_mtu        = patch_set_mtu,
279         .set_addr       = patch_set_addr,
280         .get_name       = patch_get_name,
281         .get_addr       = patch_get_addr,
282         .get_dev_flags  = vport_gen_get_dev_flags,
283         .is_running     = vport_gen_is_running,
284         .get_operstate  = vport_gen_get_operstate,
285         .get_mtu        = patch_get_mtu,
286         .send           = patch_send,
287 };