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