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