]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e23775f2 | 2 | * Copyright (c) 2007-2015 Nicira, Inc. |
a14bc59f | 3 | * |
a9a29d22 JG |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301, USA | |
064af421 BP |
17 | */ |
18 | ||
dfffaef1 JP |
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
20 | ||
064af421 BP |
21 | #include <linux/init.h> |
22 | #include <linux/module.h> | |
064af421 | 23 | #include <linux/if_arp.h> |
064af421 BP |
24 | #include <linux/if_vlan.h> |
25 | #include <linux/in.h> | |
26 | #include <linux/ip.h> | |
982b8810 | 27 | #include <linux/jhash.h> |
064af421 BP |
28 | #include <linux/delay.h> |
29 | #include <linux/time.h> | |
30 | #include <linux/etherdevice.h> | |
ed099e92 | 31 | #include <linux/genetlink.h> |
064af421 BP |
32 | #include <linux/kernel.h> |
33 | #include <linux/kthread.h> | |
064af421 BP |
34 | #include <linux/mutex.h> |
35 | #include <linux/percpu.h> | |
36 | #include <linux/rcupdate.h> | |
37 | #include <linux/tcp.h> | |
38 | #include <linux/udp.h> | |
39 | #include <linux/version.h> | |
40 | #include <linux/ethtool.h> | |
064af421 | 41 | #include <linux/wait.h> |
064af421 | 42 | #include <asm/div64.h> |
656a0e37 | 43 | #include <linux/highmem.h> |
064af421 BP |
44 | #include <linux/netfilter_bridge.h> |
45 | #include <linux/netfilter_ipv4.h> | |
46 | #include <linux/inetdevice.h> | |
47 | #include <linux/list.h> | |
077257b8 | 48 | #include <linux/openvswitch.h> |
064af421 | 49 | #include <linux/rculist.h> |
064af421 | 50 | #include <linux/dmi.h> |
36956a7d | 51 | #include <net/genetlink.h> |
2a4999f3 PS |
52 | #include <net/net_namespace.h> |
53 | #include <net/netns/generic.h> | |
064af421 | 54 | |
064af421 | 55 | #include "datapath.h" |
064af421 | 56 | #include "flow.h" |
d103f479 | 57 | #include "flow_table.h" |
a097c0b2 | 58 | #include "flow_netlink.h" |
e23775f2 | 59 | #include "gso.h" |
303708cc | 60 | #include "vlan.h" |
f2459fe7 | 61 | #include "vport-internal_dev.h" |
d5de5b0d | 62 | #include "vport-netdev.h" |
064af421 | 63 | |
2a4999f3 | 64 | int ovs_net_id __read_mostly; |
5a38795f | 65 | EXPORT_SYMBOL_GPL(ovs_net_id); |
2a4999f3 | 66 | |
cb25142c PS |
67 | static struct genl_family dp_packet_genl_family; |
68 | static struct genl_family dp_flow_genl_family; | |
69 | static struct genl_family dp_datapath_genl_family; | |
70 | ||
bc619e29 JS |
71 | static const struct nla_policy flow_policy[]; |
72 | ||
18fd3a52 PS |
73 | static struct genl_multicast_group ovs_dp_flow_multicast_group = { |
74 | .name = OVS_FLOW_MCGROUP | |
cb25142c PS |
75 | }; |
76 | ||
18fd3a52 PS |
77 | static struct genl_multicast_group ovs_dp_datapath_multicast_group = { |
78 | .name = OVS_DATAPATH_MCGROUP | |
cb25142c PS |
79 | }; |
80 | ||
18fd3a52 PS |
81 | struct genl_multicast_group ovs_dp_vport_multicast_group = { |
82 | .name = OVS_VPORT_MCGROUP | |
cb25142c PS |
83 | }; |
84 | ||
afad3556 | 85 | /* Check if need to build a reply message. |
af465b67 PS |
86 | * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. |
87 | */ | |
114fce23 SG |
88 | static bool ovs_must_notify(struct genl_family *family, struct genl_info *info, |
89 | unsigned int group) | |
afad3556 JR |
90 | { |
91 | return info->nlhdr->nlmsg_flags & NLM_F_ECHO || | |
6233a1bd | 92 | genl_has_listeners(family, genl_info_net(info), group); |
afad3556 JR |
93 | } |
94 | ||
18fd3a52 | 95 | static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp, |
cb25142c | 96 | struct sk_buff *skb, struct genl_info *info) |
e297c6b7 | 97 | { |
cb25142c PS |
98 | genl_notify(family, skb, genl_info_net(info), |
99 | info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL); | |
e297c6b7 TG |
100 | } |
101 | ||
ed099e92 BP |
102 | /** |
103 | * DOC: Locking: | |
064af421 | 104 | * |
cd2a59e9 PS |
105 | * All writes e.g. Writes to device state (add/remove datapath, port, set |
106 | * operations on vports, etc.), Writes to other state (flow table | |
107 | * modifications, set miscellaneous datapath parameters, etc.) are protected | |
108 | * by ovs_lock. | |
ed099e92 BP |
109 | * |
110 | * Reads are protected by RCU. | |
111 | * | |
112 | * There are a few special cases (mostly stats) that have their own | |
113 | * synchronization but they nest under all of above and don't interact with | |
114 | * each other. | |
cd2a59e9 PS |
115 | * |
116 | * The RTNL lock nests inside ovs_mutex. | |
064af421 | 117 | */ |
ed099e92 | 118 | |
cd2a59e9 PS |
119 | static DEFINE_MUTEX(ovs_mutex); |
120 | ||
121 | void ovs_lock(void) | |
122 | { | |
123 | mutex_lock(&ovs_mutex); | |
124 | } | |
125 | ||
126 | void ovs_unlock(void) | |
127 | { | |
128 | mutex_unlock(&ovs_mutex); | |
129 | } | |
130 | ||
131 | #ifdef CONFIG_LOCKDEP | |
132 | int lockdep_ovsl_is_held(void) | |
133 | { | |
134 | if (debug_locks) | |
135 | return lockdep_is_held(&ovs_mutex); | |
136 | else | |
137 | return 1; | |
138 | } | |
5a38795f | 139 | EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held); |
cd2a59e9 PS |
140 | #endif |
141 | ||
5ae440c3 | 142 | static int queue_gso_packets(struct datapath *dp, struct sk_buff *, |
f1f60b85 TG |
143 | const struct sw_flow_key *, |
144 | const struct dp_upcall_info *); | |
5ae440c3 | 145 | static int queue_userspace_packet(struct datapath *dp, struct sk_buff *, |
7d16c847 | 146 | const struct sw_flow_key *, |
7257b535 | 147 | const struct dp_upcall_info *); |
064af421 | 148 | |
01ac0970 AZ |
149 | /* Must be called with rcu_read_lock. */ |
150 | static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex) | |
064af421 | 151 | { |
01ac0970 | 152 | struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex); |
ed099e92 | 153 | |
254f2dc8 | 154 | if (dev) { |
850b6b3b | 155 | struct vport *vport = ovs_internal_dev_get_vport(dev); |
254f2dc8 | 156 | if (vport) |
01ac0970 | 157 | return vport->dp; |
254f2dc8 | 158 | } |
01ac0970 AZ |
159 | |
160 | return NULL; | |
161 | } | |
162 | ||
163 | /* The caller must hold either ovs_mutex or rcu_read_lock to keep the | |
af465b67 PS |
164 | * returned dp pointer valid. |
165 | */ | |
01ac0970 AZ |
166 | static inline struct datapath *get_dp(struct net *net, int dp_ifindex) |
167 | { | |
168 | struct datapath *dp; | |
169 | ||
170 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); | |
171 | rcu_read_lock(); | |
172 | dp = get_dp_rcu(net, dp_ifindex); | |
254f2dc8 BP |
173 | rcu_read_unlock(); |
174 | ||
175 | return dp; | |
064af421 | 176 | } |
064af421 | 177 | |
cd2a59e9 | 178 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
850b6b3b | 179 | const char *ovs_dp_name(const struct datapath *dp) |
f2459fe7 | 180 | { |
cd2a59e9 | 181 | struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL); |
e23775f2 | 182 | return ovs_vport_name(vport); |
f2459fe7 JG |
183 | } |
184 | ||
f1f60b85 | 185 | static int get_dpifindex(const struct datapath *dp) |
99769a40 JG |
186 | { |
187 | struct vport *local; | |
188 | int ifindex; | |
189 | ||
190 | rcu_read_lock(); | |
191 | ||
95b1d73a | 192 | local = ovs_vport_rcu(dp, OVSP_LOCAL); |
99769a40 | 193 | if (local) |
e23775f2 | 194 | ifindex = local->dev->ifindex; |
99769a40 JG |
195 | else |
196 | ifindex = 0; | |
197 | ||
198 | rcu_read_unlock(); | |
199 | ||
200 | return ifindex; | |
201 | } | |
202 | ||
46c6a11d JG |
203 | static void destroy_dp_rcu(struct rcu_head *rcu) |
204 | { | |
205 | struct datapath *dp = container_of(rcu, struct datapath, rcu); | |
46c6a11d | 206 | |
e379e4d1 | 207 | ovs_flow_tbl_destroy(&dp->table); |
46c6a11d | 208 | free_percpu(dp->stats_percpu); |
95b1d73a | 209 | kfree(dp->ports); |
5ca1ba48 | 210 | kfree(dp); |
46c6a11d JG |
211 | } |
212 | ||
95b1d73a PS |
213 | static struct hlist_head *vport_hash_bucket(const struct datapath *dp, |
214 | u16 port_no) | |
215 | { | |
216 | return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; | |
217 | } | |
218 | ||
aa917006 | 219 | /* Called with ovs_mutex or RCU read lock. */ |
95b1d73a PS |
220 | struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) |
221 | { | |
222 | struct vport *vport; | |
95b1d73a PS |
223 | struct hlist_head *head; |
224 | ||
225 | head = vport_hash_bucket(dp, port_no); | |
f8dfbcb7 | 226 | hlist_for_each_entry_rcu(vport, head, dp_hash_node) { |
95b1d73a PS |
227 | if (vport->port_no == port_no) |
228 | return vport; | |
229 | } | |
230 | return NULL; | |
231 | } | |
232 | ||
cd2a59e9 | 233 | /* Called with ovs_mutex. */ |
c19e6535 | 234 | static struct vport *new_vport(const struct vport_parms *parms) |
064af421 | 235 | { |
f2459fe7 | 236 | struct vport *vport; |
f2459fe7 | 237 | |
850b6b3b | 238 | vport = ovs_vport_add(parms); |
c19e6535 BP |
239 | if (!IS_ERR(vport)) { |
240 | struct datapath *dp = parms->dp; | |
95b1d73a | 241 | struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); |
064af421 | 242 | |
95b1d73a | 243 | hlist_add_head_rcu(&vport->dp_hash_node, head); |
c19e6535 | 244 | } |
c19e6535 | 245 | return vport; |
064af421 BP |
246 | } |
247 | ||
850b6b3b | 248 | void ovs_dp_detach_port(struct vport *p) |
064af421 | 249 | { |
cd2a59e9 | 250 | ASSERT_OVSL(); |
064af421 | 251 | |
064af421 | 252 | /* First drop references to device. */ |
95b1d73a | 253 | hlist_del_rcu(&p->dp_hash_node); |
f2459fe7 | 254 | |
7237e4f4 | 255 | /* Then destroy it. */ |
850b6b3b | 256 | ovs_vport_del(p); |
064af421 BP |
257 | } |
258 | ||
fb66fbd1 | 259 | /* Must be called with rcu_read_lock. */ |
e74d4817 | 260 | void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key) |
064af421 | 261 | { |
a6059080 | 262 | const struct vport *p = OVS_CB(skb)->input_vport; |
064af421 | 263 | struct datapath *dp = p->dp; |
3544358a | 264 | struct sw_flow *flow; |
ad50cb60 | 265 | struct sw_flow_actions *sf_acts; |
064af421 | 266 | struct dp_stats_percpu *stats; |
e9141eec | 267 | u64 *stats_counter; |
4fa72a95 | 268 | u32 n_mask_hit; |
064af421 | 269 | |
70dbc259 | 270 | stats = this_cpu_ptr(dp->stats_percpu); |
a063b0df | 271 | |
52a23d92 | 272 | /* Look up flow. */ |
e74d4817 | 273 | flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb), |
5604935e | 274 | &n_mask_hit); |
52a23d92 JG |
275 | if (unlikely(!flow)) { |
276 | struct dp_upcall_info upcall; | |
a7d607c5 | 277 | int error; |
52a23d92 | 278 | |
0e469d3b | 279 | memset(&upcall, 0, sizeof(upcall)); |
52a23d92 | 280 | upcall.cmd = OVS_PACKET_CMD_MISS; |
beb1c69a | 281 | upcall.portid = ovs_vport_find_upcall_portid(p, skb); |
e74d4817 | 282 | error = ovs_dp_upcall(dp, skb, key, &upcall); |
a7d607c5 LR |
283 | if (unlikely(error)) |
284 | kfree_skb(skb); | |
285 | else | |
286 | consume_skb(skb); | |
52a23d92 JG |
287 | stats_counter = &stats->n_missed; |
288 | goto out; | |
289 | } | |
290 | ||
e74d4817 | 291 | ovs_flow_stats_update(flow, key->tp.flags, skb); |
ad50cb60 | 292 | sf_acts = rcu_dereference(flow->sf_acts); |
7d16c847 PS |
293 | ovs_execute_actions(dp, skb, sf_acts, key); |
294 | ||
b0b906cc | 295 | stats_counter = &stats->n_hit; |
55574bb0 | 296 | |
8819fac7 | 297 | out: |
55574bb0 | 298 | /* Update datapath statistics. */ |
b81deb15 | 299 | u64_stats_update_begin(&stats->syncp); |
e9141eec | 300 | (*stats_counter)++; |
4fa72a95 | 301 | stats->n_mask_hit += n_mask_hit; |
b81deb15 | 302 | u64_stats_update_end(&stats->syncp); |
064af421 BP |
303 | } |
304 | ||
850b6b3b | 305 | int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, |
f1f60b85 | 306 | const struct sw_flow_key *key, |
850b6b3b | 307 | const struct dp_upcall_info *upcall_info) |
aa5a8fdc JG |
308 | { |
309 | struct dp_stats_percpu *stats; | |
310 | int err; | |
311 | ||
28aea917 | 312 | if (upcall_info->portid == 0) { |
b063d9f0 | 313 | err = -ENOTCONN; |
b063d9f0 JG |
314 | goto err; |
315 | } | |
316 | ||
7257b535 | 317 | if (!skb_is_gso(skb)) |
e74d4817 | 318 | err = queue_userspace_packet(dp, skb, key, upcall_info); |
7257b535 | 319 | else |
e74d4817 | 320 | err = queue_gso_packets(dp, skb, key, upcall_info); |
d76195db JG |
321 | if (err) |
322 | goto err; | |
323 | ||
324 | return 0; | |
aa5a8fdc | 325 | |
aa5a8fdc | 326 | err: |
70dbc259 | 327 | stats = this_cpu_ptr(dp->stats_percpu); |
aa5a8fdc | 328 | |
b81deb15 | 329 | u64_stats_update_begin(&stats->syncp); |
aa5a8fdc | 330 | stats->n_lost++; |
b81deb15 | 331 | u64_stats_update_end(&stats->syncp); |
aa5a8fdc | 332 | |
aa5a8fdc | 333 | return err; |
982b8810 BP |
334 | } |
335 | ||
5ae440c3 | 336 | static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb, |
f1f60b85 | 337 | const struct sw_flow_key *key, |
7257b535 | 338 | const struct dp_upcall_info *upcall_info) |
cb5087ca | 339 | { |
d4cba1f8 | 340 | unsigned short gso_type = skb_shinfo(skb)->gso_type; |
7257b535 BP |
341 | struct sw_flow_key later_key; |
342 | struct sk_buff *segs, *nskb; | |
b2a23c4e | 343 | struct ovs_skb_cb ovs_cb; |
7257b535 | 344 | int err; |
cb5087ca | 345 | |
b2a23c4e | 346 | ovs_cb = *OVS_CB(skb); |
1d04cd4e | 347 | segs = __skb_gso_segment(skb, NETIF_F_SG, false); |
b2a23c4e | 348 | *OVS_CB(skb) = ovs_cb; |
79089764 PS |
349 | if (IS_ERR(segs)) |
350 | return PTR_ERR(segs); | |
d1da7669 PS |
351 | if (segs == NULL) |
352 | return -EINVAL; | |
99769a40 | 353 | |
9b277b39 | 354 | if (gso_type & SKB_GSO_UDP) { |
c135bba1 | 355 | /* The initial flow key extracted by ovs_flow_key_extract() |
9b277b39 PS |
356 | * in this case is for a first fragment, so we need to |
357 | * properly mark later fragments. | |
358 | */ | |
e74d4817 | 359 | later_key = *key; |
9b277b39 PS |
360 | later_key.ip.frag = OVS_FRAG_TYPE_LATER; |
361 | } | |
362 | ||
7257b535 BP |
363 | /* Queue all of the segments. */ |
364 | skb = segs; | |
cb5087ca | 365 | do { |
b2a23c4e | 366 | *OVS_CB(skb) = ovs_cb; |
9b277b39 | 367 | if (gso_type & SKB_GSO_UDP && skb != segs) |
e74d4817 | 368 | key = &later_key; |
9b277b39 | 369 | |
e74d4817 | 370 | err = queue_userspace_packet(dp, skb, key, upcall_info); |
982b8810 | 371 | if (err) |
7257b535 | 372 | break; |
856081f6 | 373 | |
36ce148c | 374 | } while ((skb = skb->next)); |
cb5087ca | 375 | |
7257b535 BP |
376 | /* Free all of the segments. */ |
377 | skb = segs; | |
378 | do { | |
379 | nskb = skb->next; | |
380 | if (err) | |
381 | kfree_skb(skb); | |
382 | else | |
383 | consume_skb(skb); | |
384 | } while ((skb = nskb)); | |
385 | return err; | |
386 | } | |
387 | ||
8b7ea2d4 | 388 | static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info, |
533bea51 | 389 | unsigned int hdrlen) |
0afa2373 TG |
390 | { |
391 | size_t size = NLMSG_ALIGN(sizeof(struct ovs_header)) | |
533bea51 | 392 | + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */ |
4e25b8c1 | 393 | + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */ |
0afa2373 TG |
394 | |
395 | /* OVS_PACKET_ATTR_USERDATA */ | |
8b7ea2d4 WZ |
396 | if (upcall_info->userdata) |
397 | size += NLA_ALIGN(upcall_info->userdata->nla_len); | |
398 | ||
399 | /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */ | |
400 | if (upcall_info->egress_tun_info) | |
401 | size += nla_total_size(ovs_tun_key_attr_size()); | |
0afa2373 | 402 | |
0e469d3b NM |
403 | /* OVS_PACKET_ATTR_ACTIONS */ |
404 | if (upcall_info->actions_len) | |
405 | size += nla_total_size(upcall_info->actions_len); | |
406 | ||
0afa2373 TG |
407 | return size; |
408 | } | |
409 | ||
5ae440c3 | 410 | static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb, |
f1f60b85 | 411 | const struct sw_flow_key *key, |
7257b535 BP |
412 | const struct dp_upcall_info *upcall_info) |
413 | { | |
414 | struct ovs_header *upcall; | |
6161d3fd | 415 | struct sk_buff *nskb = NULL; |
82706a6f | 416 | struct sk_buff *user_skb = NULL; /* to be queued to userspace */ |
7257b535 | 417 | struct nlattr *nla; |
68eadcf0 | 418 | struct genl_info info = { |
705e9260 | 419 | #ifdef HAVE_GENLMSG_NEW_UNICAST |
5ae440c3 | 420 | .dst_sk = ovs_dp_get_net(dp)->genl_sock, |
68eadcf0 TG |
421 | #endif |
422 | .snd_portid = upcall_info->portid, | |
423 | }; | |
978188b2 | 424 | size_t len; |
533bea51 | 425 | unsigned int hlen; |
5ae440c3 TG |
426 | int err, dp_ifindex; |
427 | ||
428 | dp_ifindex = get_dpifindex(dp); | |
429 | if (!dp_ifindex) | |
430 | return -ENODEV; | |
7257b535 | 431 | |
efd8a18e | 432 | if (skb_vlan_tag_present(skb)) { |
6161d3fd JG |
433 | nskb = skb_clone(skb, GFP_ATOMIC); |
434 | if (!nskb) | |
435 | return -ENOMEM; | |
07ac71ea | 436 | |
efd8a18e | 437 | nskb = vlan_insert_tag_set_proto(nskb, nskb->vlan_proto, skb_vlan_tag_get(nskb)); |
07ac71ea PS |
438 | if (!nskb) |
439 | return -ENOMEM; | |
440 | ||
441 | vlan_set_tci(nskb, 0); | |
7257b535 | 442 | |
6161d3fd JG |
443 | skb = nskb; |
444 | } | |
445 | ||
446 | if (nla_attr_size(skb->len) > USHRT_MAX) { | |
447 | err = -EFBIG; | |
448 | goto out; | |
449 | } | |
7257b535 | 450 | |
533bea51 TG |
451 | /* Complete checksum if needed */ |
452 | if (skb->ip_summed == CHECKSUM_PARTIAL && | |
453 | (err = skb_checksum_help(skb))) | |
454 | goto out; | |
455 | ||
456 | /* Older versions of OVS user space enforce alignment of the last | |
457 | * Netlink attribute to NLA_ALIGNTO which would require extensive | |
458 | * padding logic. Only perform zerocopy if padding is not required. | |
459 | */ | |
460 | if (dp->user_features & OVS_DP_F_UNALIGNED) | |
461 | hlen = skb_zerocopy_headlen(skb); | |
462 | else | |
463 | hlen = skb->len; | |
464 | ||
8b7ea2d4 | 465 | len = upcall_msg_size(upcall_info, hlen); |
68eadcf0 | 466 | user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC); |
6161d3fd JG |
467 | if (!user_skb) { |
468 | err = -ENOMEM; | |
469 | goto out; | |
470 | } | |
7257b535 BP |
471 | |
472 | upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, | |
473 | 0, upcall_info->cmd); | |
474 | upcall->dp_ifindex = dp_ifindex; | |
475 | ||
db7f2238 | 476 | err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb); |
9a621f82 | 477 | BUG_ON(err); |
7257b535 BP |
478 | |
479 | if (upcall_info->userdata) | |
e995e3df | 480 | __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA, |
462a988b | 481 | nla_len(upcall_info->userdata), |
e995e3df | 482 | nla_data(upcall_info->userdata)); |
7257b535 | 483 | |
e23775f2 | 484 | |
8b7ea2d4 WZ |
485 | if (upcall_info->egress_tun_info) { |
486 | nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY); | |
487 | err = ovs_nla_put_egress_tunnel_key(user_skb, | |
e23775f2 PS |
488 | upcall_info->egress_tun_info, |
489 | upcall_info->egress_tun_opts); | |
8b7ea2d4 WZ |
490 | BUG_ON(err); |
491 | nla_nest_end(user_skb, nla); | |
492 | } | |
493 | ||
0e469d3b NM |
494 | if (upcall_info->actions_len) { |
495 | nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS); | |
496 | err = ovs_nla_put_actions(upcall_info->actions, | |
497 | upcall_info->actions_len, | |
498 | user_skb); | |
499 | if (!err) | |
500 | nla_nest_end(user_skb, nla); | |
501 | else | |
502 | nla_nest_cancel(user_skb, nla); | |
503 | } | |
504 | ||
533bea51 | 505 | /* Only reserve room for attribute header, packet data is added |
af465b67 PS |
506 | * in skb_zerocopy() |
507 | */ | |
533bea51 TG |
508 | if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) { |
509 | err = -ENOBUFS; | |
510 | goto out; | |
511 | } | |
512 | nla->nla_len = nla_attr_size(skb->len); | |
bed53bd1 | 513 | |
2c272bd9 ZK |
514 | err = skb_zerocopy(user_skb, skb, skb->len, hlen); |
515 | if (err) | |
516 | goto out; | |
7257b535 | 517 | |
ef507cec | 518 | /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */ |
978188b2 JG |
519 | if (!(dp->user_features & OVS_DP_F_UNALIGNED)) { |
520 | size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len; | |
521 | ||
522 | if (plen > 0) | |
523 | memset(skb_put(user_skb, plen), 0, plen); | |
524 | } | |
ef507cec | 525 | |
533bea51 | 526 | ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len; |
6161d3fd | 527 | |
533bea51 | 528 | err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid); |
82706a6f | 529 | user_skb = NULL; |
6161d3fd | 530 | out: |
2c272bd9 ZK |
531 | if (err) |
532 | skb_tx_error(skb); | |
82706a6f | 533 | kfree_skb(user_skb); |
6161d3fd JG |
534 | kfree_skb(nskb); |
535 | return err; | |
cb5087ca BP |
536 | } |
537 | ||
df2c07f4 | 538 | static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) |
064af421 | 539 | { |
df2c07f4 | 540 | struct ovs_header *ovs_header = info->userhdr; |
982b8810 | 541 | struct nlattr **a = info->attrs; |
e0e57990 | 542 | struct sw_flow_actions *acts; |
982b8810 | 543 | struct sk_buff *packet; |
e0e57990 | 544 | struct sw_flow *flow; |
ad50cb60 | 545 | struct sw_flow_actions *sf_acts; |
f7cd0081 | 546 | struct datapath *dp; |
d6569377 | 547 | struct ethhdr *eth; |
a6059080 | 548 | struct vport *input_vport; |
3f19d399 | 549 | int len; |
d6569377 | 550 | int err; |
2e460098 | 551 | bool log = !a[OVS_PACKET_ATTR_PROBE]; |
064af421 | 552 | |
f7cd0081 | 553 | err = -EINVAL; |
df2c07f4 | 554 | if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || |
7c3072cc | 555 | !a[OVS_PACKET_ATTR_ACTIONS]) |
e5cad958 | 556 | goto err; |
064af421 | 557 | |
df2c07f4 | 558 | len = nla_len(a[OVS_PACKET_ATTR_PACKET]); |
3f19d399 | 559 | packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); |
f7cd0081 BP |
560 | err = -ENOMEM; |
561 | if (!packet) | |
e5cad958 | 562 | goto err; |
3f19d399 BP |
563 | skb_reserve(packet, NET_IP_ALIGN); |
564 | ||
bf3d6fce | 565 | nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len); |
8d5ebd83 | 566 | |
f7cd0081 BP |
567 | skb_reset_mac_header(packet); |
568 | eth = eth_hdr(packet); | |
064af421 | 569 | |
d6569377 BP |
570 | /* Normally, setting the skb 'protocol' field would be handled by a |
571 | * call to eth_type_trans(), but it assumes there's a sending | |
af465b67 PS |
572 | * device, which we may not have. |
573 | */ | |
935fc582 | 574 | if (eth_proto_is_802_3(eth->h_proto)) |
f7cd0081 | 575 | packet->protocol = eth->h_proto; |
d6569377 | 576 | else |
f7cd0081 | 577 | packet->protocol = htons(ETH_P_802_2); |
d3c54451 | 578 | |
e0e57990 | 579 | /* Build an sw_flow for sending this packet. */ |
df65fec1 | 580 | flow = ovs_flow_alloc(); |
e0e57990 BP |
581 | err = PTR_ERR(flow); |
582 | if (IS_ERR(flow)) | |
e5cad958 | 583 | goto err_kfree_skb; |
064af421 | 584 | |
c135bba1 | 585 | err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet, |
9233cef7 | 586 | &flow->key, log); |
e0e57990 | 587 | if (err) |
9321954a | 588 | goto err_flow_free; |
e0e57990 | 589 | |
a097c0b2 | 590 | err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], |
9233cef7 | 591 | &flow->key, &acts, log); |
9b405f1a PS |
592 | if (err) |
593 | goto err_flow_free; | |
e0e57990 | 594 | |
ff27161e | 595 | rcu_assign_pointer(flow->sf_acts, acts); |
abff858b | 596 | packet->priority = flow->key.phy.priority; |
3025a772 | 597 | packet->mark = flow->key.phy.skb_mark; |
e0e57990 | 598 | |
d6569377 | 599 | rcu_read_lock(); |
01ac0970 | 600 | dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); |
f7cd0081 | 601 | err = -ENODEV; |
e5cad958 BP |
602 | if (!dp) |
603 | goto err_unlock; | |
cc4015df | 604 | |
a6059080 AZ |
605 | input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port); |
606 | if (!input_vport) | |
607 | input_vport = ovs_vport_rcu(dp, OVSP_LOCAL); | |
608 | ||
609 | if (!input_vport) | |
610 | goto err_unlock; | |
611 | ||
e23775f2 | 612 | packet->dev = input_vport->dev; |
a6059080 | 613 | OVS_CB(packet)->input_vport = input_vport; |
ad50cb60 | 614 | sf_acts = rcu_dereference(flow->sf_acts); |
a6059080 | 615 | |
e9141eec | 616 | local_bh_disable(); |
7d16c847 | 617 | err = ovs_execute_actions(dp, packet, sf_acts, &flow->key); |
e9141eec | 618 | local_bh_enable(); |
d6569377 | 619 | rcu_read_unlock(); |
e0e57990 | 620 | |
a1c564be | 621 | ovs_flow_free(flow, false); |
e5cad958 | 622 | return err; |
064af421 | 623 | |
e5cad958 BP |
624 | err_unlock: |
625 | rcu_read_unlock(); | |
9321954a | 626 | err_flow_free: |
a1c564be | 627 | ovs_flow_free(flow, false); |
e5cad958 BP |
628 | err_kfree_skb: |
629 | kfree_skb(packet); | |
630 | err: | |
d6569377 | 631 | return err; |
064af421 BP |
632 | } |
633 | ||
df2c07f4 | 634 | static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { |
7c3072cc | 635 | [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN }, |
df2c07f4 JP |
636 | [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, |
637 | [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, | |
2e460098 | 638 | [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG }, |
982b8810 BP |
639 | }; |
640 | ||
18fd3a52 | 641 | static struct genl_ops dp_packet_genl_ops[] = { |
df2c07f4 | 642 | { .cmd = OVS_PACKET_CMD_EXECUTE, |
982b8810 BP |
643 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
644 | .policy = packet_policy, | |
df2c07f4 | 645 | .doit = ovs_packet_cmd_execute |
982b8810 BP |
646 | } |
647 | }; | |
648 | ||
cb25142c PS |
649 | static struct genl_family dp_packet_genl_family = { |
650 | .id = GENL_ID_GENERATE, | |
651 | .hdrsize = sizeof(struct ovs_header), | |
652 | .name = OVS_PACKET_FAMILY, | |
653 | .version = OVS_PACKET_VERSION, | |
654 | .maxattr = OVS_PACKET_ATTR_MAX, | |
655 | .netnsok = true, | |
656 | .parallel_ops = true, | |
657 | .ops = dp_packet_genl_ops, | |
658 | .n_ops = ARRAY_SIZE(dp_packet_genl_ops), | |
659 | }; | |
660 | ||
f1f60b85 | 661 | static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats, |
4fa72a95 | 662 | struct ovs_dp_megaflow_stats *mega_stats) |
064af421 | 663 | { |
d6569377 | 664 | int i; |
f180c2e2 | 665 | |
4fa72a95 AZ |
666 | memset(mega_stats, 0, sizeof(*mega_stats)); |
667 | ||
994dc286 | 668 | stats->n_flows = ovs_flow_tbl_count(&dp->table); |
4fa72a95 | 669 | mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table); |
064af421 | 670 | |
7257b535 | 671 | stats->n_hit = stats->n_missed = stats->n_lost = 0; |
4fa72a95 | 672 | |
d6569377 BP |
673 | for_each_possible_cpu(i) { |
674 | const struct dp_stats_percpu *percpu_stats; | |
675 | struct dp_stats_percpu local_stats; | |
821cb9fa | 676 | unsigned int start; |
44e05eca | 677 | |
d6569377 | 678 | percpu_stats = per_cpu_ptr(dp->stats_percpu, i); |
064af421 | 679 | |
d6569377 | 680 | do { |
b81deb15 | 681 | start = u64_stats_fetch_begin_irq(&percpu_stats->syncp); |
d6569377 | 682 | local_stats = *percpu_stats; |
b81deb15 | 683 | } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start)); |
064af421 | 684 | |
d6569377 BP |
685 | stats->n_hit += local_stats.n_hit; |
686 | stats->n_missed += local_stats.n_missed; | |
687 | stats->n_lost += local_stats.n_lost; | |
4fa72a95 | 688 | mega_stats->n_mask_hit += local_stats.n_mask_hit; |
d6569377 BP |
689 | } |
690 | } | |
064af421 | 691 | |
bc619e29 JS |
692 | static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags) |
693 | { | |
694 | return ovs_identifier_is_ufid(sfid) && | |
695 | !(ufid_flags & OVS_UFID_F_OMIT_KEY); | |
696 | } | |
697 | ||
698 | static bool should_fill_mask(uint32_t ufid_flags) | |
699 | { | |
700 | return !(ufid_flags & OVS_UFID_F_OMIT_MASK); | |
701 | } | |
702 | ||
703 | static bool should_fill_actions(uint32_t ufid_flags) | |
0afa2373 | 704 | { |
bc619e29 JS |
705 | return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS); |
706 | } | |
707 | ||
708 | static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts, | |
709 | const struct sw_flow_id *sfid, | |
710 | uint32_t ufid_flags) | |
711 | { | |
712 | size_t len = NLMSG_ALIGN(sizeof(struct ovs_header)); | |
713 | ||
714 | /* OVS_FLOW_ATTR_UFID */ | |
715 | if (sfid && ovs_identifier_is_ufid(sfid)) | |
716 | len += nla_total_size(sfid->ufid_len); | |
717 | ||
718 | /* OVS_FLOW_ATTR_KEY */ | |
719 | if (!sfid || should_fill_key(sfid, ufid_flags)) | |
720 | len += nla_total_size(ovs_key_attr_size()); | |
721 | ||
722 | /* OVS_FLOW_ATTR_MASK */ | |
723 | if (should_fill_mask(ufid_flags)) | |
724 | len += nla_total_size(ovs_key_attr_size()); | |
725 | ||
726 | /* OVS_FLOW_ATTR_ACTIONS */ | |
727 | if (should_fill_actions(ufid_flags)) | |
c3bb15b3 | 728 | len += nla_total_size(acts->orig_len); |
bc619e29 JS |
729 | |
730 | return len | |
0afa2373 TG |
731 | + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */ |
732 | + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */ | |
bc619e29 | 733 | + nla_total_size(8); /* OVS_FLOW_ATTR_USED */ |
0afa2373 TG |
734 | } |
735 | ||
f1948bb9 JS |
736 | /* Called with ovs_mutex or RCU read lock. */ |
737 | static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow, | |
738 | struct sk_buff *skb) | |
739 | { | |
740 | struct ovs_flow_stats stats; | |
741 | __be16 tcp_flags; | |
742 | unsigned long used; | |
743 | ||
b0f3a2fe | 744 | ovs_flow_stats_get(flow, &stats, &used, &tcp_flags); |
f71db6b1 | 745 | |
b0f3a2fe PS |
746 | if (used && |
747 | nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used))) | |
f1948bb9 | 748 | return -EMSGSIZE; |
d6569377 | 749 | |
b0f3a2fe PS |
750 | if (stats.n_packets && |
751 | nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats)) | |
f1948bb9 | 752 | return -EMSGSIZE; |
b0b906cc | 753 | |
b0f3a2fe PS |
754 | if ((u8)ntohs(tcp_flags) && |
755 | nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags))) | |
f1948bb9 JS |
756 | return -EMSGSIZE; |
757 | ||
758 | return 0; | |
759 | } | |
760 | ||
761 | /* Called with ovs_mutex or RCU read lock. */ | |
762 | static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow, | |
763 | struct sk_buff *skb, int skb_orig_len) | |
764 | { | |
765 | struct nlattr *start; | |
766 | int err; | |
d6569377 | 767 | |
df2c07f4 | 768 | /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if |
30053024 BP |
769 | * this is the first flow to be dumped into 'skb'. This is unusual for |
770 | * Netlink but individual action lists can be longer than | |
771 | * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. | |
772 | * The userspace caller can always fetch the actions separately if it | |
773 | * really wants them. (Most userspace callers in fact don't care.) | |
774 | * | |
775 | * This can only fail for dump operations because the skb is always | |
776 | * properly sized for single flows. | |
777 | */ | |
9b405f1a | 778 | start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS); |
f6f481ee | 779 | if (start) { |
f44ccce1 PS |
780 | const struct sw_flow_actions *sf_acts; |
781 | ||
780ec6ae | 782 | sf_acts = rcu_dereference_ovsl(flow->sf_acts); |
a097c0b2 PS |
783 | err = ovs_nla_put_actions(sf_acts->actions, |
784 | sf_acts->actions_len, skb); | |
f71db6b1 | 785 | |
0a25b039 BP |
786 | if (!err) |
787 | nla_nest_end(skb, start); | |
788 | else { | |
789 | if (skb_orig_len) | |
f1948bb9 | 790 | return err; |
0a25b039 BP |
791 | |
792 | nla_nest_cancel(skb, start); | |
793 | } | |
f1948bb9 JS |
794 | } else if (skb_orig_len) { |
795 | return -EMSGSIZE; | |
796 | } | |
797 | ||
798 | return 0; | |
799 | } | |
800 | ||
801 | /* Called with ovs_mutex or RCU read lock. */ | |
2c622e5a | 802 | static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex, |
f1948bb9 | 803 | struct sk_buff *skb, u32 portid, |
bc619e29 | 804 | u32 seq, u32 flags, u8 cmd, u32 ufid_flags) |
f1948bb9 JS |
805 | { |
806 | const int skb_orig_len = skb->len; | |
807 | struct ovs_header *ovs_header; | |
808 | int err; | |
809 | ||
7d16c847 PS |
810 | ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, |
811 | flags, cmd); | |
f1948bb9 JS |
812 | if (!ovs_header) |
813 | return -EMSGSIZE; | |
7d16c847 | 814 | |
f1948bb9 JS |
815 | ovs_header->dp_ifindex = dp_ifindex; |
816 | ||
bc619e29 | 817 | err = ovs_nla_put_identifier(flow, skb); |
db7f2238 JS |
818 | if (err) |
819 | goto error; | |
820 | ||
bc619e29 JS |
821 | if (should_fill_key(&flow->id, ufid_flags)) { |
822 | err = ovs_nla_put_masked_key(flow, skb); | |
823 | if (err) | |
824 | goto error; | |
825 | } | |
826 | ||
827 | if (should_fill_mask(ufid_flags)) { | |
828 | err = ovs_nla_put_mask(flow, skb); | |
829 | if (err) | |
830 | goto error; | |
831 | } | |
f1948bb9 JS |
832 | |
833 | err = ovs_flow_cmd_fill_stats(flow, skb); | |
834 | if (err) | |
835 | goto error; | |
836 | ||
bc619e29 JS |
837 | if (should_fill_actions(ufid_flags)) { |
838 | err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len); | |
839 | if (err) | |
840 | goto error; | |
841 | } | |
37a1300c | 842 | |
23b48dc1 TG |
843 | genlmsg_end(skb, ovs_header); |
844 | return 0; | |
d6569377 | 845 | |
37a1300c | 846 | error: |
df2c07f4 | 847 | genlmsg_cancel(skb, ovs_header); |
d6569377 | 848 | return err; |
44e05eca BP |
849 | } |
850 | ||
f71db6b1 JR |
851 | /* May not be called with RCU read lock. */ |
852 | static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts, | |
bc619e29 | 853 | const struct sw_flow_id *sfid, |
afad3556 | 854 | struct genl_info *info, |
bc619e29 JS |
855 | bool always, |
856 | uint32_t ufid_flags) | |
44e05eca | 857 | { |
afad3556 | 858 | struct sk_buff *skb; |
bc619e29 | 859 | size_t len; |
d6569377 | 860 | |
114fce23 SG |
861 | if (!always && !ovs_must_notify(&dp_flow_genl_family, info, |
862 | GROUP_ID(&ovs_dp_flow_multicast_group))) | |
afad3556 JR |
863 | return NULL; |
864 | ||
bc619e29 JS |
865 | len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags); |
866 | skb = genlmsg_new_unicast(len, info, GFP_KERNEL); | |
afad3556 JR |
867 | if (!skb) |
868 | return ERR_PTR(-ENOMEM); | |
869 | ||
870 | return skb; | |
37a1300c | 871 | } |
8d5ebd83 | 872 | |
f71db6b1 | 873 | /* Called with ovs_mutex. */ |
7d16c847 | 874 | static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow, |
f71db6b1 JR |
875 | int dp_ifindex, |
876 | struct genl_info *info, u8 cmd, | |
bc619e29 | 877 | bool always, u32 ufid_flags) |
37a1300c BP |
878 | { |
879 | struct sk_buff *skb; | |
880 | int retval; | |
d6569377 | 881 | |
bc619e29 JS |
882 | skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), |
883 | &flow->id, info, always, ufid_flags); | |
a6ddcc9a | 884 | if (IS_ERR_OR_NULL(skb)) |
afad3556 | 885 | return skb; |
d6569377 | 886 | |
2c622e5a | 887 | retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb, |
f71db6b1 | 888 | info->snd_portid, info->snd_seq, 0, |
bc619e29 | 889 | cmd, ufid_flags); |
37a1300c | 890 | BUG_ON(retval < 0); |
d6569377 | 891 | return skb; |
064af421 BP |
892 | } |
893 | ||
0c9fd022 | 894 | static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info) |
064af421 | 895 | { |
37a1300c | 896 | struct nlattr **a = info->attrs; |
df2c07f4 | 897 | struct ovs_header *ovs_header = info->userhdr; |
bc619e29 | 898 | struct sw_flow *flow = NULL, *new_flow; |
a1c564be | 899 | struct sw_flow_mask mask; |
37a1300c | 900 | struct sk_buff *reply; |
9c52546b | 901 | struct datapath *dp; |
bc619e29 | 902 | struct sw_flow_key key; |
0c9fd022 | 903 | struct sw_flow_actions *acts; |
a1c564be | 904 | struct sw_flow_match match; |
bc619e29 | 905 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); |
bc4a05c6 | 906 | int error; |
9233cef7 | 907 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
064af421 | 908 | |
6740b721 | 909 | /* Must have key and actions. */ |
37a1300c | 910 | error = -EINVAL; |
a473df5b | 911 | if (!a[OVS_FLOW_ATTR_KEY]) { |
7d16c847 | 912 | OVS_NLERR(log, "Flow key attr not present in new flow."); |
37a1300c | 913 | goto error; |
a473df5b JG |
914 | } |
915 | if (!a[OVS_FLOW_ATTR_ACTIONS]) { | |
7d16c847 | 916 | OVS_NLERR(log, "Flow actions attr not present in new flow."); |
6740b721 | 917 | goto error; |
a473df5b | 918 | } |
a1c564be | 919 | |
6740b721 | 920 | /* Most of the time we need to allocate a new flow, do it before |
af465b67 PS |
921 | * locking. |
922 | */ | |
6740b721 JR |
923 | new_flow = ovs_flow_alloc(); |
924 | if (IS_ERR(new_flow)) { | |
925 | error = PTR_ERR(new_flow); | |
926 | goto error; | |
927 | } | |
928 | ||
929 | /* Extract key. */ | |
bc619e29 | 930 | ovs_match_init(&match, &key, &mask); |
9233cef7 JR |
931 | error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], |
932 | a[OVS_FLOW_ATTR_MASK], log); | |
37a1300c | 933 | if (error) |
6740b721 | 934 | goto err_kfree_flow; |
064af421 | 935 | |
ad4adec2 | 936 | ovs_flow_mask_key(&new_flow->key, &key, true, &mask); |
bc619e29 JS |
937 | |
938 | /* Extract flow identifier. */ | |
939 | error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID], | |
940 | &key, log); | |
941 | if (error) | |
942 | goto err_kfree_flow; | |
9b405f1a | 943 | |
6740b721 | 944 | /* Validate actions. */ |
6740b721 | 945 | error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key, |
9233cef7 | 946 | &acts, log); |
0c9fd022 | 947 | if (error) { |
7d16c847 | 948 | OVS_NLERR(log, "Flow actions may not be safe on all matching packets."); |
4f67b12a | 949 | goto err_kfree_flow; |
6740b721 JR |
950 | } |
951 | ||
bc619e29 JS |
952 | reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false, |
953 | ufid_flags); | |
6740b721 JR |
954 | if (IS_ERR(reply)) { |
955 | error = PTR_ERR(reply); | |
956 | goto err_kfree_acts; | |
37a1300c BP |
957 | } |
958 | ||
cd2a59e9 | 959 | ovs_lock(); |
2a4999f3 | 960 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
6740b721 JR |
961 | if (unlikely(!dp)) { |
962 | error = -ENODEV; | |
cd2a59e9 | 963 | goto err_unlock_ovs; |
6740b721 | 964 | } |
bc619e29 | 965 | |
a1c564be | 966 | /* Check if this is a duplicate flow */ |
bc619e29 JS |
967 | if (ovs_identifier_is_ufid(&new_flow->id)) |
968 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id); | |
969 | if (!flow) | |
970 | flow = ovs_flow_tbl_lookup(&dp->table, &key); | |
6740b721 JR |
971 | if (likely(!flow)) { |
972 | rcu_assign_pointer(new_flow->sf_acts, acts); | |
d6569377 | 973 | |
d6569377 | 974 | /* Put flow in bucket. */ |
6740b721 JR |
975 | error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask); |
976 | if (unlikely(error)) { | |
0585f7a8 | 977 | acts = NULL; |
6740b721 JR |
978 | goto err_unlock_ovs; |
979 | } | |
980 | ||
981 | if (unlikely(reply)) { | |
2c622e5a | 982 | error = ovs_flow_cmd_fill_info(new_flow, |
6740b721 JR |
983 | ovs_header->dp_ifindex, |
984 | reply, info->snd_portid, | |
985 | info->snd_seq, 0, | |
bc619e29 JS |
986 | OVS_FLOW_CMD_NEW, |
987 | ufid_flags); | |
6740b721 | 988 | BUG_ON(error < 0); |
0585f7a8 | 989 | } |
6740b721 | 990 | ovs_unlock(); |
d6569377 | 991 | } else { |
0c9fd022 JR |
992 | struct sw_flow_actions *old_acts; |
993 | ||
d6569377 BP |
994 | /* Bail out if we're not allowed to modify an existing flow. |
995 | * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL | |
996 | * because Generic Netlink treats the latter as a dump | |
997 | * request. We also accept NLM_F_EXCL in case that bug ever | |
998 | * gets fixed. | |
999 | */ | |
6740b721 JR |
1000 | if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE |
1001 | | NLM_F_EXCL))) { | |
1002 | error = -EEXIST; | |
cd2a59e9 | 1003 | goto err_unlock_ovs; |
6740b721 | 1004 | } |
bc619e29 JS |
1005 | /* The flow identifier has to be the same for flow updates. |
1006 | * Look for any overlapping flow. | |
1007 | */ | |
1008 | if (unlikely(!ovs_flow_cmp(flow, &match))) { | |
1009 | if (ovs_identifier_is_key(&flow->id)) | |
1010 | flow = ovs_flow_tbl_lookup_exact(&dp->table, | |
1011 | &match); | |
1012 | else /* UFID matches but key is different */ | |
1013 | flow = NULL; | |
3440e4bc AW |
1014 | if (!flow) { |
1015 | error = -ENOENT; | |
1016 | goto err_unlock_ovs; | |
1017 | } | |
6740b721 | 1018 | } |
0c9fd022 JR |
1019 | /* Update actions. */ |
1020 | old_acts = ovsl_dereference(flow->sf_acts); | |
1021 | rcu_assign_pointer(flow->sf_acts, acts); | |
0c9fd022 | 1022 | |
6740b721 | 1023 | if (unlikely(reply)) { |
2c622e5a | 1024 | error = ovs_flow_cmd_fill_info(flow, |
6740b721 JR |
1025 | ovs_header->dp_ifindex, |
1026 | reply, info->snd_portid, | |
1027 | info->snd_seq, 0, | |
bc619e29 JS |
1028 | OVS_FLOW_CMD_NEW, |
1029 | ufid_flags); | |
6740b721 JR |
1030 | BUG_ON(error < 0); |
1031 | } | |
1032 | ovs_unlock(); | |
0c9fd022 | 1033 | |
e23775f2 | 1034 | ovs_nla_free_flow_actions_rcu(old_acts); |
6740b721 | 1035 | ovs_flow_free(new_flow, false); |
0c9fd022 | 1036 | } |
6740b721 JR |
1037 | |
1038 | if (reply) | |
cb25142c | 1039 | ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info); |
0c9fd022 JR |
1040 | return 0; |
1041 | ||
0c9fd022 JR |
1042 | err_unlock_ovs: |
1043 | ovs_unlock(); | |
6740b721 JR |
1044 | kfree_skb(reply); |
1045 | err_kfree_acts: | |
e23775f2 | 1046 | ovs_nla_free_flow_actions(acts); |
6740b721 JR |
1047 | err_kfree_flow: |
1048 | ovs_flow_free(new_flow, false); | |
0c9fd022 JR |
1049 | error: |
1050 | return error; | |
1051 | } | |
1052 | ||
cc561abf PS |
1053 | /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */ |
1054 | static struct sw_flow_actions *get_flow_actions(const struct nlattr *a, | |
1055 | const struct sw_flow_key *key, | |
9233cef7 JR |
1056 | const struct sw_flow_mask *mask, |
1057 | bool log) | |
cc561abf PS |
1058 | { |
1059 | struct sw_flow_actions *acts; | |
1060 | struct sw_flow_key masked_key; | |
1061 | int error; | |
1062 | ||
ad4adec2 | 1063 | ovs_flow_mask_key(&masked_key, key, true, mask); |
9233cef7 | 1064 | error = ovs_nla_copy_actions(a, &masked_key, &acts, log); |
cc561abf | 1065 | if (error) { |
9233cef7 | 1066 | OVS_NLERR(log, |
7d16c847 | 1067 | "Actions may not be safe on all matching packets"); |
cc561abf PS |
1068 | return ERR_PTR(error); |
1069 | } | |
1070 | ||
1071 | return acts; | |
1072 | } | |
1073 | ||
0c9fd022 JR |
1074 | static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info) |
1075 | { | |
1076 | struct nlattr **a = info->attrs; | |
1077 | struct ovs_header *ovs_header = info->userhdr; | |
1d2a1b5f | 1078 | struct sw_flow_key key; |
0c9fd022 JR |
1079 | struct sw_flow *flow; |
1080 | struct sw_flow_mask mask; | |
1081 | struct sk_buff *reply = NULL; | |
1082 | struct datapath *dp; | |
6740b721 | 1083 | struct sw_flow_actions *old_acts = NULL, *acts = NULL; |
0c9fd022 | 1084 | struct sw_flow_match match; |
bc619e29 JS |
1085 | struct sw_flow_id sfid; |
1086 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); | |
0c9fd022 | 1087 | int error; |
9233cef7 | 1088 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
bc619e29 | 1089 | bool ufid_present; |
0c9fd022 JR |
1090 | |
1091 | /* Extract key. */ | |
1092 | error = -EINVAL; | |
a473df5b | 1093 | if (!a[OVS_FLOW_ATTR_KEY]) { |
9233cef7 | 1094 | OVS_NLERR(log, "Flow key attribute not present in set flow."); |
0c9fd022 | 1095 | goto error; |
a473df5b | 1096 | } |
0c9fd022 | 1097 | |
bc619e29 | 1098 | ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log); |
0c9fd022 | 1099 | ovs_match_init(&match, &key, &mask); |
9233cef7 JR |
1100 | error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], |
1101 | a[OVS_FLOW_ATTR_MASK], log); | |
0c9fd022 JR |
1102 | if (error) |
1103 | goto error; | |
d6569377 | 1104 | |
0c9fd022 JR |
1105 | /* Validate actions. */ |
1106 | if (a[OVS_FLOW_ATTR_ACTIONS]) { | |
9233cef7 JR |
1107 | acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask, |
1108 | log); | |
cc561abf PS |
1109 | if (IS_ERR(acts)) { |
1110 | error = PTR_ERR(acts); | |
0c9fd022 | 1111 | goto error; |
6740b721 | 1112 | } |
6740b721 | 1113 | |
ff27161e | 1114 | /* Can allocate before locking if have acts. */ |
bc619e29 JS |
1115 | reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false, |
1116 | ufid_flags); | |
6740b721 JR |
1117 | if (IS_ERR(reply)) { |
1118 | error = PTR_ERR(reply); | |
1119 | goto err_kfree_acts; | |
90b8c2f7 | 1120 | } |
0c9fd022 JR |
1121 | } |
1122 | ||
1123 | ovs_lock(); | |
1124 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); | |
6740b721 JR |
1125 | if (unlikely(!dp)) { |
1126 | error = -ENODEV; | |
0c9fd022 | 1127 | goto err_unlock_ovs; |
6740b721 | 1128 | } |
0c9fd022 | 1129 | /* Check that the flow exists. */ |
bc619e29 JS |
1130 | if (ufid_present) |
1131 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid); | |
1132 | else | |
1133 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); | |
6740b721 JR |
1134 | if (unlikely(!flow)) { |
1135 | error = -ENOENT; | |
0c9fd022 | 1136 | goto err_unlock_ovs; |
6740b721 | 1137 | } |
3440e4bc | 1138 | |
0c9fd022 | 1139 | /* Update actions, if present. */ |
6740b721 | 1140 | if (likely(acts)) { |
0c9fd022 JR |
1141 | old_acts = ovsl_dereference(flow->sf_acts); |
1142 | rcu_assign_pointer(flow->sf_acts, acts); | |
6740b721 JR |
1143 | |
1144 | if (unlikely(reply)) { | |
2c622e5a | 1145 | error = ovs_flow_cmd_fill_info(flow, |
6740b721 JR |
1146 | ovs_header->dp_ifindex, |
1147 | reply, info->snd_portid, | |
1148 | info->snd_seq, 0, | |
bc619e29 JS |
1149 | OVS_FLOW_CMD_NEW, |
1150 | ufid_flags); | |
6740b721 JR |
1151 | BUG_ON(error < 0); |
1152 | } | |
1153 | } else { | |
1154 | /* Could not alloc without acts before locking. */ | |
7d16c847 | 1155 | reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, |
bc619e29 JS |
1156 | info, OVS_FLOW_CMD_NEW, false, |
1157 | ufid_flags); | |
1158 | ||
6740b721 JR |
1159 | if (unlikely(IS_ERR(reply))) { |
1160 | error = PTR_ERR(reply); | |
1161 | goto err_unlock_ovs; | |
1162 | } | |
9c52546b | 1163 | } |
0c9fd022 | 1164 | |
0c9fd022 JR |
1165 | /* Clear stats. */ |
1166 | if (a[OVS_FLOW_ATTR_CLEAR]) | |
1167 | ovs_flow_stats_clear(flow); | |
cd2a59e9 | 1168 | ovs_unlock(); |
37a1300c | 1169 | |
6740b721 | 1170 | if (reply) |
cb25142c | 1171 | ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info); |
6740b721 | 1172 | if (old_acts) |
e23775f2 | 1173 | ovs_nla_free_flow_actions_rcu(old_acts); |
7d16c847 | 1174 | |
d6569377 | 1175 | return 0; |
704a1e09 | 1176 | |
cd2a59e9 PS |
1177 | err_unlock_ovs: |
1178 | ovs_unlock(); | |
6740b721 JR |
1179 | kfree_skb(reply); |
1180 | err_kfree_acts: | |
e23775f2 | 1181 | ovs_nla_free_flow_actions(acts); |
37a1300c | 1182 | error: |
9c52546b | 1183 | return error; |
704a1e09 BP |
1184 | } |
1185 | ||
df2c07f4 | 1186 | static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) |
704a1e09 | 1187 | { |
37a1300c | 1188 | struct nlattr **a = info->attrs; |
df2c07f4 | 1189 | struct ovs_header *ovs_header = info->userhdr; |
37a1300c | 1190 | struct sw_flow_key key; |
37a1300c | 1191 | struct sk_buff *reply; |
704a1e09 | 1192 | struct sw_flow *flow; |
9c52546b | 1193 | struct datapath *dp; |
a1c564be | 1194 | struct sw_flow_match match; |
bc619e29 JS |
1195 | struct sw_flow_id ufid; |
1196 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); | |
1197 | int err = 0; | |
9233cef7 | 1198 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
bc619e29 | 1199 | bool ufid_present; |
704a1e09 | 1200 | |
bc619e29 JS |
1201 | ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); |
1202 | if (a[OVS_FLOW_ATTR_KEY]) { | |
1203 | ovs_match_init(&match, &key, NULL); | |
1204 | err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL, | |
1205 | log); | |
1206 | } else if (!ufid_present) { | |
9233cef7 JR |
1207 | OVS_NLERR(log, |
1208 | "Flow get message rejected, Key attribute missing."); | |
bc619e29 | 1209 | err = -EINVAL; |
1b936472 | 1210 | } |
37a1300c BP |
1211 | if (err) |
1212 | return err; | |
704a1e09 | 1213 | |
cd2a59e9 | 1214 | ovs_lock(); |
2a4999f3 | 1215 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
cd2a59e9 PS |
1216 | if (!dp) { |
1217 | err = -ENODEV; | |
1218 | goto unlock; | |
1219 | } | |
704a1e09 | 1220 | |
bc619e29 JS |
1221 | if (ufid_present) |
1222 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid); | |
1223 | else | |
1224 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); | |
3440e4bc | 1225 | if (!flow) { |
cd2a59e9 PS |
1226 | err = -ENOENT; |
1227 | goto unlock; | |
1228 | } | |
d6569377 | 1229 | |
7d16c847 | 1230 | reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info, |
bc619e29 | 1231 | OVS_FLOW_CMD_NEW, true, ufid_flags); |
cd2a59e9 PS |
1232 | if (IS_ERR(reply)) { |
1233 | err = PTR_ERR(reply); | |
1234 | goto unlock; | |
1235 | } | |
36956a7d | 1236 | |
cd2a59e9 | 1237 | ovs_unlock(); |
37a1300c | 1238 | return genlmsg_reply(reply, info); |
cd2a59e9 PS |
1239 | unlock: |
1240 | ovs_unlock(); | |
1241 | return err; | |
d6569377 | 1242 | } |
9c52546b | 1243 | |
df2c07f4 | 1244 | static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) |
d6569377 | 1245 | { |
37a1300c | 1246 | struct nlattr **a = info->attrs; |
df2c07f4 | 1247 | struct ovs_header *ovs_header = info->userhdr; |
37a1300c | 1248 | struct sw_flow_key key; |
37a1300c | 1249 | struct sk_buff *reply; |
bc619e29 | 1250 | struct sw_flow *flow = NULL; |
d6569377 | 1251 | struct datapath *dp; |
a1c564be | 1252 | struct sw_flow_match match; |
bc619e29 JS |
1253 | struct sw_flow_id ufid; |
1254 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); | |
d6569377 | 1255 | int err; |
9233cef7 | 1256 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
bc619e29 | 1257 | bool ufid_present; |
36956a7d | 1258 | |
bc619e29 JS |
1259 | ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); |
1260 | if (a[OVS_FLOW_ATTR_KEY]) { | |
cde7f3ba | 1261 | ovs_match_init(&match, &key, NULL); |
9233cef7 JR |
1262 | err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL, |
1263 | log); | |
cde7f3ba JR |
1264 | if (unlikely(err)) |
1265 | return err; | |
1266 | } | |
1267 | ||
cd2a59e9 | 1268 | ovs_lock(); |
2a4999f3 | 1269 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
cde7f3ba | 1270 | if (unlikely(!dp)) { |
cd2a59e9 PS |
1271 | err = -ENODEV; |
1272 | goto unlock; | |
1273 | } | |
7d16c847 | 1274 | |
bc619e29 | 1275 | if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) { |
994dc286 | 1276 | err = ovs_flow_tbl_flush(&dp->table); |
cd2a59e9 PS |
1277 | goto unlock; |
1278 | } | |
7d16c847 | 1279 | |
bc619e29 JS |
1280 | if (ufid_present) |
1281 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid); | |
1282 | else | |
1283 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); | |
3440e4bc | 1284 | if (unlikely(!flow)) { |
cd2a59e9 PS |
1285 | err = -ENOENT; |
1286 | goto unlock; | |
1287 | } | |
d6569377 | 1288 | |
994dc286 | 1289 | ovs_flow_tbl_remove(&dp->table, flow); |
cde7f3ba | 1290 | ovs_unlock(); |
37a1300c | 1291 | |
46051cf8 | 1292 | reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts), |
bc619e29 | 1293 | &flow->id, info, false, ufid_flags); |
cde7f3ba JR |
1294 | |
1295 | if (likely(reply)) { | |
1296 | if (likely(!IS_ERR(reply))) { | |
7d16c847 PS |
1297 | rcu_read_lock(); /*To keep RCU checker happy. */ |
1298 | err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, | |
cde7f3ba JR |
1299 | reply, info->snd_portid, |
1300 | info->snd_seq, 0, | |
bc619e29 JS |
1301 | OVS_FLOW_CMD_DEL, |
1302 | ufid_flags); | |
cde7f3ba JR |
1303 | rcu_read_unlock(); |
1304 | BUG_ON(err < 0); | |
cb25142c | 1305 | ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info); |
cde7f3ba | 1306 | } else { |
cb25142c PS |
1307 | genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0, |
1308 | GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply)); | |
1309 | ||
cde7f3ba | 1310 | } |
afad3556 | 1311 | } |
37a1300c | 1312 | |
a1c564be | 1313 | ovs_flow_free(flow, true); |
37a1300c | 1314 | return 0; |
cd2a59e9 PS |
1315 | unlock: |
1316 | ovs_unlock(); | |
1317 | return err; | |
37a1300c BP |
1318 | } |
1319 | ||
df2c07f4 | 1320 | static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
37a1300c | 1321 | { |
bc619e29 | 1322 | struct nlattr *a[__OVS_FLOW_ATTR_MAX]; |
df2c07f4 | 1323 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
994dc286 | 1324 | struct table_instance *ti; |
37a1300c | 1325 | struct datapath *dp; |
bc619e29 JS |
1326 | u32 ufid_flags; |
1327 | int err; | |
1328 | ||
1329 | err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a, | |
1330 | OVS_FLOW_ATTR_MAX, flow_policy); | |
1331 | if (err) | |
1332 | return err; | |
1333 | ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); | |
37a1300c | 1334 | |
f44ccce1 | 1335 | rcu_read_lock(); |
01ac0970 | 1336 | dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); |
cd2a59e9 | 1337 | if (!dp) { |
f44ccce1 | 1338 | rcu_read_unlock(); |
37a1300c | 1339 | return -ENODEV; |
cd2a59e9 | 1340 | } |
37a1300c | 1341 | |
994dc286 | 1342 | ti = rcu_dereference(dp->table.ti); |
37a1300c | 1343 | for (;;) { |
37a1300c BP |
1344 | struct sw_flow *flow; |
1345 | u32 bucket, obj; | |
1346 | ||
1347 | bucket = cb->args[0]; | |
1348 | obj = cb->args[1]; | |
994dc286 | 1349 | flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj); |
3544358a | 1350 | if (!flow) |
37a1300c BP |
1351 | break; |
1352 | ||
2c622e5a | 1353 | if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb, |
28aea917 | 1354 | NETLINK_CB(cb->skb).portid, |
37a1300c | 1355 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
bc619e29 | 1356 | OVS_FLOW_CMD_NEW, ufid_flags) < 0) |
37a1300c BP |
1357 | break; |
1358 | ||
1359 | cb->args[0] = bucket; | |
1360 | cb->args[1] = obj; | |
1361 | } | |
f44ccce1 | 1362 | rcu_read_unlock(); |
37a1300c | 1363 | return skb->len; |
704a1e09 BP |
1364 | } |
1365 | ||
cb25142c PS |
1366 | static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { |
1367 | [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, | |
9233cef7 | 1368 | [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED }, |
cb25142c PS |
1369 | [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, |
1370 | [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, | |
9233cef7 | 1371 | [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG }, |
bc619e29 JS |
1372 | [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 }, |
1373 | [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 }, | |
cb25142c PS |
1374 | }; |
1375 | ||
18fd3a52 | 1376 | static struct genl_ops dp_flow_genl_ops[] = { |
df2c07f4 | 1377 | { .cmd = OVS_FLOW_CMD_NEW, |
37a1300c BP |
1378 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1379 | .policy = flow_policy, | |
0c9fd022 | 1380 | .doit = ovs_flow_cmd_new |
37a1300c | 1381 | }, |
df2c07f4 | 1382 | { .cmd = OVS_FLOW_CMD_DEL, |
37a1300c BP |
1383 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1384 | .policy = flow_policy, | |
df2c07f4 | 1385 | .doit = ovs_flow_cmd_del |
37a1300c | 1386 | }, |
df2c07f4 | 1387 | { .cmd = OVS_FLOW_CMD_GET, |
37a1300c BP |
1388 | .flags = 0, /* OK for unprivileged users. */ |
1389 | .policy = flow_policy, | |
df2c07f4 JP |
1390 | .doit = ovs_flow_cmd_get, |
1391 | .dumpit = ovs_flow_cmd_dump | |
37a1300c | 1392 | }, |
df2c07f4 | 1393 | { .cmd = OVS_FLOW_CMD_SET, |
37a1300c BP |
1394 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1395 | .policy = flow_policy, | |
0c9fd022 | 1396 | .doit = ovs_flow_cmd_set, |
37a1300c BP |
1397 | }, |
1398 | }; | |
1399 | ||
cb25142c | 1400 | static struct genl_family dp_flow_genl_family = { |
aaff4b55 | 1401 | .id = GENL_ID_GENERATE, |
df2c07f4 | 1402 | .hdrsize = sizeof(struct ovs_header), |
cb25142c PS |
1403 | .name = OVS_FLOW_FAMILY, |
1404 | .version = OVS_FLOW_VERSION, | |
1405 | .maxattr = OVS_FLOW_ATTR_MAX, | |
b3dcb73c | 1406 | .netnsok = true, |
cb25142c PS |
1407 | .parallel_ops = true, |
1408 | .ops = dp_flow_genl_ops, | |
1409 | .n_ops = ARRAY_SIZE(dp_flow_genl_ops), | |
1410 | .mcgrps = &ovs_dp_flow_multicast_group, | |
1411 | .n_mcgrps = 1, | |
aaff4b55 BP |
1412 | }; |
1413 | ||
0afa2373 TG |
1414 | static size_t ovs_dp_cmd_msg_size(void) |
1415 | { | |
1416 | size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); | |
1417 | ||
1418 | msgsize += nla_total_size(IFNAMSIZ); | |
1419 | msgsize += nla_total_size(sizeof(struct ovs_dp_stats)); | |
4fa72a95 | 1420 | msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats)); |
300af20a | 1421 | msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */ |
0afa2373 TG |
1422 | |
1423 | return msgsize; | |
1424 | } | |
1425 | ||
d637497c | 1426 | /* Called with ovs_mutex. */ |
df2c07f4 | 1427 | static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, |
28aea917 | 1428 | u32 portid, u32 seq, u32 flags, u8 cmd) |
064af421 | 1429 | { |
df2c07f4 | 1430 | struct ovs_header *ovs_header; |
e926dfe3 | 1431 | struct ovs_dp_stats dp_stats; |
4fa72a95 | 1432 | struct ovs_dp_megaflow_stats dp_megaflow_stats; |
064af421 BP |
1433 | int err; |
1434 | ||
28aea917 | 1435 | ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, |
aaff4b55 | 1436 | flags, cmd); |
df2c07f4 | 1437 | if (!ovs_header) |
aaff4b55 | 1438 | goto error; |
064af421 | 1439 | |
b063d9f0 | 1440 | ovs_header->dp_ifindex = get_dpifindex(dp); |
064af421 | 1441 | |
850b6b3b | 1442 | err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); |
064af421 | 1443 | if (err) |
d6569377 | 1444 | goto nla_put_failure; |
064af421 | 1445 | |
4fa72a95 AZ |
1446 | get_dp_stats(dp, &dp_stats, &dp_megaflow_stats); |
1447 | if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), | |
1448 | &dp_stats)) | |
1449 | goto nla_put_failure; | |
1450 | ||
1451 | if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS, | |
1452 | sizeof(struct ovs_dp_megaflow_stats), | |
1453 | &dp_megaflow_stats)) | |
c3cc8c03 | 1454 | goto nla_put_failure; |
d6569377 | 1455 | |
c58cc9a4 TG |
1456 | if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features)) |
1457 | goto nla_put_failure; | |
1458 | ||
23b48dc1 TG |
1459 | genlmsg_end(skb, ovs_header); |
1460 | return 0; | |
d6569377 BP |
1461 | |
1462 | nla_put_failure: | |
df2c07f4 | 1463 | genlmsg_cancel(skb, ovs_header); |
aaff4b55 BP |
1464 | error: |
1465 | return -EMSGSIZE; | |
d6569377 BP |
1466 | } |
1467 | ||
d81eef1b | 1468 | static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info) |
d6569377 | 1469 | { |
d81eef1b | 1470 | return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL); |
aaff4b55 | 1471 | } |
9dca7bd5 | 1472 | |
aa917006 | 1473 | /* Called with rcu_read_lock or ovs_mutex. */ |
2a4999f3 | 1474 | static struct datapath *lookup_datapath(struct net *net, |
f1f60b85 | 1475 | const struct ovs_header *ovs_header, |
6455100f | 1476 | struct nlattr *a[OVS_DP_ATTR_MAX + 1]) |
d6569377 | 1477 | { |
254f2dc8 BP |
1478 | struct datapath *dp; |
1479 | ||
df2c07f4 | 1480 | if (!a[OVS_DP_ATTR_NAME]) |
2a4999f3 | 1481 | dp = get_dp(net, ovs_header->dp_ifindex); |
254f2dc8 | 1482 | else { |
d6569377 | 1483 | struct vport *vport; |
d6569377 | 1484 | |
2a4999f3 | 1485 | vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); |
df2c07f4 | 1486 | dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; |
d6569377 | 1487 | } |
254f2dc8 | 1488 | return dp ? dp : ERR_PTR(-ENODEV); |
d6569377 BP |
1489 | } |
1490 | ||
94358dcf TG |
1491 | static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info) |
1492 | { | |
1493 | struct datapath *dp; | |
1494 | ||
1495 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); | |
09350a3d | 1496 | if (IS_ERR(dp)) |
94358dcf TG |
1497 | return; |
1498 | ||
1499 | WARN(dp->user_features, "Dropping previously announced user features\n"); | |
1500 | dp->user_features = 0; | |
1501 | } | |
1502 | ||
f1f60b85 | 1503 | static void ovs_dp_change(struct datapath *dp, struct nlattr *a[]) |
c58cc9a4 TG |
1504 | { |
1505 | if (a[OVS_DP_ATTR_USER_FEATURES]) | |
1506 | dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]); | |
1507 | } | |
1508 | ||
df2c07f4 | 1509 | static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) |
d6569377 | 1510 | { |
aaff4b55 | 1511 | struct nlattr **a = info->attrs; |
d6569377 | 1512 | struct vport_parms parms; |
aaff4b55 | 1513 | struct sk_buff *reply; |
d6569377 BP |
1514 | struct datapath *dp; |
1515 | struct vport *vport; | |
2a4999f3 | 1516 | struct ovs_net *ovs_net; |
95b1d73a | 1517 | int err, i; |
d6569377 | 1518 | |
d6569377 | 1519 | err = -EINVAL; |
ea36840f | 1520 | if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) |
aaff4b55 BP |
1521 | goto err; |
1522 | ||
d81eef1b JR |
1523 | reply = ovs_dp_cmd_alloc_info(info); |
1524 | if (!reply) | |
1525 | return -ENOMEM; | |
d6569377 | 1526 | |
d6569377 BP |
1527 | err = -ENOMEM; |
1528 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); | |
1529 | if (dp == NULL) | |
d81eef1b | 1530 | goto err_free_reply; |
2a4999f3 | 1531 | |
c0cddcec | 1532 | ovs_dp_set_net(dp, sock_net(skb->sk)); |
0ceaa66c | 1533 | |
d6569377 | 1534 | /* Allocate table. */ |
994dc286 PS |
1535 | err = ovs_flow_tbl_init(&dp->table); |
1536 | if (err) | |
d6569377 BP |
1537 | goto err_free_dp; |
1538 | ||
08fb1bbd | 1539 | dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu); |
99769a40 JG |
1540 | if (!dp->stats_percpu) { |
1541 | err = -ENOMEM; | |
1542 | goto err_destroy_table; | |
1543 | } | |
1544 | ||
95b1d73a PS |
1545 | dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), |
1546 | GFP_KERNEL); | |
1547 | if (!dp->ports) { | |
1548 | err = -ENOMEM; | |
1549 | goto err_destroy_percpu; | |
1550 | } | |
1551 | ||
1552 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) | |
1553 | INIT_HLIST_HEAD(&dp->ports[i]); | |
1554 | ||
d6569377 | 1555 | /* Set up our datapath device. */ |
df2c07f4 JP |
1556 | parms.name = nla_data(a[OVS_DP_ATTR_NAME]); |
1557 | parms.type = OVS_VPORT_TYPE_INTERNAL; | |
d6569377 BP |
1558 | parms.options = NULL; |
1559 | parms.dp = dp; | |
df2c07f4 | 1560 | parms.port_no = OVSP_LOCAL; |
beb1c69a | 1561 | parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID]; |
b063d9f0 | 1562 | |
c58cc9a4 TG |
1563 | ovs_dp_change(dp, a); |
1564 | ||
d81eef1b JR |
1565 | /* So far only local changes have been made, now need the lock. */ |
1566 | ovs_lock(); | |
1567 | ||
d6569377 BP |
1568 | vport = new_vport(&parms); |
1569 | if (IS_ERR(vport)) { | |
1570 | err = PTR_ERR(vport); | |
1571 | if (err == -EBUSY) | |
1572 | err = -EEXIST; | |
1573 | ||
94358dcf TG |
1574 | if (err == -EEXIST) { |
1575 | /* An outdated user space instance that does not understand | |
1576 | * the concept of user_features has attempted to create a new | |
1577 | * datapath and is likely to reuse it. Drop all user features. | |
1578 | */ | |
1579 | if (info->genlhdr->version < OVS_DP_VER_FEATURES) | |
1580 | ovs_dp_reset_user_features(skb, info); | |
1581 | } | |
1582 | ||
95b1d73a | 1583 | goto err_destroy_ports_array; |
d6569377 | 1584 | } |
d6569377 | 1585 | |
d81eef1b JR |
1586 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1587 | info->snd_seq, 0, OVS_DP_CMD_NEW); | |
1588 | BUG_ON(err < 0); | |
aaff4b55 | 1589 | |
2a4999f3 | 1590 | ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); |
fb93e9aa | 1591 | list_add_tail_rcu(&dp->list_node, &ovs_net->dps); |
a0fb56c1 | 1592 | |
cd2a59e9 | 1593 | ovs_unlock(); |
d6569377 | 1594 | |
cb25142c | 1595 | ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info); |
d6569377 BP |
1596 | return 0; |
1597 | ||
95b1d73a | 1598 | err_destroy_ports_array: |
d81eef1b | 1599 | ovs_unlock(); |
95b1d73a | 1600 | kfree(dp->ports); |
99769a40 JG |
1601 | err_destroy_percpu: |
1602 | free_percpu(dp->stats_percpu); | |
d6569377 | 1603 | err_destroy_table: |
e379e4d1 | 1604 | ovs_flow_tbl_destroy(&dp->table); |
d6569377 | 1605 | err_free_dp: |
d6569377 | 1606 | kfree(dp); |
d81eef1b JR |
1607 | err_free_reply: |
1608 | kfree_skb(reply); | |
d6569377 | 1609 | err: |
064af421 BP |
1610 | return err; |
1611 | } | |
1612 | ||
cd2a59e9 | 1613 | /* Called with ovs_mutex. */ |
2a4999f3 | 1614 | static void __dp_destroy(struct datapath *dp) |
44e05eca | 1615 | { |
95b1d73a | 1616 | int i; |
44e05eca | 1617 | |
95b1d73a PS |
1618 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { |
1619 | struct vport *vport; | |
f8dfbcb7 | 1620 | struct hlist_node *n; |
95b1d73a | 1621 | |
f8dfbcb7 | 1622 | hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node) |
95b1d73a PS |
1623 | if (vport->port_no != OVSP_LOCAL) |
1624 | ovs_dp_detach_port(vport); | |
1625 | } | |
ed099e92 | 1626 | |
fb93e9aa | 1627 | list_del_rcu(&dp->list_node); |
ed099e92 | 1628 | |
cd2a59e9 | 1629 | /* OVSP_LOCAL is datapath internal port. We need to make sure that |
d103f479 AZ |
1630 | * all ports in datapath are destroyed first before freeing datapath. |
1631 | */ | |
cd2a59e9 | 1632 | ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); |
99620d2c | 1633 | |
d103f479 | 1634 | /* RCU destroy the flow table */ |
ed099e92 | 1635 | call_rcu(&dp->rcu, destroy_dp_rcu); |
2a4999f3 PS |
1636 | } |
1637 | ||
1638 | static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1639 | { | |
1640 | struct sk_buff *reply; | |
1641 | struct datapath *dp; | |
1642 | int err; | |
1643 | ||
d81eef1b JR |
1644 | reply = ovs_dp_cmd_alloc_info(info); |
1645 | if (!reply) | |
1646 | return -ENOMEM; | |
1647 | ||
cd2a59e9 | 1648 | ovs_lock(); |
2a4999f3 PS |
1649 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
1650 | err = PTR_ERR(dp); | |
1651 | if (IS_ERR(dp)) | |
d81eef1b | 1652 | goto err_unlock_free; |
2a4999f3 | 1653 | |
d81eef1b JR |
1654 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1655 | info->snd_seq, 0, OVS_DP_CMD_DEL); | |
1656 | BUG_ON(err < 0); | |
2a4999f3 PS |
1657 | |
1658 | __dp_destroy(dp); | |
d81eef1b | 1659 | ovs_unlock(); |
7d16c847 | 1660 | |
cb25142c | 1661 | ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info); |
99620d2c | 1662 | return 0; |
d81eef1b JR |
1663 | |
1664 | err_unlock_free: | |
cd2a59e9 | 1665 | ovs_unlock(); |
d81eef1b | 1666 | kfree_skb(reply); |
cd2a59e9 | 1667 | return err; |
44e05eca BP |
1668 | } |
1669 | ||
df2c07f4 | 1670 | static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) |
064af421 | 1671 | { |
aaff4b55 | 1672 | struct sk_buff *reply; |
d6569377 | 1673 | struct datapath *dp; |
d6569377 | 1674 | int err; |
064af421 | 1675 | |
d81eef1b JR |
1676 | reply = ovs_dp_cmd_alloc_info(info); |
1677 | if (!reply) | |
1678 | return -ENOMEM; | |
1679 | ||
cd2a59e9 | 1680 | ovs_lock(); |
2a4999f3 | 1681 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
cd2a59e9 | 1682 | err = PTR_ERR(dp); |
d6569377 | 1683 | if (IS_ERR(dp)) |
d81eef1b | 1684 | goto err_unlock_free; |
38c6ecbc | 1685 | |
c58cc9a4 TG |
1686 | ovs_dp_change(dp, info->attrs); |
1687 | ||
d81eef1b JR |
1688 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1689 | info->snd_seq, 0, OVS_DP_CMD_NEW); | |
1690 | BUG_ON(err < 0); | |
a0fb56c1 | 1691 | |
cd2a59e9 | 1692 | ovs_unlock(); |
7d16c847 | 1693 | |
cb25142c | 1694 | ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info); |
aaff4b55 | 1695 | return 0; |
d81eef1b JR |
1696 | |
1697 | err_unlock_free: | |
cd2a59e9 | 1698 | ovs_unlock(); |
d81eef1b | 1699 | kfree_skb(reply); |
cd2a59e9 | 1700 | return err; |
064af421 BP |
1701 | } |
1702 | ||
df2c07f4 | 1703 | static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) |
1dcf111b | 1704 | { |
aaff4b55 | 1705 | struct sk_buff *reply; |
d6569377 | 1706 | struct datapath *dp; |
d6569377 | 1707 | int err; |
1dcf111b | 1708 | |
d81eef1b JR |
1709 | reply = ovs_dp_cmd_alloc_info(info); |
1710 | if (!reply) | |
1711 | return -ENOMEM; | |
1712 | ||
d637497c | 1713 | ovs_lock(); |
2a4999f3 | 1714 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
cd2a59e9 PS |
1715 | if (IS_ERR(dp)) { |
1716 | err = PTR_ERR(dp); | |
d81eef1b | 1717 | goto err_unlock_free; |
cd2a59e9 | 1718 | } |
d81eef1b JR |
1719 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
1720 | info->snd_seq, 0, OVS_DP_CMD_NEW); | |
1721 | BUG_ON(err < 0); | |
d637497c | 1722 | ovs_unlock(); |
aaff4b55 BP |
1723 | |
1724 | return genlmsg_reply(reply, info); | |
cd2a59e9 | 1725 | |
d81eef1b | 1726 | err_unlock_free: |
d637497c | 1727 | ovs_unlock(); |
d81eef1b | 1728 | kfree_skb(reply); |
cd2a59e9 | 1729 | return err; |
1dcf111b JP |
1730 | } |
1731 | ||
df2c07f4 | 1732 | static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
a7786963 | 1733 | { |
2a4999f3 | 1734 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); |
254f2dc8 BP |
1735 | struct datapath *dp; |
1736 | int skip = cb->args[0]; | |
1737 | int i = 0; | |
a7786963 | 1738 | |
d637497c PS |
1739 | ovs_lock(); |
1740 | list_for_each_entry(dp, &ovs_net->dps, list_node) { | |
a2bab2f0 | 1741 | if (i >= skip && |
28aea917 | 1742 | ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, |
aaff4b55 | 1743 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
df2c07f4 | 1744 | OVS_DP_CMD_NEW) < 0) |
aaff4b55 | 1745 | break; |
254f2dc8 | 1746 | i++; |
a7786963 | 1747 | } |
d637497c | 1748 | ovs_unlock(); |
aaff4b55 | 1749 | |
254f2dc8 BP |
1750 | cb->args[0] = i; |
1751 | ||
aaff4b55 | 1752 | return skb->len; |
c19e6535 BP |
1753 | } |
1754 | ||
cb25142c PS |
1755 | static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { |
1756 | [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, | |
1757 | [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, | |
1758 | [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 }, | |
1759 | }; | |
1760 | ||
18fd3a52 | 1761 | static struct genl_ops dp_datapath_genl_ops[] = { |
df2c07f4 | 1762 | { .cmd = OVS_DP_CMD_NEW, |
aaff4b55 BP |
1763 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1764 | .policy = datapath_policy, | |
df2c07f4 | 1765 | .doit = ovs_dp_cmd_new |
aaff4b55 | 1766 | }, |
df2c07f4 | 1767 | { .cmd = OVS_DP_CMD_DEL, |
aaff4b55 BP |
1768 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1769 | .policy = datapath_policy, | |
df2c07f4 | 1770 | .doit = ovs_dp_cmd_del |
aaff4b55 | 1771 | }, |
df2c07f4 | 1772 | { .cmd = OVS_DP_CMD_GET, |
aaff4b55 BP |
1773 | .flags = 0, /* OK for unprivileged users. */ |
1774 | .policy = datapath_policy, | |
df2c07f4 JP |
1775 | .doit = ovs_dp_cmd_get, |
1776 | .dumpit = ovs_dp_cmd_dump | |
aaff4b55 | 1777 | }, |
df2c07f4 | 1778 | { .cmd = OVS_DP_CMD_SET, |
aaff4b55 BP |
1779 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
1780 | .policy = datapath_policy, | |
df2c07f4 | 1781 | .doit = ovs_dp_cmd_set, |
aaff4b55 BP |
1782 | }, |
1783 | }; | |
1784 | ||
cb25142c | 1785 | static struct genl_family dp_datapath_genl_family = { |
f0fef760 | 1786 | .id = GENL_ID_GENERATE, |
df2c07f4 | 1787 | .hdrsize = sizeof(struct ovs_header), |
cb25142c PS |
1788 | .name = OVS_DATAPATH_FAMILY, |
1789 | .version = OVS_DATAPATH_VERSION, | |
1790 | .maxattr = OVS_DP_ATTR_MAX, | |
b3dcb73c | 1791 | .netnsok = true, |
cb25142c PS |
1792 | .parallel_ops = true, |
1793 | .ops = dp_datapath_genl_ops, | |
1794 | .n_ops = ARRAY_SIZE(dp_datapath_genl_ops), | |
1795 | .mcgrps = &ovs_dp_datapath_multicast_group, | |
1796 | .n_mcgrps = 1, | |
f0fef760 BP |
1797 | }; |
1798 | ||
cd2a59e9 | 1799 | /* Called with ovs_mutex or RCU read lock. */ |
df2c07f4 | 1800 | static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, |
28aea917 | 1801 | u32 portid, u32 seq, u32 flags, u8 cmd) |
064af421 | 1802 | { |
df2c07f4 | 1803 | struct ovs_header *ovs_header; |
e926dfe3 | 1804 | struct ovs_vport_stats vport_stats; |
c19e6535 BP |
1805 | int err; |
1806 | ||
28aea917 | 1807 | ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, |
f0fef760 | 1808 | flags, cmd); |
df2c07f4 | 1809 | if (!ovs_header) |
f0fef760 | 1810 | return -EMSGSIZE; |
c19e6535 | 1811 | |
99769a40 | 1812 | ovs_header->dp_ifindex = get_dpifindex(vport->dp); |
c19e6535 | 1813 | |
c3cc8c03 DM |
1814 | if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || |
1815 | nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || | |
e23775f2 PS |
1816 | nla_put_string(skb, OVS_VPORT_ATTR_NAME, |
1817 | ovs_vport_name(vport))) | |
c3cc8c03 | 1818 | goto nla_put_failure; |
c19e6535 | 1819 | |
850b6b3b | 1820 | ovs_vport_get_stats(vport, &vport_stats); |
c3cc8c03 DM |
1821 | if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats), |
1822 | &vport_stats)) | |
1823 | goto nla_put_failure; | |
c19e6535 | 1824 | |
beb1c69a AW |
1825 | if (ovs_vport_get_upcall_portids(vport, skb)) |
1826 | goto nla_put_failure; | |
1827 | ||
850b6b3b | 1828 | err = ovs_vport_get_options(vport, skb); |
f0fef760 BP |
1829 | if (err == -EMSGSIZE) |
1830 | goto error; | |
c19e6535 | 1831 | |
23b48dc1 TG |
1832 | genlmsg_end(skb, ovs_header); |
1833 | return 0; | |
c19e6535 BP |
1834 | |
1835 | nla_put_failure: | |
1836 | err = -EMSGSIZE; | |
f0fef760 | 1837 | error: |
df2c07f4 | 1838 | genlmsg_cancel(skb, ovs_header); |
f0fef760 | 1839 | return err; |
064af421 BP |
1840 | } |
1841 | ||
d81eef1b JR |
1842 | static struct sk_buff *ovs_vport_cmd_alloc_info(void) |
1843 | { | |
1844 | return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
1845 | } | |
1846 | ||
1847 | /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */ | |
28aea917 | 1848 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid, |
f14d8083 | 1849 | u32 seq, u8 cmd) |
064af421 | 1850 | { |
c19e6535 | 1851 | struct sk_buff *skb; |
f0fef760 | 1852 | int retval; |
c19e6535 | 1853 | |
f0fef760 | 1854 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
c19e6535 BP |
1855 | if (!skb) |
1856 | return ERR_PTR(-ENOMEM); | |
1857 | ||
28aea917 | 1858 | retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd); |
c25ea534 JG |
1859 | BUG_ON(retval < 0); |
1860 | ||
c19e6535 | 1861 | return skb; |
f0fef760 | 1862 | } |
c19e6535 | 1863 | |
cd2a59e9 | 1864 | /* Called with ovs_mutex or RCU read lock. */ |
2a4999f3 | 1865 | static struct vport *lookup_vport(struct net *net, |
f1f60b85 | 1866 | const struct ovs_header *ovs_header, |
df2c07f4 | 1867 | struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) |
c19e6535 BP |
1868 | { |
1869 | struct datapath *dp; | |
1870 | struct vport *vport; | |
1871 | ||
df2c07f4 | 1872 | if (a[OVS_VPORT_ATTR_NAME]) { |
2a4999f3 | 1873 | vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); |
ed099e92 | 1874 | if (!vport) |
c19e6535 | 1875 | return ERR_PTR(-ENODEV); |
24ce832d BP |
1876 | if (ovs_header->dp_ifindex && |
1877 | ovs_header->dp_ifindex != get_dpifindex(vport->dp)) | |
1878 | return ERR_PTR(-ENODEV); | |
c19e6535 | 1879 | return vport; |
df2c07f4 JP |
1880 | } else if (a[OVS_VPORT_ATTR_PORT_NO]) { |
1881 | u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); | |
c19e6535 BP |
1882 | |
1883 | if (port_no >= DP_MAX_PORTS) | |
f0fef760 | 1884 | return ERR_PTR(-EFBIG); |
c19e6535 | 1885 | |
2a4999f3 | 1886 | dp = get_dp(net, ovs_header->dp_ifindex); |
c19e6535 BP |
1887 | if (!dp) |
1888 | return ERR_PTR(-ENODEV); | |
f2459fe7 | 1889 | |
cd2a59e9 | 1890 | vport = ovs_vport_ovsl_rcu(dp, port_no); |
ed099e92 | 1891 | if (!vport) |
17535c57 | 1892 | return ERR_PTR(-ENODEV); |
c19e6535 BP |
1893 | return vport; |
1894 | } else | |
1895 | return ERR_PTR(-EINVAL); | |
064af421 BP |
1896 | } |
1897 | ||
df2c07f4 | 1898 | static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) |
c19e6535 | 1899 | { |
f0fef760 | 1900 | struct nlattr **a = info->attrs; |
df2c07f4 | 1901 | struct ovs_header *ovs_header = info->userhdr; |
c19e6535 | 1902 | struct vport_parms parms; |
ed099e92 | 1903 | struct sk_buff *reply; |
c19e6535 | 1904 | struct vport *vport; |
c19e6535 | 1905 | struct datapath *dp; |
b0ec0f27 | 1906 | u32 port_no; |
c19e6535 | 1907 | int err; |
b0ec0f27 | 1908 | |
ea36840f BP |
1909 | if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || |
1910 | !a[OVS_VPORT_ATTR_UPCALL_PID]) | |
d81eef1b JR |
1911 | return -EINVAL; |
1912 | ||
1913 | port_no = a[OVS_VPORT_ATTR_PORT_NO] | |
1914 | ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0; | |
1915 | if (port_no >= DP_MAX_PORTS) | |
1916 | return -EFBIG; | |
1917 | ||
1918 | reply = ovs_vport_cmd_alloc_info(); | |
1919 | if (!reply) | |
1920 | return -ENOMEM; | |
f0fef760 | 1921 | |
cd2a59e9 | 1922 | ovs_lock(); |
5a38795f | 1923 | restart: |
2a4999f3 | 1924 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
c19e6535 BP |
1925 | err = -ENODEV; |
1926 | if (!dp) | |
d81eef1b | 1927 | goto exit_unlock_free; |
c19e6535 | 1928 | |
d81eef1b | 1929 | if (port_no) { |
cd2a59e9 | 1930 | vport = ovs_vport_ovsl(dp, port_no); |
c19e6535 BP |
1931 | err = -EBUSY; |
1932 | if (vport) | |
d81eef1b | 1933 | goto exit_unlock_free; |
c19e6535 BP |
1934 | } else { |
1935 | for (port_no = 1; ; port_no++) { | |
1936 | if (port_no >= DP_MAX_PORTS) { | |
1937 | err = -EFBIG; | |
d81eef1b | 1938 | goto exit_unlock_free; |
c19e6535 | 1939 | } |
cd2a59e9 | 1940 | vport = ovs_vport_ovsl(dp, port_no); |
c19e6535 BP |
1941 | if (!vport) |
1942 | break; | |
51d4d598 | 1943 | } |
064af421 | 1944 | } |
b0ec0f27 | 1945 | |
df2c07f4 JP |
1946 | parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); |
1947 | parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); | |
1948 | parms.options = a[OVS_VPORT_ATTR_OPTIONS]; | |
c19e6535 BP |
1949 | parms.dp = dp; |
1950 | parms.port_no = port_no; | |
beb1c69a | 1951 | parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID]; |
c19e6535 BP |
1952 | |
1953 | vport = new_vport(&parms); | |
1954 | err = PTR_ERR(vport); | |
5a38795f TG |
1955 | if (IS_ERR(vport)) { |
1956 | if (err == -EAGAIN) | |
1957 | goto restart; | |
d81eef1b | 1958 | goto exit_unlock_free; |
5a38795f | 1959 | } |
c19e6535 | 1960 | |
d81eef1b JR |
1961 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
1962 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
1963 | BUG_ON(err < 0); | |
1964 | ovs_unlock(); | |
e297c6b7 | 1965 | |
cb25142c | 1966 | ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info); |
d81eef1b | 1967 | return 0; |
c19e6535 | 1968 | |
d81eef1b | 1969 | exit_unlock_free: |
cd2a59e9 | 1970 | ovs_unlock(); |
d81eef1b | 1971 | kfree_skb(reply); |
c19e6535 | 1972 | return err; |
44e05eca BP |
1973 | } |
1974 | ||
df2c07f4 | 1975 | static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) |
44e05eca | 1976 | { |
f0fef760 BP |
1977 | struct nlattr **a = info->attrs; |
1978 | struct sk_buff *reply; | |
c19e6535 | 1979 | struct vport *vport; |
c19e6535 | 1980 | int err; |
44e05eca | 1981 | |
d81eef1b JR |
1982 | reply = ovs_vport_cmd_alloc_info(); |
1983 | if (!reply) | |
1984 | return -ENOMEM; | |
1985 | ||
cd2a59e9 | 1986 | ovs_lock(); |
2a4999f3 | 1987 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
c19e6535 BP |
1988 | err = PTR_ERR(vport); |
1989 | if (IS_ERR(vport)) | |
d81eef1b | 1990 | goto exit_unlock_free; |
44e05eca | 1991 | |
6455100f | 1992 | if (a[OVS_VPORT_ATTR_TYPE] && |
17ec1d04 | 1993 | nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) { |
4879d4c7 | 1994 | err = -EINVAL; |
d81eef1b | 1995 | goto exit_unlock_free; |
c25ea534 JG |
1996 | } |
1997 | ||
17ec1d04 | 1998 | if (a[OVS_VPORT_ATTR_OPTIONS]) { |
850b6b3b | 1999 | err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); |
17ec1d04 | 2000 | if (err) |
d81eef1b | 2001 | goto exit_unlock_free; |
17ec1d04 | 2002 | } |
1fc7083d | 2003 | |
beb1c69a | 2004 | if (a[OVS_VPORT_ATTR_UPCALL_PID]) { |
7d16c847 PS |
2005 | struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID]; |
2006 | ||
2007 | err = ovs_vport_set_upcall_portids(vport, ids); | |
beb1c69a AW |
2008 | if (err) |
2009 | goto exit_unlock_free; | |
2010 | } | |
c19e6535 | 2011 | |
c25ea534 JG |
2012 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
2013 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
2014 | BUG_ON(err < 0); | |
cd2a59e9 | 2015 | ovs_unlock(); |
d81eef1b | 2016 | |
cb25142c | 2017 | ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info); |
c25ea534 JG |
2018 | return 0; |
2019 | ||
d81eef1b | 2020 | exit_unlock_free: |
cd2a59e9 | 2021 | ovs_unlock(); |
d81eef1b | 2022 | kfree_skb(reply); |
c19e6535 | 2023 | return err; |
064af421 BP |
2024 | } |
2025 | ||
df2c07f4 | 2026 | static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) |
7c40efc9 | 2027 | { |
f0fef760 BP |
2028 | struct nlattr **a = info->attrs; |
2029 | struct sk_buff *reply; | |
c19e6535 | 2030 | struct vport *vport; |
c19e6535 BP |
2031 | int err; |
2032 | ||
d81eef1b JR |
2033 | reply = ovs_vport_cmd_alloc_info(); |
2034 | if (!reply) | |
2035 | return -ENOMEM; | |
2036 | ||
cd2a59e9 | 2037 | ovs_lock(); |
2a4999f3 | 2038 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
c19e6535 | 2039 | err = PTR_ERR(vport); |
f0fef760 | 2040 | if (IS_ERR(vport)) |
d81eef1b | 2041 | goto exit_unlock_free; |
c19e6535 | 2042 | |
df2c07f4 | 2043 | if (vport->port_no == OVSP_LOCAL) { |
f0fef760 | 2044 | err = -EINVAL; |
d81eef1b | 2045 | goto exit_unlock_free; |
f0fef760 BP |
2046 | } |
2047 | ||
d81eef1b JR |
2048 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
2049 | info->snd_seq, 0, OVS_VPORT_CMD_DEL); | |
2050 | BUG_ON(err < 0); | |
850b6b3b | 2051 | ovs_dp_detach_port(vport); |
d81eef1b | 2052 | ovs_unlock(); |
f0fef760 | 2053 | |
cb25142c | 2054 | ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info); |
d81eef1b | 2055 | return 0; |
f0fef760 | 2056 | |
d81eef1b | 2057 | exit_unlock_free: |
cd2a59e9 | 2058 | ovs_unlock(); |
d81eef1b | 2059 | kfree_skb(reply); |
c19e6535 | 2060 | return err; |
7c40efc9 BP |
2061 | } |
2062 | ||
df2c07f4 | 2063 | static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) |
7c40efc9 | 2064 | { |
f0fef760 | 2065 | struct nlattr **a = info->attrs; |
df2c07f4 | 2066 | struct ovs_header *ovs_header = info->userhdr; |
ed099e92 | 2067 | struct sk_buff *reply; |
c19e6535 | 2068 | struct vport *vport; |
c19e6535 BP |
2069 | int err; |
2070 | ||
d81eef1b JR |
2071 | reply = ovs_vport_cmd_alloc_info(); |
2072 | if (!reply) | |
2073 | return -ENOMEM; | |
2074 | ||
ed099e92 | 2075 | rcu_read_lock(); |
2a4999f3 | 2076 | vport = lookup_vport(sock_net(skb->sk), ovs_header, a); |
c19e6535 BP |
2077 | err = PTR_ERR(vport); |
2078 | if (IS_ERR(vport)) | |
d81eef1b JR |
2079 | goto exit_unlock_free; |
2080 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, | |
2081 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
2082 | BUG_ON(err < 0); | |
df2fa9b5 JG |
2083 | rcu_read_unlock(); |
2084 | ||
2085 | return genlmsg_reply(reply, info); | |
ed099e92 | 2086 | |
d81eef1b | 2087 | exit_unlock_free: |
ed099e92 | 2088 | rcu_read_unlock(); |
d81eef1b | 2089 | kfree_skb(reply); |
c19e6535 BP |
2090 | return err; |
2091 | } | |
2092 | ||
df2c07f4 | 2093 | static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
c19e6535 | 2094 | { |
df2c07f4 | 2095 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
c19e6535 | 2096 | struct datapath *dp; |
95b1d73a PS |
2097 | int bucket = cb->args[0], skip = cb->args[1]; |
2098 | int i, j = 0; | |
c19e6535 | 2099 | |
03fc2881 | 2100 | rcu_read_lock(); |
01ac0970 | 2101 | dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); |
03fc2881 JR |
2102 | if (!dp) { |
2103 | rcu_read_unlock(); | |
f0fef760 | 2104 | return -ENODEV; |
03fc2881 | 2105 | } |
95b1d73a | 2106 | for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { |
ed099e92 | 2107 | struct vport *vport; |
95b1d73a PS |
2108 | |
2109 | j = 0; | |
f8dfbcb7 | 2110 | hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { |
95b1d73a PS |
2111 | if (j >= skip && |
2112 | ovs_vport_cmd_fill_info(vport, skb, | |
28aea917 | 2113 | NETLINK_CB(cb->skb).portid, |
95b1d73a PS |
2114 | cb->nlh->nlmsg_seq, |
2115 | NLM_F_MULTI, | |
2116 | OVS_VPORT_CMD_NEW) < 0) | |
2117 | goto out; | |
2118 | ||
2119 | j++; | |
2120 | } | |
2121 | skip = 0; | |
c19e6535 | 2122 | } |
95b1d73a | 2123 | out: |
ed099e92 | 2124 | rcu_read_unlock(); |
c19e6535 | 2125 | |
95b1d73a PS |
2126 | cb->args[0] = i; |
2127 | cb->args[1] = j; | |
f0fef760 | 2128 | |
95b1d73a | 2129 | return skb->len; |
7c40efc9 BP |
2130 | } |
2131 | ||
cb25142c PS |
2132 | static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { |
2133 | [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, | |
2134 | [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, | |
2135 | [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, | |
2136 | [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, | |
2137 | [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 }, | |
2138 | [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, | |
2139 | }; | |
2140 | ||
18fd3a52 | 2141 | static struct genl_ops dp_vport_genl_ops[] = { |
df2c07f4 | 2142 | { .cmd = OVS_VPORT_CMD_NEW, |
f0fef760 BP |
2143 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
2144 | .policy = vport_policy, | |
df2c07f4 | 2145 | .doit = ovs_vport_cmd_new |
f0fef760 | 2146 | }, |
df2c07f4 | 2147 | { .cmd = OVS_VPORT_CMD_DEL, |
f0fef760 BP |
2148 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
2149 | .policy = vport_policy, | |
df2c07f4 | 2150 | .doit = ovs_vport_cmd_del |
f0fef760 | 2151 | }, |
df2c07f4 | 2152 | { .cmd = OVS_VPORT_CMD_GET, |
f0fef760 BP |
2153 | .flags = 0, /* OK for unprivileged users. */ |
2154 | .policy = vport_policy, | |
df2c07f4 JP |
2155 | .doit = ovs_vport_cmd_get, |
2156 | .dumpit = ovs_vport_cmd_dump | |
f0fef760 | 2157 | }, |
df2c07f4 | 2158 | { .cmd = OVS_VPORT_CMD_SET, |
f0fef760 BP |
2159 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
2160 | .policy = vport_policy, | |
df2c07f4 | 2161 | .doit = ovs_vport_cmd_set, |
f0fef760 BP |
2162 | }, |
2163 | }; | |
2164 | ||
cb25142c PS |
2165 | struct genl_family dp_vport_genl_family = { |
2166 | .id = GENL_ID_GENERATE, | |
2167 | .hdrsize = sizeof(struct ovs_header), | |
2168 | .name = OVS_VPORT_FAMILY, | |
2169 | .version = OVS_VPORT_VERSION, | |
2170 | .maxattr = OVS_VPORT_ATTR_MAX, | |
2171 | .netnsok = true, | |
2172 | .parallel_ops = true, | |
2173 | .ops = dp_vport_genl_ops, | |
2174 | .n_ops = ARRAY_SIZE(dp_vport_genl_ops), | |
2175 | .mcgrps = &ovs_dp_vport_multicast_group, | |
2176 | .n_mcgrps = 1, | |
982b8810 | 2177 | }; |
ed099e92 | 2178 | |
18fd3a52 | 2179 | static struct genl_family *dp_genl_families[] = { |
cb25142c PS |
2180 | &dp_datapath_genl_family, |
2181 | &dp_vport_genl_family, | |
2182 | &dp_flow_genl_family, | |
2183 | &dp_packet_genl_family, | |
982b8810 | 2184 | }; |
ed099e92 | 2185 | |
982b8810 BP |
2186 | static void dp_unregister_genl(int n_families) |
2187 | { | |
2188 | int i; | |
ed099e92 | 2189 | |
b867ca75 | 2190 | for (i = 0; i < n_families; i++) |
cb25142c | 2191 | genl_unregister_family(dp_genl_families[i]); |
ed099e92 BP |
2192 | } |
2193 | ||
982b8810 | 2194 | static int dp_register_genl(void) |
064af421 | 2195 | { |
982b8810 BP |
2196 | int err; |
2197 | int i; | |
064af421 | 2198 | |
982b8810 | 2199 | for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { |
064af421 | 2200 | |
cb25142c | 2201 | err = genl_register_family(dp_genl_families[i]); |
982b8810 BP |
2202 | if (err) |
2203 | goto error; | |
982b8810 | 2204 | } |
9cc8b4e4 | 2205 | |
982b8810 | 2206 | return 0; |
064af421 BP |
2207 | |
2208 | error: | |
cb25142c | 2209 | dp_unregister_genl(i); |
982b8810 | 2210 | return err; |
064af421 BP |
2211 | } |
2212 | ||
2a4999f3 PS |
2213 | static int __net_init ovs_init_net(struct net *net) |
2214 | { | |
2215 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2216 | ||
2217 | INIT_LIST_HEAD(&ovs_net->dps); | |
cd2a59e9 | 2218 | INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq); |
2a4999f3 PS |
2219 | return 0; |
2220 | } | |
2221 | ||
cabd5516 PS |
2222 | static void __net_exit list_vports_from_net(struct net *net, struct net *dnet, |
2223 | struct list_head *head) | |
2a4999f3 PS |
2224 | { |
2225 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
cabd5516 PS |
2226 | struct datapath *dp; |
2227 | ||
2228 | list_for_each_entry(dp, &ovs_net->dps, list_node) { | |
2229 | int i; | |
2230 | ||
2231 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { | |
2232 | struct vport *vport; | |
2233 | ||
2234 | hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) { | |
cabd5516 PS |
2235 | |
2236 | if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL) | |
2237 | continue; | |
2238 | ||
e23775f2 | 2239 | if (dev_net(vport->dev) == dnet) |
cabd5516 PS |
2240 | list_add(&vport->detach_list, head); |
2241 | } | |
2242 | } | |
2243 | } | |
2244 | } | |
2245 | ||
2246 | static void __net_exit ovs_exit_net(struct net *dnet) | |
2247 | { | |
2248 | struct datapath *dp, *dp_next; | |
2249 | struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id); | |
2250 | struct vport *vport, *vport_next; | |
2251 | struct net *net; | |
2252 | LIST_HEAD(head); | |
2a4999f3 | 2253 | |
cd2a59e9 PS |
2254 | ovs_lock(); |
2255 | list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) | |
2256 | __dp_destroy(dp); | |
cabd5516 PS |
2257 | |
2258 | rtnl_lock(); | |
2259 | for_each_net(net) | |
2260 | list_vports_from_net(net, dnet, &head); | |
2261 | rtnl_unlock(); | |
2262 | ||
2263 | /* Detach all vports from given namespace. */ | |
2264 | list_for_each_entry_safe(vport, vport_next, &head, detach_list) { | |
2265 | list_del(&vport->detach_list); | |
2266 | ovs_dp_detach_port(vport); | |
2267 | } | |
2268 | ||
cd2a59e9 PS |
2269 | ovs_unlock(); |
2270 | ||
2271 | cancel_work_sync(&ovs_net->dp_notify_work); | |
2a4999f3 PS |
2272 | } |
2273 | ||
2274 | static struct pernet_operations ovs_net_ops = { | |
2275 | .init = ovs_init_net, | |
2276 | .exit = ovs_exit_net, | |
2277 | .id = &ovs_net_id, | |
2278 | .size = sizeof(struct ovs_net), | |
2279 | }; | |
2280 | ||
637c8268 PS |
2281 | DEFINE_COMPAT_PNET_REG_FUNC(device); |
2282 | ||
22d24ebf BP |
2283 | static int __init dp_init(void) |
2284 | { | |
2285 | int err; | |
2286 | ||
f3d85db3 | 2287 | BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb)); |
22d24ebf | 2288 | |
26bfaeaa | 2289 | pr_info("Open vSwitch switching datapath %s\n", VERSION); |
064af421 | 2290 | |
595e069a | 2291 | err = compat_init(); |
3544358a | 2292 | if (err) |
533e96e7 | 2293 | goto error; |
3544358a | 2294 | |
595e069a JS |
2295 | err = action_fifos_init(); |
2296 | if (err) | |
2297 | goto error_compat_exit; | |
2298 | ||
5282e284 | 2299 | err = ovs_internal_dev_rtnl_link_register(); |
2c8c4fb7 AZ |
2300 | if (err) |
2301 | goto error_action_fifos_exit; | |
2302 | ||
5282e284 TG |
2303 | err = ovs_flow_init(); |
2304 | if (err) | |
2305 | goto error_unreg_rtnl_link; | |
2306 | ||
850b6b3b | 2307 | err = ovs_vport_init(); |
064af421 BP |
2308 | if (err) |
2309 | goto error_flow_exit; | |
2310 | ||
2a4999f3 | 2311 | err = register_pernet_device(&ovs_net_ops); |
f2459fe7 JG |
2312 | if (err) |
2313 | goto error_vport_exit; | |
2314 | ||
2a4999f3 PS |
2315 | err = register_netdevice_notifier(&ovs_dp_device_notifier); |
2316 | if (err) | |
2317 | goto error_netns_exit; | |
2318 | ||
5a38795f TG |
2319 | err = ovs_netdev_init(); |
2320 | if (err) | |
2321 | goto error_unreg_notifier; | |
2322 | ||
982b8810 BP |
2323 | err = dp_register_genl(); |
2324 | if (err < 0) | |
5a38795f | 2325 | goto error_unreg_netdev; |
982b8810 | 2326 | |
064af421 BP |
2327 | return 0; |
2328 | ||
5a38795f TG |
2329 | error_unreg_netdev: |
2330 | ovs_netdev_exit(); | |
064af421 | 2331 | error_unreg_notifier: |
850b6b3b | 2332 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
2a4999f3 PS |
2333 | error_netns_exit: |
2334 | unregister_pernet_device(&ovs_net_ops); | |
f2459fe7 | 2335 | error_vport_exit: |
850b6b3b | 2336 | ovs_vport_exit(); |
064af421 | 2337 | error_flow_exit: |
850b6b3b | 2338 | ovs_flow_exit(); |
5282e284 TG |
2339 | error_unreg_rtnl_link: |
2340 | ovs_internal_dev_rtnl_link_unregister(); | |
2c8c4fb7 AZ |
2341 | error_action_fifos_exit: |
2342 | action_fifos_exit(); | |
595e069a JS |
2343 | error_compat_exit: |
2344 | compat_exit(); | |
064af421 BP |
2345 | error: |
2346 | return err; | |
2347 | } | |
2348 | ||
2349 | static void dp_cleanup(void) | |
2350 | { | |
982b8810 | 2351 | dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); |
5a38795f | 2352 | ovs_netdev_exit(); |
850b6b3b | 2353 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
2a4999f3 PS |
2354 | unregister_pernet_device(&ovs_net_ops); |
2355 | rcu_barrier(); | |
850b6b3b JG |
2356 | ovs_vport_exit(); |
2357 | ovs_flow_exit(); | |
5282e284 | 2358 | ovs_internal_dev_rtnl_link_unregister(); |
2c8c4fb7 | 2359 | action_fifos_exit(); |
595e069a | 2360 | compat_exit(); |
064af421 BP |
2361 | } |
2362 | ||
2363 | module_init(dp_init); | |
2364 | module_exit(dp_cleanup); | |
2365 | ||
2366 | MODULE_DESCRIPTION("Open vSwitch switching datapath"); | |
2367 | MODULE_LICENSE("GPL"); | |
3d0666d2 | 2368 | MODULE_VERSION(VERSION); |