]>
Commit | Line | Data |
---|---|---|
96fba48f | 1 | /* |
de281153 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
96fba48f BP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
9fe3b9a2 BP |
18 | |
19 | #include "dpif-linux.h" | |
96fba48f | 20 | |
96fba48f BP |
21 | #include <ctype.h> |
22 | #include <errno.h> | |
23 | #include <fcntl.h> | |
24 | #include <inttypes.h> | |
25 | #include <net/if.h> | |
b90fa799 | 26 | #include <linux/types.h> |
aae51f53 | 27 | #include <linux/pkt_sched.h> |
e9e28be3 | 28 | #include <linux/rtnetlink.h> |
96fba48f | 29 | #include <linux/sockios.h> |
8522ba09 | 30 | #include <poll.h> |
96fba48f | 31 | #include <stdlib.h> |
8522ba09 | 32 | #include <strings.h> |
50f80534 | 33 | #include <sys/epoll.h> |
10dcf8de | 34 | #include <sys/stat.h> |
96fba48f BP |
35 | #include <unistd.h> |
36 | ||
773cd538 | 37 | #include "bitmap.h" |
96fba48f | 38 | #include "dpif-provider.h" |
80e5eed9 | 39 | #include "dynamic-string.h" |
eb8b28e7 | 40 | #include "flow.h" |
3abc4a1a | 41 | #include "netdev.h" |
032aa6a3 | 42 | #include "netdev-linux.h" |
c3827f61 | 43 | #include "netdev-vport.h" |
45c8d3a1 | 44 | #include "netlink-notifier.h" |
982b8810 | 45 | #include "netlink-socket.h" |
856081f6 | 46 | #include "netlink.h" |
feebdea2 | 47 | #include "odp-util.h" |
96fba48f | 48 | #include "ofpbuf.h" |
213a13ed | 49 | #include "openvswitch/datapath-compat.h" |
856081f6 | 50 | #include "packets.h" |
96fba48f | 51 | #include "poll-loop.h" |
17411ecf | 52 | #include "random.h" |
54825e09 | 53 | #include "shash.h" |
b3c01ed3 | 54 | #include "sset.h" |
14b4d2f9 | 55 | #include "timeval.h" |
d6569377 | 56 | #include "unaligned.h" |
96fba48f | 57 | #include "util.h" |
96fba48f | 58 | #include "vlog.h" |
5136ce49 | 59 | |
d98e6007 | 60 | VLOG_DEFINE_THIS_MODULE(dpif_linux); |
95b1d73a | 61 | enum { MAX_PORTS = USHRT_MAX }; |
773cd538 | 62 | |
24b019f8 JP |
63 | /* This ethtool flag was introduced in Linux 2.6.24, so it might be |
64 | * missing if we have old headers. */ | |
65 | #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */ | |
66 | ||
d6569377 | 67 | struct dpif_linux_dp { |
aaff4b55 BP |
68 | /* Generic Netlink header. */ |
69 | uint8_t cmd; | |
d6569377 | 70 | |
df2c07f4 | 71 | /* struct ovs_header. */ |
254f2dc8 | 72 | int dp_ifindex; |
d6569377 BP |
73 | |
74 | /* Attributes. */ | |
df2c07f4 | 75 | const char *name; /* OVS_DP_ATTR_NAME. */ |
a24a6574 | 76 | const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */ |
df2c07f4 | 77 | struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */ |
d6569377 BP |
78 | }; |
79 | ||
80 | static void dpif_linux_dp_init(struct dpif_linux_dp *); | |
aaff4b55 BP |
81 | static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *, |
82 | const struct ofpbuf *); | |
83 | static void dpif_linux_dp_dump_start(struct nl_dump *); | |
d6569377 BP |
84 | static int dpif_linux_dp_transact(const struct dpif_linux_dp *request, |
85 | struct dpif_linux_dp *reply, | |
86 | struct ofpbuf **bufp); | |
87 | static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply, | |
88 | struct ofpbuf **bufp); | |
89 | ||
90 | struct dpif_linux_flow { | |
37a1300c BP |
91 | /* Generic Netlink header. */ |
92 | uint8_t cmd; | |
d6569377 | 93 | |
df2c07f4 | 94 | /* struct ovs_header. */ |
d6569377 | 95 | unsigned int nlmsg_flags; |
254f2dc8 | 96 | int dp_ifindex; |
d6569377 BP |
97 | |
98 | /* Attributes. | |
99 | * | |
0e70cdcb BP |
100 | * The 'stats' member points to 64-bit data that might only be aligned on |
101 | * 32-bit boundaries, so get_unaligned_u64() should be used to access its | |
102 | * values. | |
d2a23af2 | 103 | * |
df2c07f4 | 104 | * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in |
d2a23af2 | 105 | * the Netlink version of the command, even if actions_len is zero. */ |
df2c07f4 | 106 | const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */ |
d6569377 | 107 | size_t key_len; |
e6cc0bab AZ |
108 | const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */ |
109 | size_t mask_len; | |
df2c07f4 | 110 | const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */ |
d6569377 | 111 | size_t actions_len; |
df2c07f4 JP |
112 | const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */ |
113 | const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */ | |
0e70cdcb | 114 | const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */ |
df2c07f4 | 115 | bool clear; /* OVS_FLOW_ATTR_CLEAR. */ |
d6569377 BP |
116 | }; |
117 | ||
118 | static void dpif_linux_flow_init(struct dpif_linux_flow *); | |
37a1300c BP |
119 | static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *, |
120 | const struct ofpbuf *); | |
121 | static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *, | |
122 | struct ofpbuf *); | |
30b44744 | 123 | static int dpif_linux_flow_transact(struct dpif_linux_flow *request, |
d6569377 BP |
124 | struct dpif_linux_flow *reply, |
125 | struct ofpbuf **bufp); | |
126 | static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *, | |
127 | struct dpif_flow_stats *); | |
128 | ||
989fd548 | 129 | /* One of the dpif channels between the kernel and userspace. */ |
fe3d61b3 | 130 | struct dpif_channel { |
14b4d2f9 | 131 | struct nl_sock *sock; /* Netlink socket. */ |
14b4d2f9 | 132 | long long int last_poll; /* Last time this channel was polled. */ |
fe3d61b3 BP |
133 | }; |
134 | ||
14b4d2f9 BP |
135 | static void report_loss(struct dpif *, struct dpif_channel *); |
136 | ||
96fba48f BP |
137 | /* Datapath interface for the openvswitch Linux kernel module. */ |
138 | struct dpif_linux { | |
139 | struct dpif dpif; | |
254f2dc8 | 140 | int dp_ifindex; |
e9e28be3 | 141 | |
b063d9f0 | 142 | /* Upcall messages. */ |
97be1538 | 143 | struct ovs_mutex upcall_lock; |
989fd548 JP |
144 | int uc_array_size; /* Size of 'channels' and 'epoll_events'. */ |
145 | struct dpif_channel *channels; | |
146 | struct epoll_event *epoll_events; | |
fe3d61b3 | 147 | int epoll_fd; /* epoll fd that includes channel socks. */ |
989fd548 JP |
148 | int n_events; /* Num events returned by epoll_wait(). */ |
149 | int event_offset; /* Offset into 'epoll_events'. */ | |
982b8810 | 150 | |
e9e28be3 | 151 | /* Change notification. */ |
e4516b20 | 152 | struct nl_sock *port_notifier; /* vport multicast group subscriber. */ |
96fba48f BP |
153 | }; |
154 | ||
155 | static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5); | |
156 | ||
e4516b20 BP |
157 | /* Generic Netlink family numbers for OVS. |
158 | * | |
159 | * Initialized by dpif_linux_init(). */ | |
df2c07f4 JP |
160 | static int ovs_datapath_family; |
161 | static int ovs_vport_family; | |
162 | static int ovs_flow_family; | |
163 | static int ovs_packet_family; | |
982b8810 | 164 | |
e4516b20 BP |
165 | /* Generic Netlink multicast groups for OVS. |
166 | * | |
167 | * Initialized by dpif_linux_init(). */ | |
168 | static unsigned int ovs_vport_mcgroup; | |
982b8810 BP |
169 | |
170 | static int dpif_linux_init(void); | |
e4516b20 | 171 | static int open_dpif(const struct dpif_linux_dp *, struct dpif **); |
4e022ec0 AW |
172 | static uint32_t dpif_linux_port_get_pid(const struct dpif *, |
173 | odp_port_t port_no); | |
96fba48f | 174 | |
f0fef760 BP |
175 | static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *, |
176 | struct ofpbuf *); | |
177 | static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *, | |
178 | const struct ofpbuf *); | |
179 | ||
96fba48f BP |
180 | static struct dpif_linux * |
181 | dpif_linux_cast(const struct dpif *dpif) | |
182 | { | |
183 | dpif_assert_class(dpif, &dpif_linux_class); | |
184 | return CONTAINER_OF(dpif, struct dpif_linux, dpif); | |
185 | } | |
186 | ||
d3d22744 | 187 | static int |
d0c23a1a | 188 | dpif_linux_enumerate(struct sset *all_dps) |
d3d22744 | 189 | { |
aaff4b55 BP |
190 | struct nl_dump dump; |
191 | struct ofpbuf msg; | |
aaff4b55 | 192 | int error; |
982b8810 | 193 | |
aaff4b55 BP |
194 | error = dpif_linux_init(); |
195 | if (error) { | |
196 | return error; | |
982b8810 | 197 | } |
d3d22744 | 198 | |
aaff4b55 BP |
199 | dpif_linux_dp_dump_start(&dump); |
200 | while (nl_dump_next(&dump, &msg)) { | |
201 | struct dpif_linux_dp dp; | |
d6569377 | 202 | |
aaff4b55 | 203 | if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) { |
d0c23a1a | 204 | sset_add(all_dps, dp.name); |
d3d22744 BP |
205 | } |
206 | } | |
aaff4b55 | 207 | return nl_dump_done(&dump); |
d3d22744 BP |
208 | } |
209 | ||
96fba48f | 210 | static int |
4a387741 BP |
211 | dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name, |
212 | bool create, struct dpif **dpifp) | |
96fba48f | 213 | { |
982b8810 | 214 | struct dpif_linux_dp dp_request, dp; |
c19e6535 | 215 | struct ofpbuf *buf; |
ea36840f | 216 | uint32_t upcall_pid; |
c19e6535 | 217 | int error; |
96fba48f | 218 | |
982b8810 BP |
219 | error = dpif_linux_init(); |
220 | if (error) { | |
221 | return error; | |
222 | } | |
223 | ||
982b8810 BP |
224 | /* Create or look up datapath. */ |
225 | dpif_linux_dp_init(&dp_request); | |
ea36840f BP |
226 | if (create) { |
227 | dp_request.cmd = OVS_DP_CMD_NEW; | |
228 | upcall_pid = 0; | |
229 | dp_request.upcall_pid = &upcall_pid; | |
230 | } else { | |
231 | dp_request.cmd = OVS_DP_CMD_GET; | |
232 | } | |
254f2dc8 | 233 | dp_request.name = name; |
982b8810 BP |
234 | error = dpif_linux_dp_transact(&dp_request, &dp, &buf); |
235 | if (error) { | |
236 | return error; | |
c19e6535 | 237 | } |
254f2dc8 | 238 | |
e4516b20 | 239 | error = open_dpif(&dp, dpifp); |
8f4a4df5 | 240 | ofpbuf_delete(buf); |
e4516b20 | 241 | return error; |
c19e6535 BP |
242 | } |
243 | ||
e4516b20 | 244 | static int |
254f2dc8 | 245 | open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp) |
c19e6535 | 246 | { |
c19e6535 | 247 | struct dpif_linux *dpif; |
c19e6535 | 248 | |
17411ecf | 249 | dpif = xzalloc(sizeof *dpif); |
e4516b20 | 250 | dpif->port_notifier = NULL; |
97be1538 | 251 | ovs_mutex_init(&dpif->upcall_lock, PTHREAD_MUTEX_DEFAULT); |
50f80534 | 252 | dpif->epoll_fd = -1; |
c19e6535 | 253 | |
254f2dc8 BP |
254 | dpif_init(&dpif->dpif, &dpif_linux_class, dp->name, |
255 | dp->dp_ifindex, dp->dp_ifindex); | |
c19e6535 | 256 | |
254f2dc8 | 257 | dpif->dp_ifindex = dp->dp_ifindex; |
c19e6535 | 258 | *dpifp = &dpif->dpif; |
e4516b20 BP |
259 | |
260 | return 0; | |
96fba48f BP |
261 | } |
262 | ||
17411ecf | 263 | static void |
fe3d61b3 | 264 | destroy_channels(struct dpif_linux *dpif) |
17411ecf | 265 | { |
4e022ec0 | 266 | unsigned int i; |
17411ecf | 267 | |
989fd548 JP |
268 | if (dpif->epoll_fd < 0) { |
269 | return; | |
50f80534 | 270 | } |
989fd548 JP |
271 | |
272 | for (i = 0; i < dpif->uc_array_size; i++ ) { | |
273 | struct dpif_linux_vport vport_request; | |
274 | struct dpif_channel *ch = &dpif->channels[i]; | |
275 | uint32_t upcall_pid = 0; | |
276 | ||
277 | if (!ch->sock) { | |
278 | continue; | |
279 | } | |
280 | ||
9fafa796 BP |
281 | epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL); |
282 | ||
989fd548 JP |
283 | /* Turn off upcalls. */ |
284 | dpif_linux_vport_init(&vport_request); | |
285 | vport_request.cmd = OVS_VPORT_CMD_SET; | |
286 | vport_request.dp_ifindex = dpif->dp_ifindex; | |
4e022ec0 | 287 | vport_request.port_no = u32_to_odp(i); |
989fd548 JP |
288 | vport_request.upcall_pid = &upcall_pid; |
289 | dpif_linux_vport_transact(&vport_request, NULL, NULL); | |
290 | ||
fe3d61b3 | 291 | nl_sock_destroy(ch->sock); |
17411ecf | 292 | } |
989fd548 JP |
293 | |
294 | free(dpif->channels); | |
295 | dpif->channels = NULL; | |
296 | dpif->uc_array_size = 0; | |
297 | ||
298 | free(dpif->epoll_events); | |
299 | dpif->epoll_events = NULL; | |
300 | dpif->n_events = dpif->event_offset = 0; | |
301 | ||
9fafa796 BP |
302 | /* Don't close dpif->epoll_fd since that would cause other threads that |
303 | * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */ | |
989fd548 JP |
304 | } |
305 | ||
306 | static int | |
4e022ec0 | 307 | add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock) |
989fd548 JP |
308 | { |
309 | struct epoll_event event; | |
4e022ec0 | 310 | uint32_t port_idx = odp_to_u32(port_no); |
989fd548 JP |
311 | |
312 | if (dpif->epoll_fd < 0) { | |
313 | return 0; | |
314 | } | |
315 | ||
316 | /* We assume that the datapath densely chooses port numbers, which | |
317 | * can therefore be used as an index into an array of channels. */ | |
4e022ec0 AW |
318 | if (port_idx >= dpif->uc_array_size) { |
319 | uint32_t new_size = port_idx + 1; | |
320 | uint32_t i; | |
989fd548 | 321 | |
12d76859 | 322 | if (new_size > MAX_PORTS) { |
989fd548 JP |
323 | VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big", |
324 | dpif_name(&dpif->dpif), port_no); | |
325 | return EFBIG; | |
326 | } | |
327 | ||
328 | dpif->channels = xrealloc(dpif->channels, | |
329 | new_size * sizeof *dpif->channels); | |
330 | for (i = dpif->uc_array_size; i < new_size; i++) { | |
331 | dpif->channels[i].sock = NULL; | |
332 | } | |
333 | ||
334 | dpif->epoll_events = xrealloc(dpif->epoll_events, | |
335 | new_size * sizeof *dpif->epoll_events); | |
336 | dpif->uc_array_size = new_size; | |
337 | } | |
338 | ||
339 | memset(&event, 0, sizeof event); | |
340 | event.events = EPOLLIN; | |
4e022ec0 | 341 | event.data.u32 = port_idx; |
989fd548 JP |
342 | if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock), |
343 | &event) < 0) { | |
344 | return errno; | |
345 | } | |
346 | ||
4e022ec0 AW |
347 | nl_sock_destroy(dpif->channels[port_idx].sock); |
348 | dpif->channels[port_idx].sock = sock; | |
349 | dpif->channels[port_idx].last_poll = LLONG_MIN; | |
989fd548 JP |
350 | |
351 | return 0; | |
352 | } | |
353 | ||
354 | static void | |
4e022ec0 | 355 | del_channel(struct dpif_linux *dpif, odp_port_t port_no) |
989fd548 JP |
356 | { |
357 | struct dpif_channel *ch; | |
4e022ec0 | 358 | uint32_t port_idx = odp_to_u32(port_no); |
989fd548 | 359 | |
4e022ec0 | 360 | if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) { |
989fd548 JP |
361 | return; |
362 | } | |
363 | ||
4e022ec0 | 364 | ch = &dpif->channels[port_idx]; |
989fd548 JP |
365 | if (!ch->sock) { |
366 | return; | |
367 | } | |
368 | ||
369 | epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL); | |
fa717215 | 370 | dpif->event_offset = dpif->n_events = 0; |
989fd548 JP |
371 | |
372 | nl_sock_destroy(ch->sock); | |
373 | ch->sock = NULL; | |
17411ecf JG |
374 | } |
375 | ||
96fba48f BP |
376 | static void |
377 | dpif_linux_close(struct dpif *dpif_) | |
378 | { | |
379 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); | |
c7178a0b | 380 | |
e4516b20 | 381 | nl_sock_destroy(dpif->port_notifier); |
fe3d61b3 | 382 | destroy_channels(dpif); |
9fafa796 BP |
383 | if (dpif->epoll_fd >= 0) { |
384 | close(dpif->epoll_fd); | |
385 | } | |
97be1538 | 386 | ovs_mutex_destroy(&dpif->upcall_lock); |
96fba48f BP |
387 | free(dpif); |
388 | } | |
389 | ||
390 | static int | |
7dab847a | 391 | dpif_linux_destroy(struct dpif *dpif_) |
96fba48f | 392 | { |
d6569377 BP |
393 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
394 | struct dpif_linux_dp dp; | |
395 | ||
396 | dpif_linux_dp_init(&dp); | |
df2c07f4 | 397 | dp.cmd = OVS_DP_CMD_DEL; |
254f2dc8 | 398 | dp.dp_ifindex = dpif->dp_ifindex; |
d6569377 | 399 | return dpif_linux_dp_transact(&dp, NULL, NULL); |
96fba48f BP |
400 | } |
401 | ||
402 | static int | |
a8d9304d | 403 | dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats) |
96fba48f | 404 | { |
d6569377 BP |
405 | struct dpif_linux_dp dp; |
406 | struct ofpbuf *buf; | |
407 | int error; | |
408 | ||
409 | error = dpif_linux_dp_get(dpif_, &dp, &buf); | |
410 | if (!error) { | |
a8d9304d BP |
411 | stats->n_hit = dp.stats.n_hit; |
412 | stats->n_missed = dp.stats.n_missed; | |
413 | stats->n_lost = dp.stats.n_lost; | |
414 | stats->n_flows = dp.stats.n_flows; | |
d6569377 BP |
415 | ofpbuf_delete(buf); |
416 | } | |
417 | return error; | |
96fba48f BP |
418 | } |
419 | ||
b9ad7294 EJ |
420 | static const char * |
421 | get_vport_type(const struct dpif_linux_vport *vport) | |
422 | { | |
423 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
424 | ||
425 | switch (vport->type) { | |
426 | case OVS_VPORT_TYPE_NETDEV: | |
427 | return "system"; | |
428 | ||
429 | case OVS_VPORT_TYPE_INTERNAL: | |
430 | return "internal"; | |
431 | ||
432 | case OVS_VPORT_TYPE_GRE: | |
433 | return "gre"; | |
434 | ||
435 | case OVS_VPORT_TYPE_GRE64: | |
436 | return "gre64"; | |
437 | ||
b9ad7294 EJ |
438 | case OVS_VPORT_TYPE_VXLAN: |
439 | return "vxlan"; | |
440 | ||
a6ae068b LJ |
441 | case OVS_VPORT_TYPE_LISP: |
442 | return "lisp"; | |
443 | ||
b9ad7294 EJ |
444 | case OVS_VPORT_TYPE_UNSPEC: |
445 | case __OVS_VPORT_TYPE_MAX: | |
446 | break; | |
447 | } | |
448 | ||
449 | VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u", | |
450 | vport->dp_ifindex, vport->name, (unsigned int) vport->type); | |
451 | return "unknown"; | |
452 | } | |
453 | ||
c060c4cf EJ |
454 | static enum ovs_vport_type |
455 | netdev_to_ovs_vport_type(const struct netdev *netdev) | |
456 | { | |
457 | const char *type = netdev_get_type(netdev); | |
458 | ||
459 | if (!strcmp(type, "tap") || !strcmp(type, "system")) { | |
460 | return OVS_VPORT_TYPE_NETDEV; | |
461 | } else if (!strcmp(type, "internal")) { | |
462 | return OVS_VPORT_TYPE_INTERNAL; | |
463 | } else if (strstr(type, "gre64")) { | |
464 | return OVS_VPORT_TYPE_GRE64; | |
465 | } else if (strstr(type, "gre")) { | |
466 | return OVS_VPORT_TYPE_GRE; | |
c060c4cf EJ |
467 | } else if (!strcmp(type, "vxlan")) { |
468 | return OVS_VPORT_TYPE_VXLAN; | |
a6ae068b LJ |
469 | } else if (!strcmp(type, "lisp")) { |
470 | return OVS_VPORT_TYPE_LISP; | |
c060c4cf EJ |
471 | } else { |
472 | return OVS_VPORT_TYPE_UNSPEC; | |
473 | } | |
474 | } | |
475 | ||
96fba48f | 476 | static int |
9fafa796 BP |
477 | dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev, |
478 | odp_port_t *port_nop) | |
96fba48f | 479 | { |
c19e6535 | 480 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
26508d9a | 481 | const struct netdev_tunnel_config *tnl_cfg; |
3aa30359 BP |
482 | char namebuf[NETDEV_VPORT_NAME_BUFSIZE]; |
483 | const char *name = netdev_vport_get_dpif_port(netdev, | |
484 | namebuf, sizeof namebuf); | |
c3827f61 | 485 | const char *type = netdev_get_type(netdev); |
c19e6535 | 486 | struct dpif_linux_vport request, reply; |
989fd548 | 487 | struct nl_sock *sock = NULL; |
78a2d59c | 488 | uint32_t upcall_pid; |
c19e6535 | 489 | struct ofpbuf *buf; |
26508d9a KM |
490 | uint64_t options_stub[64 / 8]; |
491 | struct ofpbuf options; | |
78a2d59c | 492 | int error; |
96fba48f | 493 | |
989fd548 JP |
494 | if (dpif->epoll_fd >= 0) { |
495 | error = nl_sock_create(NETLINK_GENERIC, &sock); | |
496 | if (error) { | |
497 | return error; | |
498 | } | |
499 | } | |
500 | ||
c19e6535 | 501 | dpif_linux_vport_init(&request); |
df2c07f4 | 502 | request.cmd = OVS_VPORT_CMD_NEW; |
254f2dc8 | 503 | request.dp_ifindex = dpif->dp_ifindex; |
c060c4cf | 504 | request.type = netdev_to_ovs_vport_type(netdev); |
df2c07f4 | 505 | if (request.type == OVS_VPORT_TYPE_UNSPEC) { |
c283069c BP |
506 | VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has " |
507 | "unsupported type `%s'", | |
c19e6535 | 508 | dpif_name(dpif_), name, type); |
989fd548 | 509 | nl_sock_destroy(sock); |
c283069c BP |
510 | return EINVAL; |
511 | } | |
c19e6535 | 512 | request.name = name; |
c3827f61 | 513 | |
24b019f8 JP |
514 | if (request.type == OVS_VPORT_TYPE_NETDEV) { |
515 | netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false); | |
516 | } | |
517 | ||
26508d9a KM |
518 | tnl_cfg = netdev_get_tunnel_config(netdev); |
519 | if (tnl_cfg && tnl_cfg->dst_port != 0) { | |
520 | ofpbuf_use_stack(&options, options_stub, sizeof options_stub); | |
521 | nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT, | |
7e2d8aea | 522 | ntohs(tnl_cfg->dst_port)); |
26508d9a KM |
523 | request.options = options.data; |
524 | request.options_len = options.size; | |
525 | } | |
526 | ||
78a2d59c | 527 | request.port_no = *port_nop; |
989fd548 | 528 | upcall_pid = sock ? nl_sock_pid(sock) : 0; |
78a2d59c | 529 | request.upcall_pid = &upcall_pid; |
95b1d73a | 530 | |
78a2d59c | 531 | error = dpif_linux_vport_transact(&request, &reply, &buf); |
78a2d59c JP |
532 | if (!error) { |
533 | *port_nop = reply.port_no; | |
534 | VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32, | |
f205882a | 535 | dpif_name(dpif_), reply.port_no, upcall_pid); |
2510ba7c | 536 | } else { |
4e022ec0 | 537 | if (error == EBUSY && *port_nop != ODPP_NONE) { |
2510ba7c JP |
538 | VLOG_INFO("%s: requested port %"PRIu32" is in use", |
539 | dpif_name(dpif_), *port_nop); | |
540 | } | |
989fd548 JP |
541 | nl_sock_destroy(sock); |
542 | ofpbuf_delete(buf); | |
543 | return error; | |
78a2d59c | 544 | } |
78a2d59c | 545 | ofpbuf_delete(buf); |
c3827f61 | 546 | |
989fd548 JP |
547 | if (sock) { |
548 | error = add_channel(dpif, *port_nop, sock); | |
549 | if (error) { | |
550 | VLOG_INFO("%s: could not add channel for port %s", | |
551 | dpif_name(dpif_), name); | |
552 | ||
553 | /* Delete the port. */ | |
554 | dpif_linux_vport_init(&request); | |
555 | request.cmd = OVS_VPORT_CMD_DEL; | |
556 | request.dp_ifindex = dpif->dp_ifindex; | |
557 | request.port_no = *port_nop; | |
558 | dpif_linux_vport_transact(&request, NULL, NULL); | |
559 | ||
560 | nl_sock_destroy(sock); | |
561 | return error; | |
562 | } | |
563 | } | |
564 | ||
565 | return 0; | |
96fba48f BP |
566 | } |
567 | ||
568 | static int | |
9fafa796 BP |
569 | dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev, |
570 | odp_port_t *port_nop) | |
571 | { | |
572 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); | |
573 | int error; | |
574 | ||
97be1538 | 575 | ovs_mutex_lock(&dpif->upcall_lock); |
9fafa796 | 576 | error = dpif_linux_port_add__(dpif_, netdev, port_nop); |
97be1538 | 577 | ovs_mutex_unlock(&dpif->upcall_lock); |
9fafa796 BP |
578 | |
579 | return error; | |
580 | } | |
581 | ||
582 | static int | |
583 | dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no) | |
96fba48f | 584 | { |
c19e6535 BP |
585 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
586 | struct dpif_linux_vport vport; | |
773cd538 | 587 | int error; |
c19e6535 BP |
588 | |
589 | dpif_linux_vport_init(&vport); | |
df2c07f4 | 590 | vport.cmd = OVS_VPORT_CMD_DEL; |
254f2dc8 | 591 | vport.dp_ifindex = dpif->dp_ifindex; |
c19e6535 | 592 | vport.port_no = port_no; |
773cd538 EJ |
593 | error = dpif_linux_vport_transact(&vport, NULL, NULL); |
594 | ||
989fd548 JP |
595 | del_channel(dpif, port_no); |
596 | ||
773cd538 | 597 | return error; |
c3827f61 | 598 | } |
3abc4a1a | 599 | |
9fafa796 BP |
600 | static int |
601 | dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no) | |
602 | { | |
603 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); | |
604 | int error; | |
605 | ||
97be1538 | 606 | ovs_mutex_lock(&dpif->upcall_lock); |
9fafa796 | 607 | error = dpif_linux_port_del__(dpif_, port_no); |
97be1538 | 608 | ovs_mutex_unlock(&dpif->upcall_lock); |
9fafa796 BP |
609 | |
610 | return error; | |
611 | } | |
612 | ||
c3827f61 | 613 | static int |
4e022ec0 | 614 | dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no, |
4c738a8d | 615 | const char *port_name, struct dpif_port *dpif_port) |
c3827f61 | 616 | { |
c19e6535 BP |
617 | struct dpif_linux_vport request; |
618 | struct dpif_linux_vport reply; | |
619 | struct ofpbuf *buf; | |
4c738a8d BP |
620 | int error; |
621 | ||
c19e6535 | 622 | dpif_linux_vport_init(&request); |
df2c07f4 | 623 | request.cmd = OVS_VPORT_CMD_GET; |
254f2dc8 | 624 | request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex; |
c19e6535 BP |
625 | request.port_no = port_no; |
626 | request.name = port_name; | |
4c738a8d | 627 | |
c19e6535 BP |
628 | error = dpif_linux_vport_transact(&request, &reply, &buf); |
629 | if (!error) { | |
33db1592 BP |
630 | if (reply.dp_ifindex != request.dp_ifindex) { |
631 | /* A query by name reported that 'port_name' is in some datapath | |
632 | * other than 'dpif', but the caller wants to know about 'dpif'. */ | |
633 | error = ENODEV; | |
4afba28d | 634 | } else if (dpif_port) { |
33db1592 | 635 | dpif_port->name = xstrdup(reply.name); |
b9ad7294 | 636 | dpif_port->type = xstrdup(get_vport_type(&reply)); |
33db1592 BP |
637 | dpif_port->port_no = reply.port_no; |
638 | } | |
c19e6535 | 639 | ofpbuf_delete(buf); |
3abc4a1a | 640 | } |
c19e6535 | 641 | return error; |
96fba48f BP |
642 | } |
643 | ||
644 | static int | |
4e022ec0 | 645 | dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no, |
4c738a8d | 646 | struct dpif_port *dpif_port) |
96fba48f | 647 | { |
c19e6535 | 648 | return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port); |
96fba48f BP |
649 | } |
650 | ||
651 | static int | |
4c738a8d BP |
652 | dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname, |
653 | struct dpif_port *dpif_port) | |
96fba48f | 654 | { |
4c738a8d | 655 | return dpif_linux_port_query__(dpif, 0, devname, dpif_port); |
96fba48f BP |
656 | } |
657 | ||
4e022ec0 | 658 | static odp_port_t |
996c1b3d BP |
659 | dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED) |
660 | { | |
4e022ec0 | 661 | return u32_to_odp(MAX_PORTS); |
996c1b3d BP |
662 | } |
663 | ||
98403001 | 664 | static uint32_t |
4e022ec0 | 665 | dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no) |
98403001 | 666 | { |
9fafa796 | 667 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
4e022ec0 | 668 | uint32_t port_idx = odp_to_u32(port_no); |
9fafa796 | 669 | uint32_t pid = 0; |
98403001 | 670 | |
97be1538 | 671 | ovs_mutex_lock(&dpif->upcall_lock); |
9fafa796 | 672 | if (dpif->epoll_fd >= 0) { |
4e022ec0 | 673 | /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s |
989fd548 | 674 | * channel, since it is not heavily loaded. */ |
4e022ec0 | 675 | uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx; |
9fafa796 | 676 | pid = nl_sock_pid(dpif->channels[idx].sock); |
98403001 | 677 | } |
97be1538 | 678 | ovs_mutex_unlock(&dpif->upcall_lock); |
9fafa796 BP |
679 | |
680 | return pid; | |
98403001 BP |
681 | } |
682 | ||
96fba48f BP |
683 | static int |
684 | dpif_linux_flow_flush(struct dpif *dpif_) | |
685 | { | |
550f0db4 | 686 | const struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
37a1300c BP |
687 | struct dpif_linux_flow flow; |
688 | ||
689 | dpif_linux_flow_init(&flow); | |
df2c07f4 | 690 | flow.cmd = OVS_FLOW_CMD_DEL; |
254f2dc8 | 691 | flow.dp_ifindex = dpif->dp_ifindex; |
37a1300c | 692 | return dpif_linux_flow_transact(&flow, NULL, NULL); |
96fba48f BP |
693 | } |
694 | ||
c19e6535 | 695 | struct dpif_linux_port_state { |
f0fef760 | 696 | struct nl_dump dump; |
c19e6535 BP |
697 | }; |
698 | ||
96fba48f | 699 | static int |
f0fef760 | 700 | dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep) |
96fba48f | 701 | { |
550f0db4 | 702 | const struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
f0fef760 BP |
703 | struct dpif_linux_port_state *state; |
704 | struct dpif_linux_vport request; | |
705 | struct ofpbuf *buf; | |
706 | ||
707 | *statep = state = xmalloc(sizeof *state); | |
708 | ||
709 | dpif_linux_vport_init(&request); | |
df2c07f4 | 710 | request.cmd = OVS_DP_CMD_GET; |
254f2dc8 | 711 | request.dp_ifindex = dpif->dp_ifindex; |
f0fef760 BP |
712 | |
713 | buf = ofpbuf_new(1024); | |
714 | dpif_linux_vport_to_ofpbuf(&request, buf); | |
a88b4e04 | 715 | nl_dump_start(&state->dump, NETLINK_GENERIC, buf); |
f0fef760 BP |
716 | ofpbuf_delete(buf); |
717 | ||
b0ec0f27 BP |
718 | return 0; |
719 | } | |
720 | ||
721 | static int | |
f0fef760 | 722 | dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_, |
4c738a8d | 723 | struct dpif_port *dpif_port) |
b0ec0f27 | 724 | { |
c19e6535 | 725 | struct dpif_linux_port_state *state = state_; |
f0fef760 BP |
726 | struct dpif_linux_vport vport; |
727 | struct ofpbuf buf; | |
96fba48f BP |
728 | int error; |
729 | ||
f0fef760 BP |
730 | if (!nl_dump_next(&state->dump, &buf)) { |
731 | return EOF; | |
732 | } | |
c19e6535 | 733 | |
f0fef760 | 734 | error = dpif_linux_vport_from_ofpbuf(&vport, &buf); |
c3827f61 | 735 | if (error) { |
f0fef760 | 736 | return error; |
c3827f61 | 737 | } |
f0fef760 | 738 | |
ebc56baa | 739 | dpif_port->name = CONST_CAST(char *, vport.name); |
b9ad7294 | 740 | dpif_port->type = CONST_CAST(char *, get_vport_type(&vport)); |
f0fef760 BP |
741 | dpif_port->port_no = vport.port_no; |
742 | return 0; | |
b0ec0f27 BP |
743 | } |
744 | ||
745 | static int | |
95b1d73a | 746 | dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_) |
b0ec0f27 | 747 | { |
c19e6535 | 748 | struct dpif_linux_port_state *state = state_; |
f0fef760 | 749 | int error = nl_dump_done(&state->dump); |
8522b383 | 750 | |
b0ec0f27 | 751 | free(state); |
f0fef760 | 752 | return error; |
96fba48f BP |
753 | } |
754 | ||
e9e28be3 BP |
755 | static int |
756 | dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep) | |
757 | { | |
758 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); | |
e9e28be3 | 759 | |
e4516b20 BP |
760 | /* Lazily create the Netlink socket to listen for notifications. */ |
761 | if (!dpif->port_notifier) { | |
762 | struct nl_sock *sock; | |
763 | int error; | |
764 | ||
765 | error = nl_sock_create(NETLINK_GENERIC, &sock); | |
766 | if (error) { | |
767 | return error; | |
768 | } | |
769 | ||
770 | error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup); | |
771 | if (error) { | |
772 | nl_sock_destroy(sock); | |
773 | return error; | |
774 | } | |
775 | dpif->port_notifier = sock; | |
776 | ||
777 | /* We have no idea of the current state so report that everything | |
778 | * changed. */ | |
779 | return ENOBUFS; | |
780 | } | |
781 | ||
782 | for (;;) { | |
783 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
784 | uint64_t buf_stub[4096 / 8]; | |
785 | struct ofpbuf buf; | |
786 | int error; | |
787 | ||
788 | ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub); | |
789 | error = nl_sock_recv(dpif->port_notifier, &buf, false); | |
790 | if (!error) { | |
791 | struct dpif_linux_vport vport; | |
792 | ||
793 | error = dpif_linux_vport_from_ofpbuf(&vport, &buf); | |
794 | if (!error) { | |
795 | if (vport.dp_ifindex == dpif->dp_ifindex | |
796 | && (vport.cmd == OVS_VPORT_CMD_NEW | |
797 | || vport.cmd == OVS_VPORT_CMD_DEL | |
798 | || vport.cmd == OVS_VPORT_CMD_SET)) { | |
799 | VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8, | |
800 | dpif->dpif.full_name, vport.name, vport.cmd); | |
801 | *devnamep = xstrdup(vport.name); | |
802 | return 0; | |
803 | } else { | |
804 | continue; | |
805 | } | |
806 | } | |
807 | } else if (error == EAGAIN) { | |
808 | return EAGAIN; | |
809 | } | |
810 | ||
811 | VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)", | |
812 | ovs_strerror(error)); | |
813 | nl_sock_drain(dpif->port_notifier); | |
8b61709d | 814 | return ENOBUFS; |
e9e28be3 | 815 | } |
e9e28be3 BP |
816 | } |
817 | ||
818 | static void | |
819 | dpif_linux_port_poll_wait(const struct dpif *dpif_) | |
820 | { | |
550f0db4 | 821 | const struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
e4516b20 BP |
822 | |
823 | if (dpif->port_notifier) { | |
824 | nl_sock_wait(dpif->port_notifier, POLLIN); | |
825 | } else { | |
e9e28be3 | 826 | poll_immediate_wake(); |
e9e28be3 BP |
827 | } |
828 | } | |
829 | ||
96fba48f | 830 | static int |
30053024 BP |
831 | dpif_linux_flow_get__(const struct dpif *dpif_, |
832 | const struct nlattr *key, size_t key_len, | |
833 | struct dpif_linux_flow *reply, struct ofpbuf **bufp) | |
96fba48f | 834 | { |
550f0db4 | 835 | const struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
30053024 | 836 | struct dpif_linux_flow request; |
feebdea2 | 837 | |
d6569377 | 838 | dpif_linux_flow_init(&request); |
df2c07f4 | 839 | request.cmd = OVS_FLOW_CMD_GET; |
254f2dc8 | 840 | request.dp_ifindex = dpif->dp_ifindex; |
d6569377 BP |
841 | request.key = key; |
842 | request.key_len = key_len; | |
30053024 BP |
843 | return dpif_linux_flow_transact(&request, reply, bufp); |
844 | } | |
845 | ||
846 | static int | |
847 | dpif_linux_flow_get(const struct dpif *dpif_, | |
848 | const struct nlattr *key, size_t key_len, | |
849 | struct ofpbuf **actionsp, struct dpif_flow_stats *stats) | |
850 | { | |
851 | struct dpif_linux_flow reply; | |
852 | struct ofpbuf *buf; | |
853 | int error; | |
854 | ||
855 | error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf); | |
feebdea2 BP |
856 | if (!error) { |
857 | if (stats) { | |
d6569377 | 858 | dpif_linux_flow_get_stats(&reply, stats); |
feebdea2 | 859 | } |
d6569377 | 860 | if (actionsp) { |
ebc56baa | 861 | buf->data = CONST_CAST(struct nlattr *, reply.actions); |
d6569377 BP |
862 | buf->size = reply.actions_len; |
863 | *actionsp = buf; | |
864 | } else { | |
865 | ofpbuf_delete(buf); | |
feebdea2 BP |
866 | } |
867 | } | |
868 | return error; | |
96fba48f BP |
869 | } |
870 | ||
6bc60024 | 871 | static void |
89625d1e | 872 | dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put, |
6bc60024 BP |
873 | struct dpif_linux_flow *request) |
874 | { | |
d64e176c | 875 | static const struct nlattr dummy_action; |
6bc60024 | 876 | |
550f0db4 | 877 | const struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
6bc60024 BP |
878 | |
879 | dpif_linux_flow_init(request); | |
89625d1e | 880 | request->cmd = (put->flags & DPIF_FP_CREATE |
6bc60024 BP |
881 | ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET); |
882 | request->dp_ifindex = dpif->dp_ifindex; | |
89625d1e BP |
883 | request->key = put->key; |
884 | request->key_len = put->key_len; | |
e6cc0bab AZ |
885 | request->mask = put->mask; |
886 | request->mask_len = put->mask_len; | |
6bc60024 | 887 | /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */ |
d64e176c BP |
888 | request->actions = (put->actions |
889 | ? put->actions | |
890 | : CONST_CAST(struct nlattr *, &dummy_action)); | |
89625d1e BP |
891 | request->actions_len = put->actions_len; |
892 | if (put->flags & DPIF_FP_ZERO_STATS) { | |
6bc60024 BP |
893 | request->clear = true; |
894 | } | |
89625d1e | 895 | request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE; |
6bc60024 BP |
896 | } |
897 | ||
96fba48f | 898 | static int |
89625d1e | 899 | dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put) |
96fba48f | 900 | { |
d6569377 BP |
901 | struct dpif_linux_flow request, reply; |
902 | struct ofpbuf *buf; | |
feebdea2 BP |
903 | int error; |
904 | ||
89625d1e | 905 | dpif_linux_init_flow_put(dpif_, put, &request); |
d6569377 | 906 | error = dpif_linux_flow_transact(&request, |
89625d1e BP |
907 | put->stats ? &reply : NULL, |
908 | put->stats ? &buf : NULL); | |
909 | if (!error && put->stats) { | |
910 | dpif_linux_flow_get_stats(&reply, put->stats); | |
d6569377 | 911 | ofpbuf_delete(buf); |
feebdea2 BP |
912 | } |
913 | return error; | |
96fba48f BP |
914 | } |
915 | ||
b99d3cee BP |
916 | static void |
917 | dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del, | |
918 | struct dpif_linux_flow *request) | |
96fba48f | 919 | { |
550f0db4 | 920 | const struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
b99d3cee BP |
921 | |
922 | dpif_linux_flow_init(request); | |
923 | request->cmd = OVS_FLOW_CMD_DEL; | |
924 | request->dp_ifindex = dpif->dp_ifindex; | |
925 | request->key = del->key; | |
926 | request->key_len = del->key_len; | |
927 | } | |
928 | ||
929 | static int | |
930 | dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del) | |
931 | { | |
d6569377 BP |
932 | struct dpif_linux_flow request, reply; |
933 | struct ofpbuf *buf; | |
feebdea2 BP |
934 | int error; |
935 | ||
b99d3cee | 936 | dpif_linux_init_flow_del(dpif_, del, &request); |
d6569377 | 937 | error = dpif_linux_flow_transact(&request, |
b99d3cee BP |
938 | del->stats ? &reply : NULL, |
939 | del->stats ? &buf : NULL); | |
940 | if (!error && del->stats) { | |
941 | dpif_linux_flow_get_stats(&reply, del->stats); | |
d6569377 | 942 | ofpbuf_delete(buf); |
feebdea2 BP |
943 | } |
944 | return error; | |
96fba48f BP |
945 | } |
946 | ||
feebdea2 | 947 | struct dpif_linux_flow_state { |
37a1300c | 948 | struct nl_dump dump; |
d6569377 | 949 | struct dpif_linux_flow flow; |
c97fb132 | 950 | struct dpif_flow_stats stats; |
30053024 | 951 | struct ofpbuf *buf; |
feebdea2 BP |
952 | }; |
953 | ||
96fba48f | 954 | static int |
37a1300c | 955 | dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep) |
96fba48f | 956 | { |
550f0db4 | 957 | const struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
37a1300c BP |
958 | struct dpif_linux_flow_state *state; |
959 | struct dpif_linux_flow request; | |
960 | struct ofpbuf *buf; | |
961 | ||
962 | *statep = state = xmalloc(sizeof *state); | |
963 | ||
964 | dpif_linux_flow_init(&request); | |
df2c07f4 | 965 | request.cmd = OVS_DP_CMD_GET; |
254f2dc8 | 966 | request.dp_ifindex = dpif->dp_ifindex; |
37a1300c BP |
967 | |
968 | buf = ofpbuf_new(1024); | |
969 | dpif_linux_flow_to_ofpbuf(&request, buf); | |
a88b4e04 | 970 | nl_dump_start(&state->dump, NETLINK_GENERIC, buf); |
37a1300c BP |
971 | ofpbuf_delete(buf); |
972 | ||
30053024 BP |
973 | state->buf = NULL; |
974 | ||
704a1e09 BP |
975 | return 0; |
976 | } | |
977 | ||
978 | static int | |
37a1300c | 979 | dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_, |
feebdea2 | 980 | const struct nlattr **key, size_t *key_len, |
e6cc0bab | 981 | const struct nlattr **mask, size_t *mask_len, |
feebdea2 | 982 | const struct nlattr **actions, size_t *actions_len, |
c97fb132 | 983 | const struct dpif_flow_stats **stats) |
704a1e09 | 984 | { |
feebdea2 | 985 | struct dpif_linux_flow_state *state = state_; |
37a1300c | 986 | struct ofpbuf buf; |
96fba48f BP |
987 | int error; |
988 | ||
30053024 BP |
989 | do { |
990 | ofpbuf_delete(state->buf); | |
991 | state->buf = NULL; | |
feebdea2 | 992 | |
30053024 BP |
993 | if (!nl_dump_next(&state->dump, &buf)) { |
994 | return EOF; | |
feebdea2 | 995 | } |
30053024 BP |
996 | |
997 | error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf); | |
998 | if (error) { | |
999 | return error; | |
feebdea2 | 1000 | } |
30053024 BP |
1001 | |
1002 | if (actions && !state->flow.actions) { | |
1003 | error = dpif_linux_flow_get__(dpif_, state->flow.key, | |
1004 | state->flow.key_len, | |
1005 | &state->flow, &state->buf); | |
1006 | if (error == ENOENT) { | |
1007 | VLOG_DBG("dumped flow disappeared on get"); | |
1008 | } else if (error) { | |
10a89ef0 BP |
1009 | VLOG_WARN("error fetching dumped flow: %s", |
1010 | ovs_strerror(error)); | |
30053024 | 1011 | } |
feebdea2 | 1012 | } |
30053024 BP |
1013 | } while (error); |
1014 | ||
1015 | if (actions) { | |
1016 | *actions = state->flow.actions; | |
1017 | *actions_len = state->flow.actions_len; | |
1018 | } | |
1019 | if (key) { | |
1020 | *key = state->flow.key; | |
1021 | *key_len = state->flow.key_len; | |
1022 | } | |
e6cc0bab AZ |
1023 | if (mask) { |
1024 | *mask = state->flow.mask; | |
1025 | *mask_len = state->flow.mask ? state->flow.mask_len : 0; | |
1026 | } | |
30053024 BP |
1027 | if (stats) { |
1028 | dpif_linux_flow_get_stats(&state->flow, &state->stats); | |
1029 | *stats = &state->stats; | |
feebdea2 | 1030 | } |
37a1300c | 1031 | return error; |
704a1e09 BP |
1032 | } |
1033 | ||
1034 | static int | |
d6569377 | 1035 | dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_) |
704a1e09 | 1036 | { |
d6569377 | 1037 | struct dpif_linux_flow_state *state = state_; |
37a1300c | 1038 | int error = nl_dump_done(&state->dump); |
30053024 | 1039 | ofpbuf_delete(state->buf); |
704a1e09 | 1040 | free(state); |
37a1300c | 1041 | return error; |
96fba48f BP |
1042 | } |
1043 | ||
eabe7c68 BP |
1044 | static void |
1045 | dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec, | |
1046 | struct ofpbuf *buf) | |
96fba48f | 1047 | { |
89625d1e | 1048 | struct ovs_header *k_exec; |
f7cd0081 | 1049 | |
eabe7c68 BP |
1050 | ofpbuf_prealloc_tailroom(buf, (64 |
1051 | + d_exec->packet->size | |
1052 | + d_exec->key_len | |
1053 | + d_exec->actions_len)); | |
f7cd0081 | 1054 | |
df2c07f4 | 1055 | nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST, |
69685a88 | 1056 | OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION); |
f7cd0081 | 1057 | |
89625d1e BP |
1058 | k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec); |
1059 | k_exec->dp_ifindex = dp_ifindex; | |
f7cd0081 | 1060 | |
89625d1e BP |
1061 | nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, |
1062 | d_exec->packet->data, d_exec->packet->size); | |
1063 | nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len); | |
1064 | nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, | |
1065 | d_exec->actions, d_exec->actions_len); | |
6bc60024 BP |
1066 | } |
1067 | ||
1068 | static int | |
89625d1e | 1069 | dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute) |
6bc60024 | 1070 | { |
eabe7c68 BP |
1071 | uint64_t request_stub[1024 / 8]; |
1072 | struct ofpbuf request; | |
6bc60024 BP |
1073 | int error; |
1074 | ||
eabe7c68 BP |
1075 | ofpbuf_use_stub(&request, request_stub, sizeof request_stub); |
1076 | dpif_linux_encode_execute(dp_ifindex, execute, &request); | |
a88b4e04 | 1077 | error = nl_transact(NETLINK_GENERIC, &request, NULL); |
eabe7c68 | 1078 | ofpbuf_uninit(&request); |
6bc60024 | 1079 | |
f7cd0081 | 1080 | return error; |
96fba48f BP |
1081 | } |
1082 | ||
eb8b28e7 | 1083 | static int |
89625d1e | 1084 | dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute) |
eb8b28e7 | 1085 | { |
550f0db4 | 1086 | const struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
eb8b28e7 | 1087 | |
89625d1e | 1088 | return dpif_linux_execute__(dpif->dp_ifindex, execute); |
eb8b28e7 EJ |
1089 | } |
1090 | ||
eabe7c68 BP |
1091 | #define MAX_OPS 50 |
1092 | ||
6bc60024 | 1093 | static void |
eabe7c68 | 1094 | dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops) |
6bc60024 | 1095 | { |
550f0db4 | 1096 | const struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
eabe7c68 BP |
1097 | |
1098 | struct op_auxdata { | |
1099 | struct nl_transaction txn; | |
72d32ac0 | 1100 | |
eabe7c68 BP |
1101 | struct ofpbuf request; |
1102 | uint64_t request_stub[1024 / 8]; | |
72d32ac0 BP |
1103 | |
1104 | struct ofpbuf reply; | |
1105 | uint64_t reply_stub[1024 / 8]; | |
eabe7c68 BP |
1106 | } auxes[MAX_OPS]; |
1107 | ||
1108 | struct nl_transaction *txnsp[MAX_OPS]; | |
6bc60024 BP |
1109 | size_t i; |
1110 | ||
cb22974d | 1111 | ovs_assert(n_ops <= MAX_OPS); |
6bc60024 | 1112 | for (i = 0; i < n_ops; i++) { |
eabe7c68 | 1113 | struct op_auxdata *aux = &auxes[i]; |
c2b565b5 | 1114 | struct dpif_op *op = ops[i]; |
b99d3cee BP |
1115 | struct dpif_flow_put *put; |
1116 | struct dpif_flow_del *del; | |
1117 | struct dpif_execute *execute; | |
eabe7c68 BP |
1118 | struct dpif_linux_flow flow; |
1119 | ||
1120 | ofpbuf_use_stub(&aux->request, | |
1121 | aux->request_stub, sizeof aux->request_stub); | |
1122 | aux->txn.request = &aux->request; | |
b99d3cee | 1123 | |
72d32ac0 BP |
1124 | ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub); |
1125 | aux->txn.reply = NULL; | |
1126 | ||
b99d3cee BP |
1127 | switch (op->type) { |
1128 | case DPIF_OP_FLOW_PUT: | |
1129 | put = &op->u.flow_put; | |
eabe7c68 | 1130 | dpif_linux_init_flow_put(dpif_, put, &flow); |
6bc60024 | 1131 | if (put->stats) { |
eabe7c68 | 1132 | flow.nlmsg_flags |= NLM_F_ECHO; |
72d32ac0 | 1133 | aux->txn.reply = &aux->reply; |
6bc60024 | 1134 | } |
eabe7c68 | 1135 | dpif_linux_flow_to_ofpbuf(&flow, &aux->request); |
b99d3cee BP |
1136 | break; |
1137 | ||
1138 | case DPIF_OP_FLOW_DEL: | |
1139 | del = &op->u.flow_del; | |
eabe7c68 | 1140 | dpif_linux_init_flow_del(dpif_, del, &flow); |
b99d3cee | 1141 | if (del->stats) { |
eabe7c68 | 1142 | flow.nlmsg_flags |= NLM_F_ECHO; |
72d32ac0 | 1143 | aux->txn.reply = &aux->reply; |
b99d3cee | 1144 | } |
eabe7c68 | 1145 | dpif_linux_flow_to_ofpbuf(&flow, &aux->request); |
b99d3cee | 1146 | break; |
6bc60024 | 1147 | |
b99d3cee BP |
1148 | case DPIF_OP_EXECUTE: |
1149 | execute = &op->u.execute; | |
eabe7c68 BP |
1150 | dpif_linux_encode_execute(dpif->dp_ifindex, execute, |
1151 | &aux->request); | |
b99d3cee BP |
1152 | break; |
1153 | ||
1154 | default: | |
6bc60024 BP |
1155 | NOT_REACHED(); |
1156 | } | |
1157 | } | |
1158 | ||
6bc60024 | 1159 | for (i = 0; i < n_ops; i++) { |
eabe7c68 | 1160 | txnsp[i] = &auxes[i].txn; |
6bc60024 | 1161 | } |
a88b4e04 | 1162 | nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops); |
6bc60024 | 1163 | |
6bc60024 | 1164 | for (i = 0; i < n_ops; i++) { |
72d32ac0 | 1165 | struct op_auxdata *aux = &auxes[i]; |
eabe7c68 | 1166 | struct nl_transaction *txn = &auxes[i].txn; |
c2b565b5 | 1167 | struct dpif_op *op = ops[i]; |
b99d3cee BP |
1168 | struct dpif_flow_put *put; |
1169 | struct dpif_flow_del *del; | |
6bc60024 | 1170 | |
b99d3cee | 1171 | op->error = txn->error; |
6bc60024 | 1172 | |
b99d3cee BP |
1173 | switch (op->type) { |
1174 | case DPIF_OP_FLOW_PUT: | |
1175 | put = &op->u.flow_put; | |
cfceb2b5 | 1176 | if (put->stats) { |
b99d3cee | 1177 | if (!op->error) { |
cfceb2b5 BP |
1178 | struct dpif_linux_flow reply; |
1179 | ||
1180 | op->error = dpif_linux_flow_from_ofpbuf(&reply, | |
1181 | txn->reply); | |
1182 | if (!op->error) { | |
1183 | dpif_linux_flow_get_stats(&reply, put->stats); | |
1184 | } | |
1185 | } | |
1186 | ||
1187 | if (op->error) { | |
1188 | memset(put->stats, 0, sizeof *put->stats); | |
6bc60024 BP |
1189 | } |
1190 | } | |
b99d3cee BP |
1191 | break; |
1192 | ||
1193 | case DPIF_OP_FLOW_DEL: | |
1194 | del = &op->u.flow_del; | |
cfceb2b5 | 1195 | if (del->stats) { |
b99d3cee | 1196 | if (!op->error) { |
cfceb2b5 BP |
1197 | struct dpif_linux_flow reply; |
1198 | ||
1199 | op->error = dpif_linux_flow_from_ofpbuf(&reply, | |
1200 | txn->reply); | |
1201 | if (!op->error) { | |
1202 | dpif_linux_flow_get_stats(&reply, del->stats); | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | if (op->error) { | |
1207 | memset(del->stats, 0, sizeof *del->stats); | |
b99d3cee BP |
1208 | } |
1209 | } | |
1210 | break; | |
1211 | ||
1212 | case DPIF_OP_EXECUTE: | |
1213 | break; | |
1214 | ||
1215 | default: | |
6bc60024 BP |
1216 | NOT_REACHED(); |
1217 | } | |
1218 | ||
72d32ac0 BP |
1219 | ofpbuf_uninit(&aux->request); |
1220 | ofpbuf_uninit(&aux->reply); | |
6bc60024 | 1221 | } |
eabe7c68 BP |
1222 | } |
1223 | ||
1224 | static void | |
1225 | dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops) | |
1226 | { | |
1227 | while (n_ops > 0) { | |
1228 | size_t chunk = MIN(n_ops, MAX_OPS); | |
1229 | dpif_linux_operate__(dpif, ops, chunk); | |
1230 | ops += chunk; | |
1231 | n_ops -= chunk; | |
1232 | } | |
6bc60024 BP |
1233 | } |
1234 | ||
96fba48f | 1235 | static int |
9fafa796 | 1236 | dpif_linux_recv_set__(struct dpif *dpif_, bool enable) |
96fba48f | 1237 | { |
982b8810 | 1238 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
982b8810 | 1239 | |
a12b3ead | 1240 | if ((dpif->epoll_fd >= 0) == enable) { |
982b8810 | 1241 | return 0; |
17411ecf | 1242 | } |
b063d9f0 | 1243 | |
a12b3ead | 1244 | if (!enable) { |
fe3d61b3 | 1245 | destroy_channels(dpif); |
a12b3ead | 1246 | } else { |
989fd548 JP |
1247 | struct dpif_port_dump port_dump; |
1248 | struct dpif_port port; | |
982b8810 | 1249 | |
50f80534 | 1250 | if (dpif->epoll_fd < 0) { |
9fafa796 BP |
1251 | dpif->epoll_fd = epoll_create(10); |
1252 | if (dpif->epoll_fd < 0) { | |
1253 | return errno; | |
1254 | } | |
50f80534 BP |
1255 | } |
1256 | ||
989fd548 JP |
1257 | DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) { |
1258 | struct dpif_linux_vport vport_request; | |
1259 | struct nl_sock *sock; | |
1260 | uint32_t upcall_pid; | |
1261 | int error; | |
50f80534 | 1262 | |
989fd548 | 1263 | error = nl_sock_create(NETLINK_GENERIC, &sock); |
b063d9f0 | 1264 | if (error) { |
17411ecf | 1265 | return error; |
982b8810 | 1266 | } |
50f80534 | 1267 | |
989fd548 JP |
1268 | upcall_pid = nl_sock_pid(sock); |
1269 | ||
1270 | dpif_linux_vport_init(&vport_request); | |
1271 | vport_request.cmd = OVS_VPORT_CMD_SET; | |
1272 | vport_request.dp_ifindex = dpif->dp_ifindex; | |
1273 | vport_request.port_no = port.port_no; | |
1274 | vport_request.upcall_pid = &upcall_pid; | |
1275 | error = dpif_linux_vport_transact(&vport_request, NULL, NULL); | |
1276 | if (!error) { | |
1277 | VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32, | |
1278 | dpif_name(&dpif->dpif), vport_request.port_no, | |
1279 | upcall_pid); | |
1280 | } else { | |
1281 | VLOG_WARN_RL(&error_rl, | |
1282 | "%s: failed to set upcall pid on port: %s", | |
10a89ef0 | 1283 | dpif_name(&dpif->dpif), ovs_strerror(error)); |
989fd548 JP |
1284 | nl_sock_destroy(sock); |
1285 | ||
1286 | if (error == ENODEV || error == ENOENT) { | |
1287 | /* This device isn't there, but keep trying the others. */ | |
1288 | continue; | |
1289 | } else { | |
1290 | return error; | |
1291 | } | |
50f80534 | 1292 | } |
14b4d2f9 | 1293 | |
989fd548 JP |
1294 | error = add_channel(dpif, port.port_no, sock); |
1295 | if (error) { | |
1296 | VLOG_INFO("%s: could not add channel for port %s", | |
1297 | dpif_name(dpif_), port.name); | |
1298 | nl_sock_destroy(sock); | |
1299 | return error; | |
1300 | } | |
982b8810 BP |
1301 | } |
1302 | } | |
b063d9f0 | 1303 | |
b063d9f0 | 1304 | return 0; |
96fba48f BP |
1305 | } |
1306 | ||
9fafa796 BP |
1307 | static int |
1308 | dpif_linux_recv_set(struct dpif *dpif_, bool enable) | |
1309 | { | |
1310 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); | |
1311 | int error; | |
1312 | ||
97be1538 | 1313 | ovs_mutex_lock(&dpif->upcall_lock); |
9fafa796 | 1314 | error = dpif_linux_recv_set__(dpif_, enable); |
97be1538 | 1315 | ovs_mutex_unlock(&dpif->upcall_lock); |
9fafa796 BP |
1316 | |
1317 | return error; | |
1318 | } | |
1319 | ||
aae51f53 BP |
1320 | static int |
1321 | dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED, | |
1322 | uint32_t queue_id, uint32_t *priority) | |
1323 | { | |
1324 | if (queue_id < 0xf000) { | |
17ee3c1f | 1325 | *priority = TC_H_MAKE(1 << 16, queue_id + 1); |
aae51f53 BP |
1326 | return 0; |
1327 | } else { | |
1328 | return EINVAL; | |
1329 | } | |
1330 | } | |
1331 | ||
96fba48f | 1332 | static int |
982b8810 | 1333 | parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall, |
254f2dc8 | 1334 | int *dp_ifindex) |
856081f6 | 1335 | { |
df2c07f4 | 1336 | static const struct nl_policy ovs_packet_policy[] = { |
856081f6 | 1337 | /* Always present. */ |
df2c07f4 | 1338 | [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC, |
856081f6 | 1339 | .min_len = ETH_HEADER_LEN }, |
df2c07f4 | 1340 | [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED }, |
856081f6 | 1341 | |
df2c07f4 | 1342 | /* OVS_PACKET_CMD_ACTION only. */ |
e995e3df | 1343 | [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true }, |
856081f6 BP |
1344 | }; |
1345 | ||
df2c07f4 JP |
1346 | struct ovs_header *ovs_header; |
1347 | struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)]; | |
982b8810 BP |
1348 | struct nlmsghdr *nlmsg; |
1349 | struct genlmsghdr *genl; | |
1350 | struct ofpbuf b; | |
aaff4b55 | 1351 | int type; |
982b8810 BP |
1352 | |
1353 | ofpbuf_use_const(&b, buf->data, buf->size); | |
1354 | ||
1355 | nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg); | |
1356 | genl = ofpbuf_try_pull(&b, sizeof *genl); | |
df2c07f4 JP |
1357 | ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header); |
1358 | if (!nlmsg || !genl || !ovs_header | |
1359 | || nlmsg->nlmsg_type != ovs_packet_family | |
1360 | || !nl_policy_parse(&b, 0, ovs_packet_policy, a, | |
1361 | ARRAY_SIZE(ovs_packet_policy))) { | |
856081f6 BP |
1362 | return EINVAL; |
1363 | } | |
1364 | ||
df2c07f4 JP |
1365 | type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS |
1366 | : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION | |
aaff4b55 BP |
1367 | : -1); |
1368 | if (type < 0) { | |
1369 | return EINVAL; | |
1370 | } | |
82272ede | 1371 | |
aaff4b55 BP |
1372 | memset(upcall, 0, sizeof *upcall); |
1373 | upcall->type = type; | |
856081f6 | 1374 | upcall->packet = buf; |
ebc56baa BP |
1375 | upcall->packet->data = CONST_CAST(struct nlattr *, |
1376 | nl_attr_get(a[OVS_PACKET_ATTR_PACKET])); | |
df2c07f4 | 1377 | upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]); |
ebc56baa BP |
1378 | upcall->key = CONST_CAST(struct nlattr *, |
1379 | nl_attr_get(a[OVS_PACKET_ATTR_KEY])); | |
df2c07f4 | 1380 | upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]); |
e995e3df | 1381 | upcall->userdata = a[OVS_PACKET_ATTR_USERDATA]; |
df2c07f4 | 1382 | *dp_ifindex = ovs_header->dp_ifindex; |
982b8810 | 1383 | |
856081f6 BP |
1384 | return 0; |
1385 | } | |
1386 | ||
1387 | static int | |
9fafa796 BP |
1388 | dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall, |
1389 | struct ofpbuf *buf) | |
96fba48f BP |
1390 | { |
1391 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); | |
17411ecf | 1392 | int read_tries = 0; |
96fba48f | 1393 | |
a12b3ead | 1394 | if (dpif->epoll_fd < 0) { |
17411ecf | 1395 | return EAGAIN; |
982b8810 BP |
1396 | } |
1397 | ||
989fd548 | 1398 | if (dpif->event_offset >= dpif->n_events) { |
8522ba09 | 1399 | int retval; |
989fd548 JP |
1400 | |
1401 | dpif->event_offset = dpif->n_events = 0; | |
f6d1465c | 1402 | |
8522ba09 | 1403 | do { |
989fd548 JP |
1404 | retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events, |
1405 | dpif->uc_array_size, 0); | |
8522ba09 BP |
1406 | } while (retval < 0 && errno == EINTR); |
1407 | if (retval < 0) { | |
1408 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1); | |
10a89ef0 | 1409 | VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno)); |
989fd548 JP |
1410 | } else if (retval > 0) { |
1411 | dpif->n_events = retval; | |
8522ba09 | 1412 | } |
8522ba09 BP |
1413 | } |
1414 | ||
989fd548 JP |
1415 | while (dpif->event_offset < dpif->n_events) { |
1416 | int idx = dpif->epoll_events[dpif->event_offset].data.u32; | |
1417 | struct dpif_channel *ch = &dpif->channels[idx]; | |
8522ba09 | 1418 | |
989fd548 | 1419 | dpif->event_offset++; |
17411ecf | 1420 | |
f6d1465c | 1421 | for (;;) { |
8522ba09 | 1422 | int dp_ifindex; |
f6d1465c | 1423 | int error; |
17411ecf | 1424 | |
f6d1465c BP |
1425 | if (++read_tries > 50) { |
1426 | return EAGAIN; | |
1427 | } | |
17411ecf | 1428 | |
fe3d61b3 | 1429 | error = nl_sock_recv(ch->sock, buf, false); |
14b4d2f9 BP |
1430 | if (error == ENOBUFS) { |
1431 | /* ENOBUFS typically means that we've received so many | |
1432 | * packets that the buffer overflowed. Try again | |
1433 | * immediately because there's almost certainly a packet | |
1434 | * waiting for us. */ | |
1435 | report_loss(dpif_, ch); | |
1436 | continue; | |
1437 | } | |
1438 | ||
1439 | ch->last_poll = time_msec(); | |
72d32ac0 | 1440 | if (error) { |
72d32ac0 BP |
1441 | if (error == EAGAIN) { |
1442 | break; | |
1443 | } | |
f6d1465c BP |
1444 | return error; |
1445 | } | |
17411ecf | 1446 | |
f6d1465c | 1447 | error = parse_odp_packet(buf, upcall, &dp_ifindex); |
a12b3ead | 1448 | if (!error && dp_ifindex == dpif->dp_ifindex) { |
f6d1465c | 1449 | return 0; |
989fd548 | 1450 | } else if (error) { |
f6d1465c | 1451 | return error; |
17411ecf | 1452 | } |
982b8810 | 1453 | } |
50f80534 | 1454 | } |
982b8810 BP |
1455 | |
1456 | return EAGAIN; | |
96fba48f BP |
1457 | } |
1458 | ||
9fafa796 BP |
1459 | static int |
1460 | dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall, | |
1461 | struct ofpbuf *buf) | |
1462 | { | |
1463 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); | |
1464 | int error; | |
1465 | ||
97be1538 | 1466 | ovs_mutex_lock(&dpif->upcall_lock); |
9fafa796 | 1467 | error = dpif_linux_recv__(dpif_, upcall, buf); |
97be1538 | 1468 | ovs_mutex_unlock(&dpif->upcall_lock); |
9fafa796 BP |
1469 | |
1470 | return error; | |
1471 | } | |
1472 | ||
96fba48f BP |
1473 | static void |
1474 | dpif_linux_recv_wait(struct dpif *dpif_) | |
1475 | { | |
9fafa796 | 1476 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); |
17411ecf | 1477 | |
97be1538 | 1478 | ovs_mutex_lock(&dpif->upcall_lock); |
9fafa796 BP |
1479 | if (dpif->epoll_fd >= 0) { |
1480 | poll_fd_wait(dpif->epoll_fd, POLLIN); | |
17411ecf | 1481 | } |
97be1538 | 1482 | ovs_mutex_unlock(&dpif->upcall_lock); |
96fba48f BP |
1483 | } |
1484 | ||
1ba530f4 BP |
1485 | static void |
1486 | dpif_linux_recv_purge(struct dpif *dpif_) | |
1487 | { | |
1488 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); | |
17411ecf | 1489 | |
97be1538 | 1490 | ovs_mutex_lock(&dpif->upcall_lock); |
9fafa796 BP |
1491 | if (dpif->epoll_fd >= 0) { |
1492 | struct dpif_channel *ch; | |
1ba530f4 | 1493 | |
9fafa796 BP |
1494 | for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; |
1495 | ch++) { | |
1496 | if (ch->sock) { | |
1497 | nl_sock_drain(ch->sock); | |
1498 | } | |
989fd548 | 1499 | } |
1ba530f4 | 1500 | } |
97be1538 | 1501 | ovs_mutex_unlock(&dpif->upcall_lock); |
1ba530f4 BP |
1502 | } |
1503 | ||
96fba48f | 1504 | const struct dpif_class dpif_linux_class = { |
1a6f1e2a | 1505 | "system", |
d3d22744 | 1506 | dpif_linux_enumerate, |
0aeaabc8 | 1507 | NULL, |
96fba48f BP |
1508 | dpif_linux_open, |
1509 | dpif_linux_close, | |
7dab847a | 1510 | dpif_linux_destroy, |
e4516b20 BP |
1511 | NULL, /* run */ |
1512 | NULL, /* wait */ | |
96fba48f | 1513 | dpif_linux_get_stats, |
96fba48f BP |
1514 | dpif_linux_port_add, |
1515 | dpif_linux_port_del, | |
1516 | dpif_linux_port_query_by_number, | |
1517 | dpif_linux_port_query_by_name, | |
996c1b3d | 1518 | dpif_linux_get_max_ports, |
98403001 | 1519 | dpif_linux_port_get_pid, |
b0ec0f27 BP |
1520 | dpif_linux_port_dump_start, |
1521 | dpif_linux_port_dump_next, | |
1522 | dpif_linux_port_dump_done, | |
e9e28be3 BP |
1523 | dpif_linux_port_poll, |
1524 | dpif_linux_port_poll_wait, | |
96fba48f BP |
1525 | dpif_linux_flow_get, |
1526 | dpif_linux_flow_put, | |
1527 | dpif_linux_flow_del, | |
1528 | dpif_linux_flow_flush, | |
704a1e09 BP |
1529 | dpif_linux_flow_dump_start, |
1530 | dpif_linux_flow_dump_next, | |
1531 | dpif_linux_flow_dump_done, | |
96fba48f | 1532 | dpif_linux_execute, |
6bc60024 | 1533 | dpif_linux_operate, |
a12b3ead | 1534 | dpif_linux_recv_set, |
aae51f53 | 1535 | dpif_linux_queue_to_priority, |
96fba48f BP |
1536 | dpif_linux_recv, |
1537 | dpif_linux_recv_wait, | |
1ba530f4 | 1538 | dpif_linux_recv_purge, |
96fba48f BP |
1539 | }; |
1540 | \f | |
96fba48f | 1541 | static int |
982b8810 | 1542 | dpif_linux_init(void) |
96fba48f | 1543 | { |
eb8ed438 BP |
1544 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
1545 | static int error; | |
982b8810 | 1546 | |
eb8ed438 | 1547 | if (ovsthread_once_start(&once)) { |
df2c07f4 JP |
1548 | error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY, |
1549 | &ovs_datapath_family); | |
37a1300c BP |
1550 | if (error) { |
1551 | VLOG_ERR("Generic Netlink family '%s' does not exist. " | |
1552 | "The Open vSwitch kernel module is probably not loaded.", | |
df2c07f4 | 1553 | OVS_DATAPATH_FAMILY); |
37a1300c | 1554 | } |
f0fef760 | 1555 | if (!error) { |
df2c07f4 | 1556 | error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family); |
f0fef760 | 1557 | } |
37a1300c | 1558 | if (!error) { |
df2c07f4 | 1559 | error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family); |
37a1300c | 1560 | } |
aaff4b55 | 1561 | if (!error) { |
df2c07f4 JP |
1562 | error = nl_lookup_genl_family(OVS_PACKET_FAMILY, |
1563 | &ovs_packet_family); | |
aaff4b55 | 1564 | } |
c7178a0b EJ |
1565 | if (!error) { |
1566 | error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP, | |
213a13ed EJ |
1567 | &ovs_vport_mcgroup, |
1568 | OVS_VPORT_MCGROUP_FALLBACK_ID); | |
c7178a0b | 1569 | } |
eb8ed438 BP |
1570 | |
1571 | ovsthread_once_done(&once); | |
982b8810 BP |
1572 | } |
1573 | ||
1574 | return error; | |
96fba48f BP |
1575 | } |
1576 | ||
c19e6535 BP |
1577 | bool |
1578 | dpif_linux_is_internal_device(const char *name) | |
9fe3b9a2 | 1579 | { |
c19e6535 BP |
1580 | struct dpif_linux_vport reply; |
1581 | struct ofpbuf *buf; | |
9fe3b9a2 | 1582 | int error; |
96fba48f | 1583 | |
c19e6535 BP |
1584 | error = dpif_linux_vport_get(name, &reply, &buf); |
1585 | if (!error) { | |
1586 | ofpbuf_delete(buf); | |
141d9ce4 | 1587 | } else if (error != ENODEV && error != ENOENT) { |
c19e6535 | 1588 | VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)", |
10a89ef0 | 1589 | name, ovs_strerror(error)); |
96fba48f BP |
1590 | } |
1591 | ||
df2c07f4 | 1592 | return reply.type == OVS_VPORT_TYPE_INTERNAL; |
96fba48f | 1593 | } |
c19e6535 | 1594 | \f |
df2c07f4 | 1595 | /* Parses the contents of 'buf', which contains a "struct ovs_header" followed |
c19e6535 BP |
1596 | * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a |
1597 | * positive errno value. | |
1598 | * | |
1599 | * 'vport' will contain pointers into 'buf', so the caller should not free | |
1600 | * 'buf' while 'vport' is still in use. */ | |
1601 | static int | |
1602 | dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport, | |
1603 | const struct ofpbuf *buf) | |
1604 | { | |
df2c07f4 JP |
1605 | static const struct nl_policy ovs_vport_policy[] = { |
1606 | [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 }, | |
1607 | [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 }, | |
1608 | [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ }, | |
b063d9f0 | 1609 | [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 }, |
f7df9823 | 1610 | [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats), |
c19e6535 | 1611 | .optional = true }, |
df2c07f4 | 1612 | [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true }, |
c19e6535 BP |
1613 | }; |
1614 | ||
df2c07f4 JP |
1615 | struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)]; |
1616 | struct ovs_header *ovs_header; | |
f0fef760 BP |
1617 | struct nlmsghdr *nlmsg; |
1618 | struct genlmsghdr *genl; | |
1619 | struct ofpbuf b; | |
c19e6535 BP |
1620 | |
1621 | dpif_linux_vport_init(vport); | |
1622 | ||
f0fef760 BP |
1623 | ofpbuf_use_const(&b, buf->data, buf->size); |
1624 | nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg); | |
1625 | genl = ofpbuf_try_pull(&b, sizeof *genl); | |
df2c07f4 JP |
1626 | ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header); |
1627 | if (!nlmsg || !genl || !ovs_header | |
1628 | || nlmsg->nlmsg_type != ovs_vport_family | |
1629 | || !nl_policy_parse(&b, 0, ovs_vport_policy, a, | |
1630 | ARRAY_SIZE(ovs_vport_policy))) { | |
c19e6535 BP |
1631 | return EINVAL; |
1632 | } | |
c19e6535 | 1633 | |
f0fef760 | 1634 | vport->cmd = genl->cmd; |
df2c07f4 | 1635 | vport->dp_ifindex = ovs_header->dp_ifindex; |
4e022ec0 | 1636 | vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]); |
df2c07f4 JP |
1637 | vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]); |
1638 | vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]); | |
b063d9f0 | 1639 | if (a[OVS_VPORT_ATTR_UPCALL_PID]) { |
a24a6574 | 1640 | vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]); |
b063d9f0 | 1641 | } |
df2c07f4 JP |
1642 | if (a[OVS_VPORT_ATTR_STATS]) { |
1643 | vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]); | |
1644 | } | |
df2c07f4 JP |
1645 | if (a[OVS_VPORT_ATTR_OPTIONS]) { |
1646 | vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]); | |
1647 | vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]); | |
c19e6535 | 1648 | } |
c19e6535 BP |
1649 | return 0; |
1650 | } | |
1651 | ||
df2c07f4 | 1652 | /* Appends to 'buf' (which must initially be empty) a "struct ovs_header" |
c19e6535 BP |
1653 | * followed by Netlink attributes corresponding to 'vport'. */ |
1654 | static void | |
1655 | dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport, | |
1656 | struct ofpbuf *buf) | |
1657 | { | |
df2c07f4 | 1658 | struct ovs_header *ovs_header; |
f0fef760 | 1659 | |
df2c07f4 | 1660 | nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO, |
69685a88 | 1661 | vport->cmd, OVS_VPORT_VERSION); |
c19e6535 | 1662 | |
df2c07f4 JP |
1663 | ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header); |
1664 | ovs_header->dp_ifindex = vport->dp_ifindex; | |
c19e6535 | 1665 | |
4e022ec0 AW |
1666 | if (vport->port_no != ODPP_NONE) { |
1667 | nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no); | |
c19e6535 BP |
1668 | } |
1669 | ||
df2c07f4 JP |
1670 | if (vport->type != OVS_VPORT_TYPE_UNSPEC) { |
1671 | nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type); | |
c19e6535 BP |
1672 | } |
1673 | ||
1674 | if (vport->name) { | |
df2c07f4 | 1675 | nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name); |
c19e6535 BP |
1676 | } |
1677 | ||
a24a6574 BP |
1678 | if (vport->upcall_pid) { |
1679 | nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid); | |
1680 | } | |
b063d9f0 | 1681 | |
c19e6535 | 1682 | if (vport->stats) { |
df2c07f4 | 1683 | nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS, |
c19e6535 BP |
1684 | vport->stats, sizeof *vport->stats); |
1685 | } | |
1686 | ||
c19e6535 | 1687 | if (vport->options) { |
df2c07f4 | 1688 | nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS, |
c19e6535 BP |
1689 | vport->options, vport->options_len); |
1690 | } | |
c19e6535 BP |
1691 | } |
1692 | ||
1693 | /* Clears 'vport' to "empty" values. */ | |
1694 | void | |
1695 | dpif_linux_vport_init(struct dpif_linux_vport *vport) | |
1696 | { | |
1697 | memset(vport, 0, sizeof *vport); | |
4e022ec0 | 1698 | vport->port_no = ODPP_NONE; |
c19e6535 BP |
1699 | } |
1700 | ||
1701 | /* Executes 'request' in the kernel datapath. If the command fails, returns a | |
1702 | * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0 | |
1703 | * without doing anything else. If 'reply' and 'bufp' are nonnull, then the | |
df2c07f4 | 1704 | * result of the command is expected to be an ovs_vport also, which is decoded |
c19e6535 BP |
1705 | * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the |
1706 | * reply is no longer needed ('reply' will contain pointers into '*bufp'). */ | |
1707 | int | |
1708 | dpif_linux_vport_transact(const struct dpif_linux_vport *request, | |
1709 | struct dpif_linux_vport *reply, | |
1710 | struct ofpbuf **bufp) | |
1711 | { | |
f0fef760 | 1712 | struct ofpbuf *request_buf; |
c19e6535 BP |
1713 | int error; |
1714 | ||
cb22974d | 1715 | ovs_assert((reply != NULL) == (bufp != NULL)); |
c19e6535 | 1716 | |
42bb6c72 BP |
1717 | error = dpif_linux_init(); |
1718 | if (error) { | |
1719 | if (reply) { | |
1720 | *bufp = NULL; | |
1721 | dpif_linux_vport_init(reply); | |
1722 | } | |
1723 | return error; | |
1724 | } | |
1725 | ||
f0fef760 BP |
1726 | request_buf = ofpbuf_new(1024); |
1727 | dpif_linux_vport_to_ofpbuf(request, request_buf); | |
a88b4e04 | 1728 | error = nl_transact(NETLINK_GENERIC, request_buf, bufp); |
f0fef760 | 1729 | ofpbuf_delete(request_buf); |
c19e6535 | 1730 | |
f0fef760 BP |
1731 | if (reply) { |
1732 | if (!error) { | |
1733 | error = dpif_linux_vport_from_ofpbuf(reply, *bufp); | |
1734 | } | |
c19e6535 | 1735 | if (error) { |
f0fef760 BP |
1736 | dpif_linux_vport_init(reply); |
1737 | ofpbuf_delete(*bufp); | |
1738 | *bufp = NULL; | |
c19e6535 | 1739 | } |
c19e6535 BP |
1740 | } |
1741 | return error; | |
1742 | } | |
1743 | ||
1744 | /* Obtains information about the kernel vport named 'name' and stores it into | |
1745 | * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no | |
1746 | * longer needed ('reply' will contain pointers into '*bufp'). */ | |
1747 | int | |
1748 | dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply, | |
1749 | struct ofpbuf **bufp) | |
1750 | { | |
1751 | struct dpif_linux_vport request; | |
1752 | ||
1753 | dpif_linux_vport_init(&request); | |
df2c07f4 | 1754 | request.cmd = OVS_VPORT_CMD_GET; |
c19e6535 BP |
1755 | request.name = name; |
1756 | ||
1757 | return dpif_linux_vport_transact(&request, reply, bufp); | |
1758 | } | |
d6569377 | 1759 | \f |
df2c07f4 | 1760 | /* Parses the contents of 'buf', which contains a "struct ovs_header" followed |
aaff4b55 BP |
1761 | * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a |
1762 | * positive errno value. | |
d6569377 BP |
1763 | * |
1764 | * 'dp' will contain pointers into 'buf', so the caller should not free 'buf' | |
1765 | * while 'dp' is still in use. */ | |
1766 | static int | |
1767 | dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf) | |
1768 | { | |
df2c07f4 JP |
1769 | static const struct nl_policy ovs_datapath_policy[] = { |
1770 | [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ }, | |
f7df9823 | 1771 | [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats), |
d6569377 | 1772 | .optional = true }, |
d6569377 BP |
1773 | }; |
1774 | ||
df2c07f4 JP |
1775 | struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)]; |
1776 | struct ovs_header *ovs_header; | |
aaff4b55 BP |
1777 | struct nlmsghdr *nlmsg; |
1778 | struct genlmsghdr *genl; | |
1779 | struct ofpbuf b; | |
d6569377 BP |
1780 | |
1781 | dpif_linux_dp_init(dp); | |
1782 | ||
aaff4b55 BP |
1783 | ofpbuf_use_const(&b, buf->data, buf->size); |
1784 | nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg); | |
1785 | genl = ofpbuf_try_pull(&b, sizeof *genl); | |
df2c07f4 JP |
1786 | ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header); |
1787 | if (!nlmsg || !genl || !ovs_header | |
1788 | || nlmsg->nlmsg_type != ovs_datapath_family | |
1789 | || !nl_policy_parse(&b, 0, ovs_datapath_policy, a, | |
1790 | ARRAY_SIZE(ovs_datapath_policy))) { | |
d6569377 BP |
1791 | return EINVAL; |
1792 | } | |
d6569377 | 1793 | |
aaff4b55 | 1794 | dp->cmd = genl->cmd; |
df2c07f4 JP |
1795 | dp->dp_ifindex = ovs_header->dp_ifindex; |
1796 | dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]); | |
1797 | if (a[OVS_DP_ATTR_STATS]) { | |
d6569377 BP |
1798 | /* Can't use structure assignment because Netlink doesn't ensure |
1799 | * sufficient alignment for 64-bit members. */ | |
df2c07f4 | 1800 | memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]), |
d6569377 BP |
1801 | sizeof dp->stats); |
1802 | } | |
982b8810 | 1803 | |
d6569377 BP |
1804 | return 0; |
1805 | } | |
1806 | ||
aaff4b55 | 1807 | /* Appends to 'buf' the Generic Netlink message described by 'dp'. */ |
d6569377 BP |
1808 | static void |
1809 | dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf) | |
1810 | { | |
df2c07f4 | 1811 | struct ovs_header *ovs_header; |
d6569377 | 1812 | |
df2c07f4 | 1813 | nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family, |
69685a88 JG |
1814 | NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, |
1815 | OVS_DATAPATH_VERSION); | |
aaff4b55 | 1816 | |
df2c07f4 JP |
1817 | ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header); |
1818 | ovs_header->dp_ifindex = dp->dp_ifindex; | |
d6569377 BP |
1819 | |
1820 | if (dp->name) { | |
df2c07f4 | 1821 | nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name); |
d6569377 BP |
1822 | } |
1823 | ||
a24a6574 BP |
1824 | if (dp->upcall_pid) { |
1825 | nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid); | |
1826 | } | |
b063d9f0 | 1827 | |
df2c07f4 | 1828 | /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */ |
d6569377 BP |
1829 | } |
1830 | ||
1831 | /* Clears 'dp' to "empty" values. */ | |
d3d8f1f7 | 1832 | static void |
d6569377 BP |
1833 | dpif_linux_dp_init(struct dpif_linux_dp *dp) |
1834 | { | |
1835 | memset(dp, 0, sizeof *dp); | |
d6569377 BP |
1836 | } |
1837 | ||
aaff4b55 BP |
1838 | static void |
1839 | dpif_linux_dp_dump_start(struct nl_dump *dump) | |
1840 | { | |
1841 | struct dpif_linux_dp request; | |
1842 | struct ofpbuf *buf; | |
1843 | ||
1844 | dpif_linux_dp_init(&request); | |
df2c07f4 | 1845 | request.cmd = OVS_DP_CMD_GET; |
aaff4b55 BP |
1846 | |
1847 | buf = ofpbuf_new(1024); | |
1848 | dpif_linux_dp_to_ofpbuf(&request, buf); | |
a88b4e04 | 1849 | nl_dump_start(dump, NETLINK_GENERIC, buf); |
aaff4b55 BP |
1850 | ofpbuf_delete(buf); |
1851 | } | |
1852 | ||
d6569377 BP |
1853 | /* Executes 'request' in the kernel datapath. If the command fails, returns a |
1854 | * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0 | |
1855 | * without doing anything else. If 'reply' and 'bufp' are nonnull, then the | |
aaff4b55 BP |
1856 | * result of the command is expected to be of the same form, which is decoded |
1857 | * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the | |
1858 | * reply is no longer needed ('reply' will contain pointers into '*bufp'). */ | |
d3d8f1f7 | 1859 | static int |
d6569377 BP |
1860 | dpif_linux_dp_transact(const struct dpif_linux_dp *request, |
1861 | struct dpif_linux_dp *reply, struct ofpbuf **bufp) | |
1862 | { | |
aaff4b55 | 1863 | struct ofpbuf *request_buf; |
d6569377 | 1864 | int error; |
d6569377 | 1865 | |
cb22974d | 1866 | ovs_assert((reply != NULL) == (bufp != NULL)); |
d6569377 | 1867 | |
aaff4b55 BP |
1868 | request_buf = ofpbuf_new(1024); |
1869 | dpif_linux_dp_to_ofpbuf(request, request_buf); | |
a88b4e04 | 1870 | error = nl_transact(NETLINK_GENERIC, request_buf, bufp); |
aaff4b55 | 1871 | ofpbuf_delete(request_buf); |
d6569377 | 1872 | |
aaff4b55 BP |
1873 | if (reply) { |
1874 | if (!error) { | |
1875 | error = dpif_linux_dp_from_ofpbuf(reply, *bufp); | |
1876 | } | |
d6569377 | 1877 | if (error) { |
aaff4b55 BP |
1878 | dpif_linux_dp_init(reply); |
1879 | ofpbuf_delete(*bufp); | |
1880 | *bufp = NULL; | |
d6569377 | 1881 | } |
d6569377 BP |
1882 | } |
1883 | return error; | |
1884 | } | |
1885 | ||
1886 | /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'. | |
1887 | * The caller must free '*bufp' when the reply is no longer needed ('reply' | |
1888 | * will contain pointers into '*bufp'). */ | |
d3d8f1f7 | 1889 | static int |
d6569377 BP |
1890 | dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply, |
1891 | struct ofpbuf **bufp) | |
1892 | { | |
1893 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); | |
1894 | struct dpif_linux_dp request; | |
1895 | ||
1896 | dpif_linux_dp_init(&request); | |
df2c07f4 | 1897 | request.cmd = OVS_DP_CMD_GET; |
254f2dc8 | 1898 | request.dp_ifindex = dpif->dp_ifindex; |
d6569377 BP |
1899 | |
1900 | return dpif_linux_dp_transact(&request, reply, bufp); | |
1901 | } | |
1902 | \f | |
df2c07f4 | 1903 | /* Parses the contents of 'buf', which contains a "struct ovs_header" followed |
37a1300c | 1904 | * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a |
d6569377 BP |
1905 | * positive errno value. |
1906 | * | |
1907 | * 'flow' will contain pointers into 'buf', so the caller should not free 'buf' | |
1908 | * while 'flow' is still in use. */ | |
1909 | static int | |
1910 | dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow, | |
1911 | const struct ofpbuf *buf) | |
1912 | { | |
df2c07f4 JP |
1913 | static const struct nl_policy ovs_flow_policy[] = { |
1914 | [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED }, | |
e6cc0bab | 1915 | [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true }, |
df2c07f4 | 1916 | [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true }, |
f7df9823 | 1917 | [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats), |
d6569377 | 1918 | .optional = true }, |
df2c07f4 JP |
1919 | [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true }, |
1920 | [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true }, | |
1921 | /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */ | |
d6569377 BP |
1922 | }; |
1923 | ||
df2c07f4 JP |
1924 | struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)]; |
1925 | struct ovs_header *ovs_header; | |
37a1300c BP |
1926 | struct nlmsghdr *nlmsg; |
1927 | struct genlmsghdr *genl; | |
1928 | struct ofpbuf b; | |
d6569377 BP |
1929 | |
1930 | dpif_linux_flow_init(flow); | |
1931 | ||
37a1300c BP |
1932 | ofpbuf_use_const(&b, buf->data, buf->size); |
1933 | nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg); | |
1934 | genl = ofpbuf_try_pull(&b, sizeof *genl); | |
df2c07f4 JP |
1935 | ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header); |
1936 | if (!nlmsg || !genl || !ovs_header | |
1937 | || nlmsg->nlmsg_type != ovs_flow_family | |
1938 | || !nl_policy_parse(&b, 0, ovs_flow_policy, a, | |
1939 | ARRAY_SIZE(ovs_flow_policy))) { | |
d6569377 BP |
1940 | return EINVAL; |
1941 | } | |
d6569377 | 1942 | |
37a1300c | 1943 | flow->nlmsg_flags = nlmsg->nlmsg_flags; |
df2c07f4 JP |
1944 | flow->dp_ifindex = ovs_header->dp_ifindex; |
1945 | flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]); | |
1946 | flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]); | |
e6cc0bab AZ |
1947 | |
1948 | if (a[OVS_FLOW_ATTR_MASK]) { | |
1949 | flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]); | |
1950 | flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]); | |
1951 | } | |
df2c07f4 JP |
1952 | if (a[OVS_FLOW_ATTR_ACTIONS]) { |
1953 | flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]); | |
1954 | flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]); | |
d6569377 | 1955 | } |
df2c07f4 JP |
1956 | if (a[OVS_FLOW_ATTR_STATS]) { |
1957 | flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]); | |
d6569377 | 1958 | } |
df2c07f4 JP |
1959 | if (a[OVS_FLOW_ATTR_TCP_FLAGS]) { |
1960 | flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]); | |
d6569377 | 1961 | } |
df2c07f4 JP |
1962 | if (a[OVS_FLOW_ATTR_USED]) { |
1963 | flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]); | |
9e980142 | 1964 | } |
d6569377 BP |
1965 | return 0; |
1966 | } | |
1967 | ||
df2c07f4 | 1968 | /* Appends to 'buf' (which must initially be empty) a "struct ovs_header" |
d6569377 BP |
1969 | * followed by Netlink attributes corresponding to 'flow'. */ |
1970 | static void | |
1971 | dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow, | |
1972 | struct ofpbuf *buf) | |
1973 | { | |
df2c07f4 | 1974 | struct ovs_header *ovs_header; |
d6569377 | 1975 | |
df2c07f4 | 1976 | nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family, |
30b44744 | 1977 | NLM_F_REQUEST | flow->nlmsg_flags, |
69685a88 | 1978 | flow->cmd, OVS_FLOW_VERSION); |
37a1300c | 1979 | |
df2c07f4 JP |
1980 | ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header); |
1981 | ovs_header->dp_ifindex = flow->dp_ifindex; | |
d6569377 BP |
1982 | |
1983 | if (flow->key_len) { | |
df2c07f4 | 1984 | nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len); |
d6569377 BP |
1985 | } |
1986 | ||
e6cc0bab AZ |
1987 | if (flow->mask_len) { |
1988 | nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len); | |
1989 | } | |
1990 | ||
d2a23af2 | 1991 | if (flow->actions || flow->actions_len) { |
df2c07f4 | 1992 | nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS, |
d6569377 BP |
1993 | flow->actions, flow->actions_len); |
1994 | } | |
1995 | ||
1996 | /* We never need to send these to the kernel. */ | |
cb22974d BP |
1997 | ovs_assert(!flow->stats); |
1998 | ovs_assert(!flow->tcp_flags); | |
1999 | ovs_assert(!flow->used); | |
d6569377 BP |
2000 | |
2001 | if (flow->clear) { | |
df2c07f4 | 2002 | nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR); |
d6569377 | 2003 | } |
d6569377 BP |
2004 | } |
2005 | ||
2006 | /* Clears 'flow' to "empty" values. */ | |
d3d8f1f7 | 2007 | static void |
d6569377 BP |
2008 | dpif_linux_flow_init(struct dpif_linux_flow *flow) |
2009 | { | |
2010 | memset(flow, 0, sizeof *flow); | |
2011 | } | |
2012 | ||
2013 | /* Executes 'request' in the kernel datapath. If the command fails, returns a | |
2014 | * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0 | |
2015 | * without doing anything else. If 'reply' and 'bufp' are nonnull, then the | |
37a1300c BP |
2016 | * result of the command is expected to be a flow also, which is decoded and |
2017 | * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply | |
2018 | * is no longer needed ('reply' will contain pointers into '*bufp'). */ | |
d3d8f1f7 | 2019 | static int |
30b44744 | 2020 | dpif_linux_flow_transact(struct dpif_linux_flow *request, |
d6569377 BP |
2021 | struct dpif_linux_flow *reply, struct ofpbuf **bufp) |
2022 | { | |
37a1300c | 2023 | struct ofpbuf *request_buf; |
d6569377 | 2024 | int error; |
d6569377 | 2025 | |
cb22974d | 2026 | ovs_assert((reply != NULL) == (bufp != NULL)); |
d6569377 | 2027 | |
30b44744 BP |
2028 | if (reply) { |
2029 | request->nlmsg_flags |= NLM_F_ECHO; | |
2030 | } | |
2031 | ||
37a1300c BP |
2032 | request_buf = ofpbuf_new(1024); |
2033 | dpif_linux_flow_to_ofpbuf(request, request_buf); | |
a88b4e04 | 2034 | error = nl_transact(NETLINK_GENERIC, request_buf, bufp); |
37a1300c | 2035 | ofpbuf_delete(request_buf); |
d6569377 | 2036 | |
37a1300c BP |
2037 | if (reply) { |
2038 | if (!error) { | |
2039 | error = dpif_linux_flow_from_ofpbuf(reply, *bufp); | |
2040 | } | |
d6569377 | 2041 | if (error) { |
37a1300c BP |
2042 | dpif_linux_flow_init(reply); |
2043 | ofpbuf_delete(*bufp); | |
2044 | *bufp = NULL; | |
d6569377 | 2045 | } |
d6569377 BP |
2046 | } |
2047 | return error; | |
2048 | } | |
2049 | ||
2050 | static void | |
2051 | dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow, | |
2052 | struct dpif_flow_stats *stats) | |
2053 | { | |
2054 | if (flow->stats) { | |
2055 | stats->n_packets = get_unaligned_u64(&flow->stats->n_packets); | |
2056 | stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes); | |
2057 | } else { | |
2058 | stats->n_packets = 0; | |
2059 | stats->n_bytes = 0; | |
2060 | } | |
0e70cdcb | 2061 | stats->used = flow->used ? get_32aligned_u64(flow->used) : 0; |
d6569377 BP |
2062 | stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0; |
2063 | } | |
14b4d2f9 | 2064 | \f |
14b4d2f9 BP |
2065 | /* Logs information about a packet that was recently lost in 'ch' (in |
2066 | * 'dpif_'). */ | |
2067 | static void | |
2068 | report_loss(struct dpif *dpif_, struct dpif_channel *ch) | |
2069 | { | |
2070 | struct dpif_linux *dpif = dpif_linux_cast(dpif_); | |
2071 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
14b4d2f9 BP |
2072 | struct ds s; |
2073 | ||
8d675c5a | 2074 | if (VLOG_DROP_WARN(&rl)) { |
14b4d2f9 BP |
2075 | return; |
2076 | } | |
2077 | ||
2078 | ds_init(&s); | |
2079 | if (ch->last_poll != LLONG_MIN) { | |
2080 | ds_put_format(&s, " (last polled %lld ms ago)", | |
2081 | time_msec() - ch->last_poll); | |
2082 | } | |
14b4d2f9 | 2083 | |
8d0abb5e BP |
2084 | VLOG_WARN("%s: lost packet on channel %td%s", |
2085 | dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s)); | |
14b4d2f9 BP |
2086 | ds_destroy(&s); |
2087 | } |