]> git.proxmox.com Git - mirror_ovs.git/blame - lib/dpif-linux.c
Global replace of Nicira Networks.
[mirror_ovs.git] / lib / dpif-linux.c
CommitLineData
96fba48f 1/*
e0edde6f 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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
BP
20
21#include <assert.h>
22#include <ctype.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <inttypes.h>
26#include <net/if.h>
b90fa799 27#include <linux/types.h>
aae51f53 28#include <linux/pkt_sched.h>
e9e28be3 29#include <linux/rtnetlink.h>
96fba48f 30#include <linux/sockios.h>
8522ba09 31#include <poll.h>
96fba48f 32#include <stdlib.h>
8522ba09 33#include <strings.h>
50f80534 34#include <sys/epoll.h>
10dcf8de 35#include <sys/stat.h>
96fba48f
BP
36#include <unistd.h>
37
773cd538 38#include "bitmap.h"
96fba48f 39#include "dpif-provider.h"
80e5eed9 40#include "dynamic-string.h"
eb8b28e7 41#include "flow.h"
3abc4a1a 42#include "netdev.h"
032aa6a3 43#include "netdev-linux.h"
c3827f61 44#include "netdev-vport.h"
45c8d3a1 45#include "netlink-notifier.h"
982b8810 46#include "netlink-socket.h"
856081f6 47#include "netlink.h"
feebdea2 48#include "odp-util.h"
96fba48f 49#include "ofpbuf.h"
213a13ed 50#include "openvswitch/datapath-compat.h"
e16a28b5 51#include "openvswitch/tunnel.h"
856081f6 52#include "packets.h"
96fba48f 53#include "poll-loop.h"
17411ecf 54#include "random.h"
54825e09 55#include "shash.h"
b3c01ed3 56#include "sset.h"
d6569377 57#include "unaligned.h"
96fba48f 58#include "util.h"
96fba48f 59#include "vlog.h"
5136ce49 60
d98e6007 61VLOG_DEFINE_THIS_MODULE(dpif_linux);
95b1d73a 62enum { MAX_PORTS = USHRT_MAX };
773cd538 63
17411ecf
JG
64enum { N_UPCALL_SOCKS = 16 };
65BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
8522ba09 66BUILD_ASSERT_DECL(N_UPCALL_SOCKS <= 32); /* We use a 32-bit word as a mask. */
17411ecf 67
24b019f8
JP
68/* This ethtool flag was introduced in Linux 2.6.24, so it might be
69 * missing if we have old headers. */
70#define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
71
d6569377 72struct dpif_linux_dp {
aaff4b55
BP
73 /* Generic Netlink header. */
74 uint8_t cmd;
d6569377 75
df2c07f4 76 /* struct ovs_header. */
254f2dc8 77 int dp_ifindex;
d6569377
BP
78
79 /* Attributes. */
df2c07f4 80 const char *name; /* OVS_DP_ATTR_NAME. */
a24a6574 81 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
df2c07f4 82 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
d6569377
BP
83};
84
85static void dpif_linux_dp_init(struct dpif_linux_dp *);
aaff4b55
BP
86static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
87 const struct ofpbuf *);
88static void dpif_linux_dp_dump_start(struct nl_dump *);
d6569377
BP
89static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
90 struct dpif_linux_dp *reply,
91 struct ofpbuf **bufp);
92static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
93 struct ofpbuf **bufp);
94
95struct dpif_linux_flow {
37a1300c
BP
96 /* Generic Netlink header. */
97 uint8_t cmd;
d6569377 98
df2c07f4 99 /* struct ovs_header. */
d6569377 100 unsigned int nlmsg_flags;
254f2dc8 101 int dp_ifindex;
d6569377
BP
102
103 /* Attributes.
104 *
0e70cdcb
BP
105 * The 'stats' member points to 64-bit data that might only be aligned on
106 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
107 * values.
d2a23af2 108 *
df2c07f4 109 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
d2a23af2 110 * the Netlink version of the command, even if actions_len is zero. */
df2c07f4 111 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
d6569377 112 size_t key_len;
df2c07f4 113 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
d6569377 114 size_t actions_len;
df2c07f4
JP
115 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
116 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
0e70cdcb 117 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
df2c07f4 118 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
d6569377
BP
119};
120
121static void dpif_linux_flow_init(struct dpif_linux_flow *);
37a1300c
BP
122static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
123 const struct ofpbuf *);
124static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
125 struct ofpbuf *);
30b44744 126static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
d6569377
BP
127 struct dpif_linux_flow *reply,
128 struct ofpbuf **bufp);
129static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
130 struct dpif_flow_stats *);
131
96fba48f
BP
132/* Datapath interface for the openvswitch Linux kernel module. */
133struct dpif_linux {
134 struct dpif dpif;
254f2dc8 135 int dp_ifindex;
e9e28be3 136
b063d9f0 137 /* Upcall messages. */
17411ecf 138 struct nl_sock *upcall_socks[N_UPCALL_SOCKS];
8522ba09 139 uint32_t ready_mask; /* 1-bit for each sock with unread messages. */
50f80534 140 int epoll_fd; /* epoll fd that includes the upcall socks. */
982b8810 141
e9e28be3 142 /* Change notification. */
b3c01ed3 143 struct sset changed_ports; /* Ports that have changed. */
2ee6545f 144 struct nln_notifier *port_notifier;
8b61709d 145 bool change_error;
773cd538 146
95b1d73a
PS
147 /* Port number allocation. */
148 uint16_t alloc_port_no;
96fba48f
BP
149};
150
151static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
152
df2c07f4
JP
153/* Generic Netlink family numbers for OVS. */
154static int ovs_datapath_family;
155static int ovs_vport_family;
156static int ovs_flow_family;
157static int ovs_packet_family;
982b8810
BP
158
159/* Generic Netlink socket. */
160static struct nl_sock *genl_sock;
c7178a0b 161static struct nln *nln = NULL;
982b8810
BP
162
163static int dpif_linux_init(void);
8f4a4df5 164static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
c7178a0b
EJ
165static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
166static void dpif_linux_port_changed(const void *vport, void *dpif);
a12b3ead 167static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint16_t port_no);
96fba48f 168
f0fef760
BP
169static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
170 struct ofpbuf *);
171static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
172 const struct ofpbuf *);
173
96fba48f
BP
174static struct dpif_linux *
175dpif_linux_cast(const struct dpif *dpif)
176{
177 dpif_assert_class(dpif, &dpif_linux_class);
178 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
179}
180
d3d22744 181static int
d0c23a1a 182dpif_linux_enumerate(struct sset *all_dps)
d3d22744 183{
aaff4b55
BP
184 struct nl_dump dump;
185 struct ofpbuf msg;
aaff4b55 186 int error;
982b8810 187
aaff4b55
BP
188 error = dpif_linux_init();
189 if (error) {
190 return error;
982b8810 191 }
d3d22744 192
aaff4b55
BP
193 dpif_linux_dp_dump_start(&dump);
194 while (nl_dump_next(&dump, &msg)) {
195 struct dpif_linux_dp dp;
d6569377 196
aaff4b55 197 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
d0c23a1a 198 sset_add(all_dps, dp.name);
d3d22744
BP
199 }
200 }
aaff4b55 201 return nl_dump_done(&dump);
d3d22744
BP
202}
203
96fba48f 204static int
4a387741
BP
205dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
206 bool create, struct dpif **dpifp)
96fba48f 207{
982b8810 208 struct dpif_linux_dp dp_request, dp;
c19e6535 209 struct ofpbuf *buf;
ea36840f 210 uint32_t upcall_pid;
c19e6535 211 int error;
96fba48f 212
982b8810
BP
213 error = dpif_linux_init();
214 if (error) {
215 return error;
216 }
217
982b8810
BP
218 /* Create or look up datapath. */
219 dpif_linux_dp_init(&dp_request);
ea36840f
BP
220 if (create) {
221 dp_request.cmd = OVS_DP_CMD_NEW;
222 upcall_pid = 0;
223 dp_request.upcall_pid = &upcall_pid;
224 } else {
225 dp_request.cmd = OVS_DP_CMD_GET;
226 }
254f2dc8 227 dp_request.name = name;
982b8810
BP
228 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
229 if (error) {
230 return error;
c19e6535 231 }
254f2dc8 232
8f4a4df5
EJ
233 open_dpif(&dp, dpifp);
234 ofpbuf_delete(buf);
235 return 0;
c19e6535
BP
236}
237
8f4a4df5 238static void
254f2dc8 239open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
c19e6535 240{
c19e6535 241 struct dpif_linux *dpif;
c19e6535 242
17411ecf 243 dpif = xzalloc(sizeof *dpif);
2ee6545f
EJ
244 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
245 dpif);
50f80534 246 dpif->epoll_fd = -1;
c19e6535 247
254f2dc8
BP
248 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
249 dp->dp_ifindex, dp->dp_ifindex);
c19e6535 250
254f2dc8 251 dpif->dp_ifindex = dp->dp_ifindex;
b3c01ed3 252 sset_init(&dpif->changed_ports);
c19e6535 253 *dpifp = &dpif->dpif;
96fba48f
BP
254}
255
17411ecf
JG
256static void
257destroy_upcall_socks(struct dpif_linux *dpif)
258{
259 int i;
260
50f80534
BP
261 if (dpif->epoll_fd >= 0) {
262 close(dpif->epoll_fd);
263 dpif->epoll_fd = -1;
264 }
17411ecf
JG
265 for (i = 0; i < N_UPCALL_SOCKS; i++) {
266 nl_sock_destroy(dpif->upcall_socks[i]);
267 dpif->upcall_socks[i] = NULL;
268 }
269}
270
96fba48f
BP
271static void
272dpif_linux_close(struct dpif *dpif_)
273{
274 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
c7178a0b 275
2ee6545f 276 nln_notifier_destroy(dpif->port_notifier);
17411ecf 277 destroy_upcall_socks(dpif);
b3c01ed3 278 sset_destroy(&dpif->changed_ports);
96fba48f
BP
279 free(dpif);
280}
281
282static int
7dab847a 283dpif_linux_destroy(struct dpif *dpif_)
96fba48f 284{
d6569377
BP
285 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
286 struct dpif_linux_dp dp;
287
288 dpif_linux_dp_init(&dp);
df2c07f4 289 dp.cmd = OVS_DP_CMD_DEL;
254f2dc8 290 dp.dp_ifindex = dpif->dp_ifindex;
d6569377 291 return dpif_linux_dp_transact(&dp, NULL, NULL);
96fba48f
BP
292}
293
d8abf60c
EJ
294static void
295dpif_linux_run(struct dpif *dpif OVS_UNUSED)
296{
c7178a0b 297 if (nln) {
18a23781 298 nln_run(nln);
c7178a0b 299 }
d8abf60c
EJ
300}
301
302static void
303dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
304{
c7178a0b 305 if (nln) {
18a23781 306 nln_wait(nln);
c7178a0b 307 }
d8abf60c
EJ
308}
309
96fba48f 310static int
a8d9304d 311dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
96fba48f 312{
d6569377
BP
313 struct dpif_linux_dp dp;
314 struct ofpbuf *buf;
315 int error;
316
317 error = dpif_linux_dp_get(dpif_, &dp, &buf);
318 if (!error) {
a8d9304d
BP
319 stats->n_hit = dp.stats.n_hit;
320 stats->n_missed = dp.stats.n_missed;
321 stats->n_lost = dp.stats.n_lost;
322 stats->n_flows = dp.stats.n_flows;
d6569377
BP
323 ofpbuf_delete(buf);
324 }
325 return error;
96fba48f
BP
326}
327
96fba48f 328static int
c19e6535 329dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
c3827f61 330 uint16_t *port_nop)
96fba48f 331{
c19e6535 332 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
c3827f61
BP
333 const char *name = netdev_get_name(netdev);
334 const char *type = netdev_get_type(netdev);
c19e6535
BP
335 struct dpif_linux_vport request, reply;
336 const struct ofpbuf *options;
337 struct ofpbuf *buf;
95b1d73a 338 int error, i = 0, max_ports = MAX_PORTS;
96fba48f 339
c19e6535 340 dpif_linux_vport_init(&request);
df2c07f4 341 request.cmd = OVS_VPORT_CMD_NEW;
254f2dc8 342 request.dp_ifindex = dpif->dp_ifindex;
c19e6535 343 request.type = netdev_vport_get_vport_type(netdev);
df2c07f4 344 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
c283069c
BP
345 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
346 "unsupported type `%s'",
c19e6535 347 dpif_name(dpif_), name, type);
c283069c
BP
348 return EINVAL;
349 }
c19e6535 350 request.name = name;
c3827f61 351
c19e6535
BP
352 options = netdev_vport_get_options(netdev);
353 if (options && options->size) {
354 request.options = options->data;
355 request.options_len = options->size;
356 }
357
24b019f8
JP
358 if (request.type == OVS_VPORT_TYPE_NETDEV) {
359 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
360 }
361
773cd538
EJ
362 /* Loop until we find a port that isn't used. */
363 do {
a24a6574
BP
364 uint32_t upcall_pid;
365
95b1d73a 366 request.port_no = ++dpif->alloc_port_no;
a12b3ead 367 upcall_pid = dpif_linux_port_get_pid(dpif_, request.port_no);
a24a6574 368 request.upcall_pid = &upcall_pid;
773cd538
EJ
369 error = dpif_linux_vport_transact(&request, &reply, &buf);
370
371 if (!error) {
372 *port_nop = reply.port_no;
a24a6574
BP
373 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
374 dpif_name(dpif_), request.port_no, upcall_pid);
95b1d73a
PS
375 } else if (error == EFBIG) {
376 /* Older datapath has lower limit. */
377 max_ports = dpif->alloc_port_no;
378 dpif->alloc_port_no = 0;
773cd538 379 }
95b1d73a 380
c19e6535 381 ofpbuf_delete(buf);
95b1d73a 382 } while ((i++ < max_ports)
773cd538 383 && (error == EBUSY || error == EFBIG));
c3827f61 384
96fba48f
BP
385 return error;
386}
387
388static int
c19e6535 389dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
96fba48f 390{
c19e6535
BP
391 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
392 struct dpif_linux_vport vport;
773cd538 393 int error;
c19e6535
BP
394
395 dpif_linux_vport_init(&vport);
df2c07f4 396 vport.cmd = OVS_VPORT_CMD_DEL;
254f2dc8 397 vport.dp_ifindex = dpif->dp_ifindex;
c19e6535 398 vport.port_no = port_no;
773cd538
EJ
399 error = dpif_linux_vport_transact(&vport, NULL, NULL);
400
773cd538 401 return error;
c3827f61 402}
3abc4a1a 403
c3827f61 404static int
4c738a8d
BP
405dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
406 const char *port_name, struct dpif_port *dpif_port)
c3827f61 407{
c19e6535
BP
408 struct dpif_linux_vport request;
409 struct dpif_linux_vport reply;
410 struct ofpbuf *buf;
4c738a8d
BP
411 int error;
412
c19e6535 413 dpif_linux_vport_init(&request);
df2c07f4 414 request.cmd = OVS_VPORT_CMD_GET;
254f2dc8 415 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
c19e6535
BP
416 request.port_no = port_no;
417 request.name = port_name;
4c738a8d 418
c19e6535
BP
419 error = dpif_linux_vport_transact(&request, &reply, &buf);
420 if (!error) {
33db1592
BP
421 if (reply.dp_ifindex != request.dp_ifindex) {
422 /* A query by name reported that 'port_name' is in some datapath
423 * other than 'dpif', but the caller wants to know about 'dpif'. */
424 error = ENODEV;
425 } else {
426 dpif_port->name = xstrdup(reply.name);
427 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
428 dpif_port->port_no = reply.port_no;
429 }
c19e6535 430 ofpbuf_delete(buf);
3abc4a1a 431 }
c19e6535 432 return error;
96fba48f
BP
433}
434
435static int
c3827f61 436dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
4c738a8d 437 struct dpif_port *dpif_port)
96fba48f 438{
c19e6535 439 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
96fba48f
BP
440}
441
442static int
4c738a8d
BP
443dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
444 struct dpif_port *dpif_port)
96fba48f 445{
4c738a8d 446 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
96fba48f
BP
447}
448
996c1b3d
BP
449static int
450dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
451{
95b1d73a 452 return MAX_PORTS;
996c1b3d
BP
453}
454
98403001 455static uint32_t
a12b3ead 456dpif_linux_port_get_pid(const struct dpif *dpif_, uint16_t port_no)
98403001
BP
457{
458 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
459
a12b3ead 460 if (dpif->epoll_fd < 0) {
98403001
BP
461 return 0;
462 } else {
463 int idx = port_no & (N_UPCALL_SOCKS - 1);
464 return nl_sock_pid(dpif->upcall_socks[idx]);
465 }
466}
467
96fba48f
BP
468static int
469dpif_linux_flow_flush(struct dpif *dpif_)
470{
9c52546b 471 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
37a1300c
BP
472 struct dpif_linux_flow flow;
473
474 dpif_linux_flow_init(&flow);
df2c07f4 475 flow.cmd = OVS_FLOW_CMD_DEL;
254f2dc8 476 flow.dp_ifindex = dpif->dp_ifindex;
37a1300c 477 return dpif_linux_flow_transact(&flow, NULL, NULL);
96fba48f
BP
478}
479
c19e6535 480struct dpif_linux_port_state {
f0fef760 481 struct nl_dump dump;
c19e6535
BP
482};
483
96fba48f 484static int
f0fef760 485dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
96fba48f 486{
f0fef760
BP
487 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
488 struct dpif_linux_port_state *state;
489 struct dpif_linux_vport request;
490 struct ofpbuf *buf;
491
492 *statep = state = xmalloc(sizeof *state);
493
494 dpif_linux_vport_init(&request);
df2c07f4 495 request.cmd = OVS_DP_CMD_GET;
254f2dc8 496 request.dp_ifindex = dpif->dp_ifindex;
f0fef760
BP
497
498 buf = ofpbuf_new(1024);
499 dpif_linux_vport_to_ofpbuf(&request, buf);
500 nl_dump_start(&state->dump, genl_sock, buf);
501 ofpbuf_delete(buf);
502
b0ec0f27
BP
503 return 0;
504}
505
506static int
f0fef760 507dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
4c738a8d 508 struct dpif_port *dpif_port)
b0ec0f27 509{
c19e6535 510 struct dpif_linux_port_state *state = state_;
f0fef760
BP
511 struct dpif_linux_vport vport;
512 struct ofpbuf buf;
96fba48f
BP
513 int error;
514
f0fef760
BP
515 if (!nl_dump_next(&state->dump, &buf)) {
516 return EOF;
517 }
c19e6535 518
f0fef760 519 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
c3827f61 520 if (error) {
f0fef760 521 return error;
c3827f61 522 }
f0fef760
BP
523
524 dpif_port->name = (char *) vport.name;
525 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
526 dpif_port->port_no = vport.port_no;
527 return 0;
b0ec0f27
BP
528}
529
530static int
95b1d73a 531dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
b0ec0f27 532{
c19e6535 533 struct dpif_linux_port_state *state = state_;
f0fef760 534 int error = nl_dump_done(&state->dump);
8522b383 535
b0ec0f27 536 free(state);
f0fef760 537 return error;
96fba48f
BP
538}
539
e9e28be3
BP
540static int
541dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
542{
543 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
e9e28be3 544
8b61709d
BP
545 if (dpif->change_error) {
546 dpif->change_error = false;
b3c01ed3 547 sset_clear(&dpif->changed_ports);
8b61709d 548 return ENOBUFS;
b3c01ed3
BP
549 } else if (!sset_is_empty(&dpif->changed_ports)) {
550 *devnamep = sset_pop(&dpif->changed_ports);
8b61709d 551 return 0;
e9e28be3 552 } else {
8b61709d 553 return EAGAIN;
e9e28be3 554 }
e9e28be3
BP
555}
556
557static void
558dpif_linux_port_poll_wait(const struct dpif *dpif_)
559{
560 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
b3c01ed3 561 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
e9e28be3 562 poll_immediate_wake();
e9e28be3
BP
563 }
564}
565
96fba48f 566static int
30053024
BP
567dpif_linux_flow_get__(const struct dpif *dpif_,
568 const struct nlattr *key, size_t key_len,
569 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
96fba48f 570{
9c52546b 571 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
30053024 572 struct dpif_linux_flow request;
feebdea2 573
d6569377 574 dpif_linux_flow_init(&request);
df2c07f4 575 request.cmd = OVS_FLOW_CMD_GET;
254f2dc8 576 request.dp_ifindex = dpif->dp_ifindex;
d6569377
BP
577 request.key = key;
578 request.key_len = key_len;
30053024
BP
579 return dpif_linux_flow_transact(&request, reply, bufp);
580}
581
582static int
583dpif_linux_flow_get(const struct dpif *dpif_,
584 const struct nlattr *key, size_t key_len,
585 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
586{
587 struct dpif_linux_flow reply;
588 struct ofpbuf *buf;
589 int error;
590
591 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
feebdea2
BP
592 if (!error) {
593 if (stats) {
d6569377 594 dpif_linux_flow_get_stats(&reply, stats);
feebdea2 595 }
d6569377
BP
596 if (actionsp) {
597 buf->data = (void *) reply.actions;
598 buf->size = reply.actions_len;
599 *actionsp = buf;
600 } else {
601 ofpbuf_delete(buf);
feebdea2
BP
602 }
603 }
604 return error;
96fba48f
BP
605}
606
6bc60024 607static void
89625d1e 608dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
6bc60024
BP
609 struct dpif_linux_flow *request)
610{
611 static struct nlattr dummy_action;
612
613 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
614
615 dpif_linux_flow_init(request);
89625d1e 616 request->cmd = (put->flags & DPIF_FP_CREATE
6bc60024
BP
617 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
618 request->dp_ifindex = dpif->dp_ifindex;
89625d1e
BP
619 request->key = put->key;
620 request->key_len = put->key_len;
6bc60024 621 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
89625d1e
BP
622 request->actions = put->actions ? put->actions : &dummy_action;
623 request->actions_len = put->actions_len;
624 if (put->flags & DPIF_FP_ZERO_STATS) {
6bc60024
BP
625 request->clear = true;
626 }
89625d1e 627 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
6bc60024
BP
628}
629
96fba48f 630static int
89625d1e 631dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
96fba48f 632{
d6569377
BP
633 struct dpif_linux_flow request, reply;
634 struct ofpbuf *buf;
feebdea2
BP
635 int error;
636
89625d1e 637 dpif_linux_init_flow_put(dpif_, put, &request);
d6569377 638 error = dpif_linux_flow_transact(&request,
89625d1e
BP
639 put->stats ? &reply : NULL,
640 put->stats ? &buf : NULL);
641 if (!error && put->stats) {
642 dpif_linux_flow_get_stats(&reply, put->stats);
d6569377 643 ofpbuf_delete(buf);
feebdea2
BP
644 }
645 return error;
96fba48f
BP
646}
647
b99d3cee
BP
648static void
649dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
650 struct dpif_linux_flow *request)
96fba48f 651{
9c52546b 652 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
b99d3cee
BP
653
654 dpif_linux_flow_init(request);
655 request->cmd = OVS_FLOW_CMD_DEL;
656 request->dp_ifindex = dpif->dp_ifindex;
657 request->key = del->key;
658 request->key_len = del->key_len;
659}
660
661static int
662dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
663{
d6569377
BP
664 struct dpif_linux_flow request, reply;
665 struct ofpbuf *buf;
feebdea2
BP
666 int error;
667
b99d3cee 668 dpif_linux_init_flow_del(dpif_, del, &request);
d6569377 669 error = dpif_linux_flow_transact(&request,
b99d3cee
BP
670 del->stats ? &reply : NULL,
671 del->stats ? &buf : NULL);
672 if (!error && del->stats) {
673 dpif_linux_flow_get_stats(&reply, del->stats);
d6569377 674 ofpbuf_delete(buf);
feebdea2
BP
675 }
676 return error;
96fba48f
BP
677}
678
feebdea2 679struct dpif_linux_flow_state {
37a1300c 680 struct nl_dump dump;
d6569377 681 struct dpif_linux_flow flow;
c97fb132 682 struct dpif_flow_stats stats;
30053024 683 struct ofpbuf *buf;
feebdea2
BP
684};
685
96fba48f 686static int
37a1300c 687dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
96fba48f 688{
37a1300c
BP
689 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
690 struct dpif_linux_flow_state *state;
691 struct dpif_linux_flow request;
692 struct ofpbuf *buf;
693
694 *statep = state = xmalloc(sizeof *state);
695
696 dpif_linux_flow_init(&request);
df2c07f4 697 request.cmd = OVS_DP_CMD_GET;
254f2dc8 698 request.dp_ifindex = dpif->dp_ifindex;
37a1300c
BP
699
700 buf = ofpbuf_new(1024);
701 dpif_linux_flow_to_ofpbuf(&request, buf);
702 nl_dump_start(&state->dump, genl_sock, buf);
703 ofpbuf_delete(buf);
704
30053024
BP
705 state->buf = NULL;
706
704a1e09
BP
707 return 0;
708}
709
710static int
37a1300c 711dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
feebdea2
BP
712 const struct nlattr **key, size_t *key_len,
713 const struct nlattr **actions, size_t *actions_len,
c97fb132 714 const struct dpif_flow_stats **stats)
704a1e09 715{
feebdea2 716 struct dpif_linux_flow_state *state = state_;
37a1300c 717 struct ofpbuf buf;
96fba48f
BP
718 int error;
719
30053024
BP
720 do {
721 ofpbuf_delete(state->buf);
722 state->buf = NULL;
feebdea2 723
30053024
BP
724 if (!nl_dump_next(&state->dump, &buf)) {
725 return EOF;
feebdea2 726 }
30053024
BP
727
728 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
729 if (error) {
730 return error;
feebdea2 731 }
30053024
BP
732
733 if (actions && !state->flow.actions) {
734 error = dpif_linux_flow_get__(dpif_, state->flow.key,
735 state->flow.key_len,
736 &state->flow, &state->buf);
737 if (error == ENOENT) {
738 VLOG_DBG("dumped flow disappeared on get");
739 } else if (error) {
740 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
741 }
feebdea2 742 }
30053024
BP
743 } while (error);
744
745 if (actions) {
746 *actions = state->flow.actions;
747 *actions_len = state->flow.actions_len;
748 }
749 if (key) {
750 *key = state->flow.key;
751 *key_len = state->flow.key_len;
752 }
753 if (stats) {
754 dpif_linux_flow_get_stats(&state->flow, &state->stats);
755 *stats = &state->stats;
feebdea2 756 }
37a1300c 757 return error;
704a1e09
BP
758}
759
760static int
d6569377 761dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
704a1e09 762{
d6569377 763 struct dpif_linux_flow_state *state = state_;
37a1300c 764 int error = nl_dump_done(&state->dump);
30053024 765 ofpbuf_delete(state->buf);
704a1e09 766 free(state);
37a1300c 767 return error;
96fba48f
BP
768}
769
eabe7c68
BP
770static void
771dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
772 struct ofpbuf *buf)
96fba48f 773{
89625d1e 774 struct ovs_header *k_exec;
f7cd0081 775
eabe7c68
BP
776 ofpbuf_prealloc_tailroom(buf, (64
777 + d_exec->packet->size
778 + d_exec->key_len
779 + d_exec->actions_len));
f7cd0081 780
df2c07f4 781 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
69685a88 782 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
f7cd0081 783
89625d1e
BP
784 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
785 k_exec->dp_ifindex = dp_ifindex;
f7cd0081 786
89625d1e
BP
787 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
788 d_exec->packet->data, d_exec->packet->size);
789 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
790 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
791 d_exec->actions, d_exec->actions_len);
6bc60024
BP
792}
793
794static int
89625d1e 795dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
6bc60024 796{
eabe7c68
BP
797 uint64_t request_stub[1024 / 8];
798 struct ofpbuf request;
6bc60024
BP
799 int error;
800
eabe7c68
BP
801 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
802 dpif_linux_encode_execute(dp_ifindex, execute, &request);
803 error = nl_sock_transact(genl_sock, &request, NULL);
804 ofpbuf_uninit(&request);
6bc60024 805
f7cd0081 806 return error;
96fba48f
BP
807}
808
eb8b28e7 809static int
89625d1e 810dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
eb8b28e7
EJ
811{
812 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
813
89625d1e 814 return dpif_linux_execute__(dpif->dp_ifindex, execute);
eb8b28e7
EJ
815}
816
eabe7c68
BP
817#define MAX_OPS 50
818
6bc60024 819static void
eabe7c68 820dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
6bc60024
BP
821{
822 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
eabe7c68
BP
823
824 struct op_auxdata {
825 struct nl_transaction txn;
72d32ac0 826
eabe7c68
BP
827 struct ofpbuf request;
828 uint64_t request_stub[1024 / 8];
72d32ac0
BP
829
830 struct ofpbuf reply;
831 uint64_t reply_stub[1024 / 8];
eabe7c68
BP
832 } auxes[MAX_OPS];
833
834 struct nl_transaction *txnsp[MAX_OPS];
6bc60024
BP
835 size_t i;
836
eabe7c68 837 assert(n_ops <= MAX_OPS);
6bc60024 838 for (i = 0; i < n_ops; i++) {
eabe7c68 839 struct op_auxdata *aux = &auxes[i];
c2b565b5 840 struct dpif_op *op = ops[i];
b99d3cee
BP
841 struct dpif_flow_put *put;
842 struct dpif_flow_del *del;
843 struct dpif_execute *execute;
eabe7c68
BP
844 struct dpif_linux_flow flow;
845
846 ofpbuf_use_stub(&aux->request,
847 aux->request_stub, sizeof aux->request_stub);
848 aux->txn.request = &aux->request;
b99d3cee 849
72d32ac0
BP
850 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
851 aux->txn.reply = NULL;
852
b99d3cee
BP
853 switch (op->type) {
854 case DPIF_OP_FLOW_PUT:
855 put = &op->u.flow_put;
eabe7c68 856 dpif_linux_init_flow_put(dpif_, put, &flow);
6bc60024 857 if (put->stats) {
eabe7c68 858 flow.nlmsg_flags |= NLM_F_ECHO;
72d32ac0 859 aux->txn.reply = &aux->reply;
6bc60024 860 }
eabe7c68 861 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
b99d3cee
BP
862 break;
863
864 case DPIF_OP_FLOW_DEL:
865 del = &op->u.flow_del;
eabe7c68 866 dpif_linux_init_flow_del(dpif_, del, &flow);
b99d3cee 867 if (del->stats) {
eabe7c68 868 flow.nlmsg_flags |= NLM_F_ECHO;
72d32ac0 869 aux->txn.reply = &aux->reply;
b99d3cee 870 }
eabe7c68 871 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
b99d3cee 872 break;
6bc60024 873
b99d3cee
BP
874 case DPIF_OP_EXECUTE:
875 execute = &op->u.execute;
eabe7c68
BP
876 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
877 &aux->request);
b99d3cee
BP
878 break;
879
880 default:
6bc60024
BP
881 NOT_REACHED();
882 }
883 }
884
6bc60024 885 for (i = 0; i < n_ops; i++) {
eabe7c68 886 txnsp[i] = &auxes[i].txn;
6bc60024 887 }
6bc60024
BP
888 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
889
6bc60024 890 for (i = 0; i < n_ops; i++) {
72d32ac0 891 struct op_auxdata *aux = &auxes[i];
eabe7c68 892 struct nl_transaction *txn = &auxes[i].txn;
c2b565b5 893 struct dpif_op *op = ops[i];
b99d3cee
BP
894 struct dpif_flow_put *put;
895 struct dpif_flow_del *del;
6bc60024 896
b99d3cee 897 op->error = txn->error;
6bc60024 898
b99d3cee
BP
899 switch (op->type) {
900 case DPIF_OP_FLOW_PUT:
901 put = &op->u.flow_put;
902 if (!op->error && put->stats) {
6bc60024
BP
903 struct dpif_linux_flow reply;
904
b99d3cee
BP
905 op->error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
906 if (!op->error) {
6bc60024
BP
907 dpif_linux_flow_get_stats(&reply, put->stats);
908 }
909 }
b99d3cee
BP
910 break;
911
912 case DPIF_OP_FLOW_DEL:
913 del = &op->u.flow_del;
914 if (!op->error && del->stats) {
915 struct dpif_linux_flow reply;
916
917 op->error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
918 if (!op->error) {
919 dpif_linux_flow_get_stats(&reply, del->stats);
920 }
921 }
922 break;
923
924 case DPIF_OP_EXECUTE:
925 break;
926
927 default:
6bc60024
BP
928 NOT_REACHED();
929 }
930
72d32ac0
BP
931 ofpbuf_uninit(&aux->request);
932 ofpbuf_uninit(&aux->reply);
6bc60024 933 }
eabe7c68
BP
934}
935
936static void
937dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
938{
939 while (n_ops > 0) {
940 size_t chunk = MIN(n_ops, MAX_OPS);
941 dpif_linux_operate__(dpif, ops, chunk);
942 ops += chunk;
943 n_ops -= chunk;
944 }
6bc60024
BP
945}
946
17411ecf 947static void
98403001 948set_upcall_pids(struct dpif *dpif_)
17411ecf 949{
98403001 950 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
17411ecf 951 struct dpif_port_dump port_dump;
98403001 952 struct dpif_port port;
17411ecf
JG
953 int error;
954
955 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
a12b3ead 956 uint32_t upcall_pid = dpif_linux_port_get_pid(dpif_, port.port_no);
17411ecf
JG
957 struct dpif_linux_vport vport_request;
958
959 dpif_linux_vport_init(&vport_request);
960 vport_request.cmd = OVS_VPORT_CMD_SET;
961 vport_request.dp_ifindex = dpif->dp_ifindex;
962 vport_request.port_no = port.port_no;
a24a6574 963 vport_request.upcall_pid = &upcall_pid;
17411ecf
JG
964 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
965 if (!error) {
a24a6574 966 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
17411ecf 967 dpif_name(&dpif->dpif), vport_request.port_no,
a24a6574 968 upcall_pid);
17411ecf
JG
969 } else {
970 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
971 dpif_name(&dpif->dpif), strerror(error));
972 }
973 }
17411ecf
JG
974}
975
96fba48f 976static int
a12b3ead 977dpif_linux_recv_set(struct dpif *dpif_, bool enable)
96fba48f 978{
982b8810 979 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
982b8810 980
a12b3ead 981 if ((dpif->epoll_fd >= 0) == enable) {
982b8810 982 return 0;
17411ecf 983 }
b063d9f0 984
a12b3ead 985 if (!enable) {
17411ecf 986 destroy_upcall_socks(dpif);
a12b3ead 987 } else {
17411ecf
JG
988 int i;
989 int error;
982b8810 990
50f80534
BP
991 dpif->epoll_fd = epoll_create(N_UPCALL_SOCKS);
992 if (dpif->epoll_fd < 0) {
993 return errno;
994 }
995
17411ecf 996 for (i = 0; i < N_UPCALL_SOCKS; i++) {
50f80534
BP
997 struct epoll_event event;
998
17411ecf 999 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
b063d9f0 1000 if (error) {
17411ecf
JG
1001 destroy_upcall_socks(dpif);
1002 return error;
982b8810 1003 }
50f80534 1004
733c8d13 1005 memset(&event, 0, sizeof event);
50f80534
BP
1006 event.events = EPOLLIN;
1007 event.data.u32 = i;
1008 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD,
1009 nl_sock_fd(dpif->upcall_socks[i]), &event) < 0) {
1010 error = errno;
1011 destroy_upcall_socks(dpif);
1012 return error;
1013 }
982b8810 1014 }
8522ba09
BP
1015
1016 dpif->ready_mask = 0;
982b8810 1017 }
b063d9f0 1018
98403001 1019 set_upcall_pids(dpif_);
17411ecf 1020
b063d9f0 1021 return 0;
96fba48f
BP
1022}
1023
aae51f53
BP
1024static int
1025dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1026 uint32_t queue_id, uint32_t *priority)
1027{
1028 if (queue_id < 0xf000) {
17ee3c1f 1029 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
aae51f53
BP
1030 return 0;
1031 } else {
1032 return EINVAL;
1033 }
1034}
1035
96fba48f 1036static int
982b8810 1037parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
254f2dc8 1038 int *dp_ifindex)
856081f6 1039{
df2c07f4 1040 static const struct nl_policy ovs_packet_policy[] = {
856081f6 1041 /* Always present. */
df2c07f4 1042 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
856081f6 1043 .min_len = ETH_HEADER_LEN },
df2c07f4 1044 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
856081f6 1045
df2c07f4
JP
1046 /* OVS_PACKET_CMD_ACTION only. */
1047 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
856081f6
BP
1048 };
1049
df2c07f4
JP
1050 struct ovs_header *ovs_header;
1051 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
982b8810
BP
1052 struct nlmsghdr *nlmsg;
1053 struct genlmsghdr *genl;
1054 struct ofpbuf b;
aaff4b55 1055 int type;
982b8810
BP
1056
1057 ofpbuf_use_const(&b, buf->data, buf->size);
1058
1059 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1060 genl = ofpbuf_try_pull(&b, sizeof *genl);
df2c07f4
JP
1061 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1062 if (!nlmsg || !genl || !ovs_header
1063 || nlmsg->nlmsg_type != ovs_packet_family
1064 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1065 ARRAY_SIZE(ovs_packet_policy))) {
856081f6
BP
1066 return EINVAL;
1067 }
1068
df2c07f4
JP
1069 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1070 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
aaff4b55
BP
1071 : -1);
1072 if (type < 0) {
1073 return EINVAL;
1074 }
82272ede 1075
aaff4b55
BP
1076 memset(upcall, 0, sizeof *upcall);
1077 upcall->type = type;
856081f6 1078 upcall->packet = buf;
df2c07f4
JP
1079 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1080 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1081 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1082 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1083 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1084 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
856081f6 1085 : 0);
df2c07f4 1086 *dp_ifindex = ovs_header->dp_ifindex;
982b8810 1087
856081f6
BP
1088 return 0;
1089}
1090
1091static int
90a7c55e
BP
1092dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1093 struct ofpbuf *buf)
96fba48f
BP
1094{
1095 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
17411ecf 1096 int read_tries = 0;
96fba48f 1097
a12b3ead 1098 if (dpif->epoll_fd < 0) {
17411ecf 1099 return EAGAIN;
982b8810
BP
1100 }
1101
8522ba09 1102 if (!dpif->ready_mask) {
50f80534 1103 struct epoll_event events[N_UPCALL_SOCKS];
8522ba09
BP
1104 int retval;
1105 int i;
f6d1465c 1106
8522ba09 1107 do {
50f80534 1108 retval = epoll_wait(dpif->epoll_fd, events, N_UPCALL_SOCKS, 0);
8522ba09
BP
1109 } while (retval < 0 && errno == EINTR);
1110 if (retval < 0) {
1111 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
50f80534 1112 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
8522ba09
BP
1113 }
1114
50f80534
BP
1115 for (i = 0; i < retval; i++) {
1116 dpif->ready_mask |= 1u << events[i].data.u32;
8522ba09 1117 }
8522ba09
BP
1118 }
1119
50f80534 1120 while (dpif->ready_mask) {
8522ba09
BP
1121 int indx = ffs(dpif->ready_mask) - 1;
1122 struct nl_sock *upcall_sock = dpif->upcall_socks[indx];
1123
1124 dpif->ready_mask &= ~(1u << indx);
17411ecf 1125
f6d1465c 1126 for (;;) {
8522ba09 1127 int dp_ifindex;
f6d1465c 1128 int error;
17411ecf 1129
f6d1465c
BP
1130 if (++read_tries > 50) {
1131 return EAGAIN;
1132 }
17411ecf 1133
72d32ac0
BP
1134 error = nl_sock_recv(upcall_sock, buf, false);
1135 if (error) {
72d32ac0
BP
1136 if (error == EAGAIN) {
1137 break;
1138 }
f6d1465c
BP
1139 return error;
1140 }
17411ecf 1141
f6d1465c 1142 error = parse_odp_packet(buf, upcall, &dp_ifindex);
a12b3ead 1143 if (!error && dp_ifindex == dpif->dp_ifindex) {
f6d1465c
BP
1144 return 0;
1145 }
f6d1465c
BP
1146 if (error) {
1147 return error;
17411ecf 1148 }
982b8810 1149 }
50f80534 1150 }
982b8810
BP
1151
1152 return EAGAIN;
96fba48f
BP
1153}
1154
1155static void
1156dpif_linux_recv_wait(struct dpif *dpif_)
1157{
1158 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
17411ecf 1159
a12b3ead 1160 if (dpif->epoll_fd < 0) {
17411ecf
JG
1161 return;
1162 }
1163
50f80534 1164 poll_fd_wait(dpif->epoll_fd, POLLIN);
96fba48f
BP
1165}
1166
1ba530f4
BP
1167static void
1168dpif_linux_recv_purge(struct dpif *dpif_)
1169{
1170 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
17411ecf
JG
1171 int i;
1172
a12b3ead 1173 if (dpif->epoll_fd < 0) {
17411ecf
JG
1174 return;
1175 }
1ba530f4 1176
17411ecf
JG
1177 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1178 nl_sock_drain(dpif->upcall_socks[i]);
1ba530f4
BP
1179 }
1180}
1181
96fba48f 1182const struct dpif_class dpif_linux_class = {
1a6f1e2a 1183 "system",
d3d22744 1184 dpif_linux_enumerate,
96fba48f
BP
1185 dpif_linux_open,
1186 dpif_linux_close,
7dab847a 1187 dpif_linux_destroy,
d8abf60c
EJ
1188 dpif_linux_run,
1189 dpif_linux_wait,
96fba48f 1190 dpif_linux_get_stats,
96fba48f
BP
1191 dpif_linux_port_add,
1192 dpif_linux_port_del,
1193 dpif_linux_port_query_by_number,
1194 dpif_linux_port_query_by_name,
996c1b3d 1195 dpif_linux_get_max_ports,
98403001 1196 dpif_linux_port_get_pid,
b0ec0f27
BP
1197 dpif_linux_port_dump_start,
1198 dpif_linux_port_dump_next,
1199 dpif_linux_port_dump_done,
e9e28be3
BP
1200 dpif_linux_port_poll,
1201 dpif_linux_port_poll_wait,
96fba48f
BP
1202 dpif_linux_flow_get,
1203 dpif_linux_flow_put,
1204 dpif_linux_flow_del,
1205 dpif_linux_flow_flush,
704a1e09
BP
1206 dpif_linux_flow_dump_start,
1207 dpif_linux_flow_dump_next,
1208 dpif_linux_flow_dump_done,
96fba48f 1209 dpif_linux_execute,
6bc60024 1210 dpif_linux_operate,
a12b3ead 1211 dpif_linux_recv_set,
aae51f53 1212 dpif_linux_queue_to_priority,
96fba48f
BP
1213 dpif_linux_recv,
1214 dpif_linux_recv_wait,
1ba530f4 1215 dpif_linux_recv_purge,
96fba48f
BP
1216};
1217\f
96fba48f 1218static int
982b8810 1219dpif_linux_init(void)
96fba48f 1220{
982b8810
BP
1221 static int error = -1;
1222
1223 if (error < 0) {
c7178a0b
EJ
1224 unsigned int ovs_vport_mcgroup;
1225
df2c07f4
JP
1226 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1227 &ovs_datapath_family);
37a1300c
BP
1228 if (error) {
1229 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1230 "The Open vSwitch kernel module is probably not loaded.",
df2c07f4 1231 OVS_DATAPATH_FAMILY);
37a1300c 1232 }
f0fef760 1233 if (!error) {
df2c07f4 1234 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
f0fef760 1235 }
37a1300c 1236 if (!error) {
df2c07f4 1237 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
37a1300c 1238 }
aaff4b55 1239 if (!error) {
df2c07f4
JP
1240 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1241 &ovs_packet_family);
aaff4b55 1242 }
982b8810
BP
1243 if (!error) {
1244 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1245 }
c7178a0b
EJ
1246 if (!error) {
1247 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
213a13ed
EJ
1248 &ovs_vport_mcgroup,
1249 OVS_VPORT_MCGROUP_FALLBACK_ID);
c7178a0b
EJ
1250 }
1251 if (!error) {
1252 static struct dpif_linux_vport vport;
1253 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1254 dpif_linux_nln_parse, &vport);
1255 }
982b8810
BP
1256 }
1257
1258 return error;
96fba48f
BP
1259}
1260
c19e6535
BP
1261bool
1262dpif_linux_is_internal_device(const char *name)
9fe3b9a2 1263{
c19e6535
BP
1264 struct dpif_linux_vport reply;
1265 struct ofpbuf *buf;
9fe3b9a2 1266 int error;
96fba48f 1267
c19e6535
BP
1268 error = dpif_linux_vport_get(name, &reply, &buf);
1269 if (!error) {
1270 ofpbuf_delete(buf);
141d9ce4 1271 } else if (error != ENODEV && error != ENOENT) {
c19e6535
BP
1272 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1273 name, strerror(error));
96fba48f
BP
1274 }
1275
df2c07f4 1276 return reply.type == OVS_VPORT_TYPE_INTERNAL;
96fba48f
BP
1277}
1278
7feba1ac
BP
1279int
1280dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1281 const void *data, size_t size)
1282{
eb8b28e7
EJ
1283 struct ofpbuf actions, key, packet;
1284 struct odputil_keybuf keybuf;
89625d1e 1285 struct dpif_execute execute;
eb8b28e7
EJ
1286 struct flow flow;
1287 uint64_t action;
7feba1ac 1288
eb8b28e7 1289 ofpbuf_use_const(&packet, data, size);
abff858b 1290 flow_extract(&packet, 0, htonll(0), 0, &flow);
7feba1ac 1291
eb8b28e7
EJ
1292 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1293 odp_flow_key_from_flow(&key, &flow);
7feba1ac 1294
eb8b28e7 1295 ofpbuf_use_stack(&actions, &action, sizeof action);
df2c07f4 1296 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
7feba1ac 1297
89625d1e
BP
1298 execute.key = key.data;
1299 execute.key_len = key.size;
1300 execute.actions = actions.data;
1301 execute.actions_len = actions.size;
1302 execute.packet = &packet;
1303 return dpif_linux_execute__(dp_ifindex, &execute);
7feba1ac
BP
1304}
1305
c7178a0b
EJ
1306static bool
1307dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1308{
1309 struct dpif_linux_vport *vport = vport_;
1310 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1311}
1312
e9e28be3 1313static void
c7178a0b 1314dpif_linux_port_changed(const void *vport_, void *dpif_)
e9e28be3 1315{
c7178a0b 1316 const struct dpif_linux_vport *vport = vport_;
e9e28be3
BP
1317 struct dpif_linux *dpif = dpif_;
1318
c7178a0b
EJ
1319 if (vport) {
1320 if (vport->dp_ifindex == dpif->dp_ifindex
1321 && (vport->cmd == OVS_VPORT_CMD_NEW
1322 || vport->cmd == OVS_VPORT_CMD_DEL
1323 || vport->cmd == OVS_VPORT_CMD_SET)) {
1324 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1325 dpif->dpif.full_name, vport->name, vport->cmd);
1326 sset_add(&dpif->changed_ports, vport->name);
e9e28be3 1327 }
8b61709d
BP
1328 } else {
1329 dpif->change_error = true;
e9e28be3
BP
1330 }
1331}
c19e6535 1332\f
df2c07f4 1333/* Parses the contents of 'buf', which contains a "struct ovs_header" followed
c19e6535
BP
1334 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1335 * positive errno value.
1336 *
1337 * 'vport' will contain pointers into 'buf', so the caller should not free
1338 * 'buf' while 'vport' is still in use. */
1339static int
1340dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1341 const struct ofpbuf *buf)
1342{
df2c07f4
JP
1343 static const struct nl_policy ovs_vport_policy[] = {
1344 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1345 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1346 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
b063d9f0 1347 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
f7df9823 1348 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
c19e6535 1349 .optional = true },
df2c07f4 1350 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
c19e6535
BP
1351 .min_len = ETH_ADDR_LEN,
1352 .max_len = ETH_ADDR_LEN,
1353 .optional = true },
df2c07f4 1354 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
c19e6535
BP
1355 };
1356
df2c07f4
JP
1357 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1358 struct ovs_header *ovs_header;
f0fef760
BP
1359 struct nlmsghdr *nlmsg;
1360 struct genlmsghdr *genl;
1361 struct ofpbuf b;
c19e6535
BP
1362
1363 dpif_linux_vport_init(vport);
1364
f0fef760
BP
1365 ofpbuf_use_const(&b, buf->data, buf->size);
1366 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1367 genl = ofpbuf_try_pull(&b, sizeof *genl);
df2c07f4
JP
1368 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1369 if (!nlmsg || !genl || !ovs_header
1370 || nlmsg->nlmsg_type != ovs_vport_family
1371 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1372 ARRAY_SIZE(ovs_vport_policy))) {
c19e6535
BP
1373 return EINVAL;
1374 }
c19e6535 1375
f0fef760 1376 vport->cmd = genl->cmd;
df2c07f4
JP
1377 vport->dp_ifindex = ovs_header->dp_ifindex;
1378 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1379 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1380 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
b063d9f0 1381 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
a24a6574 1382 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
b063d9f0 1383 }
df2c07f4
JP
1384 if (a[OVS_VPORT_ATTR_STATS]) {
1385 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1386 }
1387 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1388 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1389 }
df2c07f4
JP
1390 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1391 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1392 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
c19e6535 1393 }
c19e6535
BP
1394 return 0;
1395}
1396
df2c07f4 1397/* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
c19e6535
BP
1398 * followed by Netlink attributes corresponding to 'vport'. */
1399static void
1400dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1401 struct ofpbuf *buf)
1402{
df2c07f4 1403 struct ovs_header *ovs_header;
f0fef760 1404
df2c07f4 1405 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
69685a88 1406 vport->cmd, OVS_VPORT_VERSION);
c19e6535 1407
df2c07f4
JP
1408 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1409 ovs_header->dp_ifindex = vport->dp_ifindex;
c19e6535
BP
1410
1411 if (vport->port_no != UINT32_MAX) {
df2c07f4 1412 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
c19e6535
BP
1413 }
1414
df2c07f4
JP
1415 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1416 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
c19e6535
BP
1417 }
1418
1419 if (vport->name) {
df2c07f4 1420 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
c19e6535
BP
1421 }
1422
a24a6574
BP
1423 if (vport->upcall_pid) {
1424 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1425 }
b063d9f0 1426
c19e6535 1427 if (vport->stats) {
df2c07f4 1428 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
c19e6535
BP
1429 vport->stats, sizeof *vport->stats);
1430 }
1431
1432 if (vport->address) {
df2c07f4 1433 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
c19e6535
BP
1434 vport->address, ETH_ADDR_LEN);
1435 }
1436
c19e6535 1437 if (vport->options) {
df2c07f4 1438 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
c19e6535
BP
1439 vport->options, vport->options_len);
1440 }
c19e6535
BP
1441}
1442
1443/* Clears 'vport' to "empty" values. */
1444void
1445dpif_linux_vport_init(struct dpif_linux_vport *vport)
1446{
1447 memset(vport, 0, sizeof *vport);
c19e6535
BP
1448 vport->port_no = UINT32_MAX;
1449}
1450
1451/* Executes 'request' in the kernel datapath. If the command fails, returns a
1452 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1453 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
df2c07f4 1454 * result of the command is expected to be an ovs_vport also, which is decoded
c19e6535
BP
1455 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1456 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1457int
1458dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1459 struct dpif_linux_vport *reply,
1460 struct ofpbuf **bufp)
1461{
f0fef760 1462 struct ofpbuf *request_buf;
c19e6535
BP
1463 int error;
1464
1465 assert((reply != NULL) == (bufp != NULL));
c19e6535 1466
42bb6c72
BP
1467 error = dpif_linux_init();
1468 if (error) {
1469 if (reply) {
1470 *bufp = NULL;
1471 dpif_linux_vport_init(reply);
1472 }
1473 return error;
1474 }
1475
f0fef760
BP
1476 request_buf = ofpbuf_new(1024);
1477 dpif_linux_vport_to_ofpbuf(request, request_buf);
1478 error = nl_sock_transact(genl_sock, request_buf, bufp);
1479 ofpbuf_delete(request_buf);
c19e6535 1480
f0fef760
BP
1481 if (reply) {
1482 if (!error) {
1483 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1484 }
c19e6535 1485 if (error) {
f0fef760
BP
1486 dpif_linux_vport_init(reply);
1487 ofpbuf_delete(*bufp);
1488 *bufp = NULL;
c19e6535 1489 }
c19e6535
BP
1490 }
1491 return error;
1492}
1493
1494/* Obtains information about the kernel vport named 'name' and stores it into
1495 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1496 * longer needed ('reply' will contain pointers into '*bufp'). */
1497int
1498dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1499 struct ofpbuf **bufp)
1500{
1501 struct dpif_linux_vport request;
1502
1503 dpif_linux_vport_init(&request);
df2c07f4 1504 request.cmd = OVS_VPORT_CMD_GET;
c19e6535
BP
1505 request.name = name;
1506
1507 return dpif_linux_vport_transact(&request, reply, bufp);
1508}
d6569377 1509\f
df2c07f4 1510/* Parses the contents of 'buf', which contains a "struct ovs_header" followed
aaff4b55
BP
1511 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1512 * positive errno value.
d6569377
BP
1513 *
1514 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1515 * while 'dp' is still in use. */
1516static int
1517dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1518{
df2c07f4
JP
1519 static const struct nl_policy ovs_datapath_policy[] = {
1520 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
f7df9823 1521 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
d6569377 1522 .optional = true },
d6569377
BP
1523 };
1524
df2c07f4
JP
1525 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1526 struct ovs_header *ovs_header;
aaff4b55
BP
1527 struct nlmsghdr *nlmsg;
1528 struct genlmsghdr *genl;
1529 struct ofpbuf b;
d6569377
BP
1530
1531 dpif_linux_dp_init(dp);
1532
aaff4b55
BP
1533 ofpbuf_use_const(&b, buf->data, buf->size);
1534 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1535 genl = ofpbuf_try_pull(&b, sizeof *genl);
df2c07f4
JP
1536 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1537 if (!nlmsg || !genl || !ovs_header
1538 || nlmsg->nlmsg_type != ovs_datapath_family
1539 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1540 ARRAY_SIZE(ovs_datapath_policy))) {
d6569377
BP
1541 return EINVAL;
1542 }
d6569377 1543
aaff4b55 1544 dp->cmd = genl->cmd;
df2c07f4
JP
1545 dp->dp_ifindex = ovs_header->dp_ifindex;
1546 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1547 if (a[OVS_DP_ATTR_STATS]) {
d6569377
BP
1548 /* Can't use structure assignment because Netlink doesn't ensure
1549 * sufficient alignment for 64-bit members. */
df2c07f4 1550 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
d6569377
BP
1551 sizeof dp->stats);
1552 }
982b8810 1553
d6569377
BP
1554 return 0;
1555}
1556
aaff4b55 1557/* Appends to 'buf' the Generic Netlink message described by 'dp'. */
d6569377
BP
1558static void
1559dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1560{
df2c07f4 1561 struct ovs_header *ovs_header;
d6569377 1562
df2c07f4 1563 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
69685a88
JG
1564 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1565 OVS_DATAPATH_VERSION);
aaff4b55 1566
df2c07f4
JP
1567 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1568 ovs_header->dp_ifindex = dp->dp_ifindex;
d6569377
BP
1569
1570 if (dp->name) {
df2c07f4 1571 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
d6569377
BP
1572 }
1573
a24a6574
BP
1574 if (dp->upcall_pid) {
1575 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1576 }
b063d9f0 1577
df2c07f4 1578 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
d6569377
BP
1579}
1580
1581/* Clears 'dp' to "empty" values. */
d3d8f1f7 1582static void
d6569377
BP
1583dpif_linux_dp_init(struct dpif_linux_dp *dp)
1584{
1585 memset(dp, 0, sizeof *dp);
d6569377
BP
1586}
1587
aaff4b55
BP
1588static void
1589dpif_linux_dp_dump_start(struct nl_dump *dump)
1590{
1591 struct dpif_linux_dp request;
1592 struct ofpbuf *buf;
1593
1594 dpif_linux_dp_init(&request);
df2c07f4 1595 request.cmd = OVS_DP_CMD_GET;
aaff4b55
BP
1596
1597 buf = ofpbuf_new(1024);
1598 dpif_linux_dp_to_ofpbuf(&request, buf);
1599 nl_dump_start(dump, genl_sock, buf);
1600 ofpbuf_delete(buf);
1601}
1602
d6569377
BP
1603/* Executes 'request' in the kernel datapath. If the command fails, returns a
1604 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1605 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
aaff4b55
BP
1606 * result of the command is expected to be of the same form, which is decoded
1607 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1608 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
d3d8f1f7 1609static int
d6569377
BP
1610dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1611 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1612{
aaff4b55 1613 struct ofpbuf *request_buf;
d6569377 1614 int error;
d6569377
BP
1615
1616 assert((reply != NULL) == (bufp != NULL));
1617
aaff4b55
BP
1618 request_buf = ofpbuf_new(1024);
1619 dpif_linux_dp_to_ofpbuf(request, request_buf);
1620 error = nl_sock_transact(genl_sock, request_buf, bufp);
1621 ofpbuf_delete(request_buf);
d6569377 1622
aaff4b55
BP
1623 if (reply) {
1624 if (!error) {
1625 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1626 }
d6569377 1627 if (error) {
aaff4b55
BP
1628 dpif_linux_dp_init(reply);
1629 ofpbuf_delete(*bufp);
1630 *bufp = NULL;
d6569377 1631 }
d6569377
BP
1632 }
1633 return error;
1634}
1635
1636/* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1637 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1638 * will contain pointers into '*bufp'). */
d3d8f1f7 1639static int
d6569377
BP
1640dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1641 struct ofpbuf **bufp)
1642{
1643 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1644 struct dpif_linux_dp request;
1645
1646 dpif_linux_dp_init(&request);
df2c07f4 1647 request.cmd = OVS_DP_CMD_GET;
254f2dc8 1648 request.dp_ifindex = dpif->dp_ifindex;
d6569377
BP
1649
1650 return dpif_linux_dp_transact(&request, reply, bufp);
1651}
1652\f
df2c07f4 1653/* Parses the contents of 'buf', which contains a "struct ovs_header" followed
37a1300c 1654 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
d6569377
BP
1655 * positive errno value.
1656 *
1657 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1658 * while 'flow' is still in use. */
1659static int
1660dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1661 const struct ofpbuf *buf)
1662{
df2c07f4
JP
1663 static const struct nl_policy ovs_flow_policy[] = {
1664 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1665 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
f7df9823 1666 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
d6569377 1667 .optional = true },
df2c07f4
JP
1668 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1669 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1670 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
d6569377
BP
1671 };
1672
df2c07f4
JP
1673 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1674 struct ovs_header *ovs_header;
37a1300c
BP
1675 struct nlmsghdr *nlmsg;
1676 struct genlmsghdr *genl;
1677 struct ofpbuf b;
d6569377
BP
1678
1679 dpif_linux_flow_init(flow);
1680
37a1300c
BP
1681 ofpbuf_use_const(&b, buf->data, buf->size);
1682 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1683 genl = ofpbuf_try_pull(&b, sizeof *genl);
df2c07f4
JP
1684 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1685 if (!nlmsg || !genl || !ovs_header
1686 || nlmsg->nlmsg_type != ovs_flow_family
1687 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1688 ARRAY_SIZE(ovs_flow_policy))) {
d6569377
BP
1689 return EINVAL;
1690 }
d6569377 1691
37a1300c 1692 flow->nlmsg_flags = nlmsg->nlmsg_flags;
df2c07f4
JP
1693 flow->dp_ifindex = ovs_header->dp_ifindex;
1694 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1695 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1696 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1697 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1698 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
d6569377 1699 }
df2c07f4
JP
1700 if (a[OVS_FLOW_ATTR_STATS]) {
1701 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
d6569377 1702 }
df2c07f4
JP
1703 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1704 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
d6569377 1705 }
df2c07f4
JP
1706 if (a[OVS_FLOW_ATTR_USED]) {
1707 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
9e980142 1708 }
d6569377
BP
1709 return 0;
1710}
1711
df2c07f4 1712/* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
d6569377
BP
1713 * followed by Netlink attributes corresponding to 'flow'. */
1714static void
1715dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1716 struct ofpbuf *buf)
1717{
df2c07f4 1718 struct ovs_header *ovs_header;
d6569377 1719
df2c07f4 1720 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
30b44744 1721 NLM_F_REQUEST | flow->nlmsg_flags,
69685a88 1722 flow->cmd, OVS_FLOW_VERSION);
37a1300c 1723
df2c07f4
JP
1724 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1725 ovs_header->dp_ifindex = flow->dp_ifindex;
d6569377
BP
1726
1727 if (flow->key_len) {
df2c07f4 1728 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
d6569377
BP
1729 }
1730
d2a23af2 1731 if (flow->actions || flow->actions_len) {
df2c07f4 1732 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
d6569377
BP
1733 flow->actions, flow->actions_len);
1734 }
1735
1736 /* We never need to send these to the kernel. */
1737 assert(!flow->stats);
1738 assert(!flow->tcp_flags);
1739 assert(!flow->used);
1740
1741 if (flow->clear) {
df2c07f4 1742 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
d6569377 1743 }
d6569377
BP
1744}
1745
1746/* Clears 'flow' to "empty" values. */
d3d8f1f7 1747static void
d6569377
BP
1748dpif_linux_flow_init(struct dpif_linux_flow *flow)
1749{
1750 memset(flow, 0, sizeof *flow);
1751}
1752
1753/* Executes 'request' in the kernel datapath. If the command fails, returns a
1754 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1755 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
37a1300c
BP
1756 * result of the command is expected to be a flow also, which is decoded and
1757 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1758 * is no longer needed ('reply' will contain pointers into '*bufp'). */
d3d8f1f7 1759static int
30b44744 1760dpif_linux_flow_transact(struct dpif_linux_flow *request,
d6569377
BP
1761 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1762{
37a1300c 1763 struct ofpbuf *request_buf;
d6569377 1764 int error;
d6569377
BP
1765
1766 assert((reply != NULL) == (bufp != NULL));
1767
30b44744
BP
1768 if (reply) {
1769 request->nlmsg_flags |= NLM_F_ECHO;
1770 }
1771
37a1300c
BP
1772 request_buf = ofpbuf_new(1024);
1773 dpif_linux_flow_to_ofpbuf(request, request_buf);
1774 error = nl_sock_transact(genl_sock, request_buf, bufp);
1775 ofpbuf_delete(request_buf);
d6569377 1776
37a1300c
BP
1777 if (reply) {
1778 if (!error) {
1779 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1780 }
d6569377 1781 if (error) {
37a1300c
BP
1782 dpif_linux_flow_init(reply);
1783 ofpbuf_delete(*bufp);
1784 *bufp = NULL;
d6569377 1785 }
d6569377
BP
1786 }
1787 return error;
1788}
1789
1790static void
1791dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1792 struct dpif_flow_stats *stats)
1793{
1794 if (flow->stats) {
1795 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1796 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1797 } else {
1798 stats->n_packets = 0;
1799 stats->n_bytes = 0;
1800 }
0e70cdcb 1801 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
d6569377
BP
1802 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1803}