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