]> git.proxmox.com Git - ovs.git/blob - lib/dpif-linux.c
datapath: Change vport type from string to integer enumeration.
[ovs.git] / lib / dpif-linux.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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>
18
19 #include "dpif-linux.h"
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>
27 #include <linux/types.h>
28 #include <linux/ethtool.h>
29 #include <linux/pkt_sched.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/sockios.h>
32 #include <stdlib.h>
33 #include <sys/ioctl.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36
37 #include "dpif-provider.h"
38 #include "netdev.h"
39 #include "netdev-vport.h"
40 #include "netlink.h"
41 #include "ofpbuf.h"
42 #include "openvswitch/tunnel.h"
43 #include "packets.h"
44 #include "poll-loop.h"
45 #include "rtnetlink.h"
46 #include "rtnetlink-link.h"
47 #include "shash.h"
48 #include "svec.h"
49 #include "util.h"
50 #include "vlog.h"
51
52 VLOG_DEFINE_THIS_MODULE(dpif_linux);
53
54 /* Datapath interface for the openvswitch Linux kernel module. */
55 struct dpif_linux {
56 struct dpif dpif;
57 int fd;
58
59 /* Used by dpif_linux_get_all_names(). */
60 char *local_ifname;
61 int minor;
62
63 /* Change notification. */
64 int local_ifindex; /* Ifindex of local port. */
65 struct shash changed_ports; /* Ports that have changed. */
66 struct rtnetlink_notifier port_notifier;
67 bool change_error;
68 };
69
70 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
71
72 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
73 static int lookup_internal_device(const char *name, int *dp_idx, int *port_no);
74 static int lookup_minor(const char *name, int *minor);
75 static int finish_open(struct dpif *, const char *local_ifname);
76 static int get_openvswitch_major(void);
77 static int create_minor(const char *name, int minor, struct dpif **dpifp);
78 static int open_minor(int minor, struct dpif **dpifp);
79 static int make_openvswitch_device(int minor, char **fnp);
80 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
81 void *dpif);
82
83 static struct dpif_linux *
84 dpif_linux_cast(const struct dpif *dpif)
85 {
86 dpif_assert_class(dpif, &dpif_linux_class);
87 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
88 }
89
90 static int
91 dpif_linux_enumerate(struct svec *all_dps)
92 {
93 int major;
94 int error;
95 int i;
96
97 /* Check that the Open vSwitch module is loaded. */
98 major = get_openvswitch_major();
99 if (major < 0) {
100 return -major;
101 }
102
103 error = 0;
104 for (i = 0; i < ODP_MAX; i++) {
105 struct dpif *dpif;
106 char devname[16];
107 int retval;
108
109 sprintf(devname, "dp%d", i);
110 retval = dpif_open(devname, "system", &dpif);
111 if (!retval) {
112 svec_add(all_dps, devname);
113 dpif_uninit(dpif, true);
114 } else if (retval != ENODEV && !error) {
115 error = retval;
116 }
117 }
118 return error;
119 }
120
121 static int
122 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
123 bool create, struct dpif **dpifp)
124 {
125 int minor;
126
127 minor = !strncmp(name, "dp", 2)
128 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
129 if (create) {
130 if (minor >= 0) {
131 return create_minor(name, minor, dpifp);
132 } else {
133 /* Scan for unused minor number. */
134 for (minor = 0; minor < ODP_MAX; minor++) {
135 int error = create_minor(name, minor, dpifp);
136 if (error != EBUSY) {
137 return error;
138 }
139 }
140
141 /* All datapath numbers in use. */
142 return ENOBUFS;
143 }
144 } else {
145 struct dpif_linux *dpif;
146 struct odp_port port;
147 int error;
148
149 if (minor < 0) {
150 error = lookup_minor(name, &minor);
151 if (error) {
152 return error;
153 }
154 }
155
156 error = open_minor(minor, dpifp);
157 if (error) {
158 return error;
159 }
160 dpif = dpif_linux_cast(*dpifp);
161
162 /* We need the local port's ifindex for the poll function. Start by
163 * getting the local port's name. */
164 memset(&port, 0, sizeof port);
165 port.port = ODPP_LOCAL;
166 if (ioctl(dpif->fd, ODP_VPORT_QUERY, &port)) {
167 error = errno;
168 if (error != ENODEV) {
169 VLOG_WARN("%s: probe returned unexpected error: %s",
170 dpif_name(*dpifp), strerror(error));
171 }
172 dpif_uninit(*dpifp, true);
173 return error;
174 }
175
176 /* Then use that to finish up opening. */
177 return finish_open(&dpif->dpif, port.devname);
178 }
179 }
180
181 static void
182 dpif_linux_close(struct dpif *dpif_)
183 {
184 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
185 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
186 shash_destroy(&dpif->changed_ports);
187 free(dpif->local_ifname);
188 close(dpif->fd);
189 free(dpif);
190 }
191
192 static int
193 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
194 {
195 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
196
197 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
198 svec_add(all_names, dpif->local_ifname);
199 return 0;
200 }
201
202 static int
203 dpif_linux_destroy(struct dpif *dpif_)
204 {
205 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
206 }
207
208 static int
209 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
210 {
211 memset(stats, 0, sizeof *stats);
212 return do_ioctl(dpif_, ODP_DP_STATS, stats);
213 }
214
215 static int
216 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
217 {
218 int drop_frags;
219 int error;
220
221 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
222 if (!error) {
223 *drop_fragsp = drop_frags & 1;
224 }
225 return error;
226 }
227
228 static int
229 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
230 {
231 int drop_frags_int = drop_frags;
232 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
233 }
234
235 static const char *
236 vport_type_to_netdev_type(const struct odp_port *odp_port)
237 {
238 struct tnl_port_config tnl_config;
239
240 switch (odp_port->type) {
241 case ODP_VPORT_TYPE_UNSPEC:
242 break;
243
244 case ODP_VPORT_TYPE_NETDEV:
245 return "system";
246
247 case ODP_VPORT_TYPE_INTERNAL:
248 return "internal";
249
250 case ODP_VPORT_TYPE_PATCH:
251 return "patch";
252
253 case ODP_VPORT_TYPE_GRE:
254 memcpy(&tnl_config, odp_port->config, sizeof tnl_config);
255 return tnl_config.flags & TNL_F_IPSEC ? "ipsec_gre" : "gre";
256
257 case ODP_VPORT_TYPE_CAPWAP:
258 return "capwap";
259
260 case __ODP_VPORT_TYPE_MAX:
261 break;
262 }
263
264 VLOG_WARN_RL(&error_rl, "dp%d: port `%s' has unsupported type %"PRIu32,
265 odp_port->dp_idx, odp_port->devname, odp_port->type);
266 return "unknown";
267 }
268
269 static enum odp_vport_type
270 netdev_type_to_vport_type(const char *type)
271 {
272 return (!strcmp(type, "system") ? ODP_VPORT_TYPE_NETDEV
273 : !strcmp(type, "internal") ? ODP_VPORT_TYPE_INTERNAL
274 : !strcmp(type, "patch") ? ODP_VPORT_TYPE_PATCH
275 : (!strcmp(type, "gre")
276 || !strcmp(type, "ipsec_gre")) ? ODP_VPORT_TYPE_GRE
277 : !strcmp(type, "capwap") ? ODP_VPORT_TYPE_CAPWAP
278 : ODP_VPORT_TYPE_UNSPEC);
279 }
280
281 static int
282 dpif_linux_port_add(struct dpif *dpif, struct netdev *netdev,
283 uint16_t *port_nop)
284 {
285 const char *name = netdev_get_name(netdev);
286 const char *type = netdev_get_type(netdev);
287 struct odp_port port;
288 int error;
289
290 memset(&port, 0, sizeof port);
291 strncpy(port.devname, name, sizeof port.devname);
292 netdev_vport_get_config(netdev, port.config);
293
294 port.type = netdev_type_to_vport_type(type);
295 if (port.type == ODP_VPORT_TYPE_UNSPEC) {
296 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
297 "unsupported type `%s'",
298 dpif_name(dpif), name, type);
299 return EINVAL;
300 }
301
302 error = do_ioctl(dpif, ODP_VPORT_ATTACH, &port);
303 if (!error) {
304 *port_nop = port.port;
305 }
306
307 return error;
308 }
309
310 static int
311 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no_)
312 {
313 int port_no = port_no_; /* Kernel expects an "int". */
314 return do_ioctl(dpif_, ODP_VPORT_DETACH, &port_no);
315 }
316
317 static int
318 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
319 const char *port_name, struct dpif_port *dpif_port)
320 {
321 struct odp_port odp_port;
322 int error;
323
324 memset(&odp_port, 0, sizeof odp_port);
325 odp_port.port = port_no;
326 strncpy(odp_port.devname, port_name, sizeof odp_port.devname);
327
328 error = do_ioctl(dpif, ODP_VPORT_QUERY, &odp_port);
329 if (error) {
330 return error;
331 } else if (odp_port.dp_idx != dpif_linux_cast(dpif)->minor) {
332 /* A vport named 'port_name' exists but in some other datapath. */
333 return ENOENT;
334 } else {
335 dpif_port->name = xstrdup(odp_port.devname);
336 dpif_port->type = xstrdup(vport_type_to_netdev_type(&odp_port));
337 dpif_port->port_no = odp_port.port;
338 return 0;
339 }
340 }
341
342 static int
343 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
344 struct dpif_port *dpif_port)
345 {
346 return dpif_linux_port_query__(dpif, port_no, "", dpif_port);
347 }
348
349 static int
350 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
351 struct dpif_port *dpif_port)
352 {
353 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
354 }
355
356 static int
357 dpif_linux_flow_flush(struct dpif *dpif_)
358 {
359 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
360 }
361
362 static int
363 dpif_linux_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
364 {
365 *statep = xzalloc(sizeof(struct odp_port));
366 return 0;
367 }
368
369 static int
370 dpif_linux_port_dump_next(const struct dpif *dpif, void *state,
371 struct dpif_port *dpif_port)
372 {
373 struct odp_port *odp_port = state;
374 struct odp_vport_dump dump;
375 int error;
376
377 dump.port = odp_port;
378 dump.port_no = odp_port->port;
379 error = do_ioctl(dpif, ODP_VPORT_DUMP, &dump);
380 if (error) {
381 return error;
382 } else if (odp_port->devname[0] == '\0') {
383 return EOF;
384 } else {
385 dpif_port->name = odp_port->devname;
386 dpif_port->type = (char *) vport_type_to_netdev_type(odp_port);
387 dpif_port->port_no = odp_port->port;
388 odp_port->port++;
389 return 0;
390 }
391 }
392
393 static int
394 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
395 {
396 free(state);
397 return 0;
398 }
399
400 static int
401 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
402 {
403 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
404
405 if (dpif->change_error) {
406 dpif->change_error = false;
407 shash_clear(&dpif->changed_ports);
408 return ENOBUFS;
409 } else if (!shash_is_empty(&dpif->changed_ports)) {
410 struct shash_node *node = shash_first(&dpif->changed_ports);
411 *devnamep = shash_steal(&dpif->changed_ports, node);
412 return 0;
413 } else {
414 return EAGAIN;
415 }
416 }
417
418 static void
419 dpif_linux_port_poll_wait(const struct dpif *dpif_)
420 {
421 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
422 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
423 poll_immediate_wake();
424 } else {
425 rtnetlink_link_notifier_wait();
426 }
427 }
428
429 static int
430 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
431 {
432 struct odp_flowvec fv;
433 fv.flows = flows;
434 fv.n_flows = n;
435 return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
436 }
437
438 static int
439 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
440 {
441 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
442 }
443
444 static int
445 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
446 {
447 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
448 }
449
450 static int
451 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
452 {
453 *statep = xzalloc(sizeof(struct odp_flow_dump));
454 return 0;
455 }
456
457 static int
458 dpif_linux_flow_dump_next(const struct dpif *dpif, void *state,
459 struct odp_flow *flow)
460 {
461 struct odp_flow_dump *dump = state;
462 int error;
463
464 dump->flow = flow;
465 error = do_ioctl(dpif, ODP_FLOW_DUMP, dump);
466 return error ? error : flow->flags & ODPFF_EOF ? EOF : 0;
467 }
468
469 static int
470 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
471 {
472 free(state);
473 return 0;
474 }
475
476 static int
477 dpif_linux_execute(struct dpif *dpif_,
478 const struct nlattr *actions, size_t actions_len,
479 const struct ofpbuf *buf)
480 {
481 struct odp_execute execute;
482 memset(&execute, 0, sizeof execute);
483 execute.actions = (struct nlattr *) actions;
484 execute.actions_len = actions_len;
485 execute.data = buf->data;
486 execute.length = buf->size;
487 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
488 }
489
490 static int
491 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
492 {
493 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
494 }
495
496 static int
497 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
498 {
499 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
500 }
501
502 static int
503 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
504 uint32_t *probability)
505 {
506 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
507 }
508
509 static int
510 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
511 {
512 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
513 }
514
515 static int
516 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
517 uint32_t queue_id, uint32_t *priority)
518 {
519 if (queue_id < 0xf000) {
520 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
521 return 0;
522 } else {
523 return EINVAL;
524 }
525 }
526
527 static int
528 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall)
529 {
530 static const struct nl_policy odp_packet_policy[] = {
531 /* Always present. */
532 [ODP_PACKET_ATTR_TYPE] = { .type = NL_A_U32 },
533 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
534 .min_len = ETH_HEADER_LEN },
535 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
536
537 /* _ODPL_ACTION_NR only. */
538 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
539
540 /* _ODPL_SFLOW_NR only. */
541 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
542 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
543 };
544
545 struct odp_packet *odp_packet = buf->data;
546 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
547
548 if (!nl_policy_parse(buf, sizeof *odp_packet, odp_packet_policy,
549 a, ARRAY_SIZE(odp_packet_policy))) {
550 return EINVAL;
551 }
552
553 memset(upcall, 0, sizeof *upcall);
554 upcall->type = nl_attr_get_u32(a[ODP_PACKET_ATTR_TYPE]);
555 upcall->packet = buf;
556 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
557 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
558 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
559 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
560 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
561 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
562 : 0);
563 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
564 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
565 : 0);
566 if (a[ODP_PACKET_ATTR_ACTIONS]) {
567 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
568 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
569 }
570
571 return 0;
572 }
573
574 static int
575 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
576 {
577 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
578 struct ofpbuf *buf;
579 int retval;
580 int error;
581
582 buf = ofpbuf_new(65536);
583 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
584 if (retval < 0) {
585 error = errno;
586 if (error != EAGAIN) {
587 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
588 dpif_name(dpif_), strerror(error));
589 }
590 } else if (retval >= sizeof(struct odp_packet)) {
591 struct odp_packet *odp_packet = buf->data;
592 buf->size += retval;
593
594 if (odp_packet->len <= retval) {
595 error = parse_odp_packet(buf, upcall);
596 } else {
597 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
598 "from %"PRIu32" bytes to %d",
599 dpif_name(dpif_), odp_packet->len, retval);
600 error = ERANGE;
601 }
602 } else if (!retval) {
603 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
604 error = EPROTO;
605 } else {
606 VLOG_WARN_RL(&error_rl, "%s: discarding too-short message (%d bytes)",
607 dpif_name(dpif_), retval);
608 error = ERANGE;
609 }
610
611 if (error) {
612 ofpbuf_delete(buf);
613 }
614 return error;
615 }
616
617 static void
618 dpif_linux_recv_wait(struct dpif *dpif_)
619 {
620 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
621 poll_fd_wait(dpif->fd, POLLIN);
622 }
623
624 const struct dpif_class dpif_linux_class = {
625 "system",
626 NULL,
627 NULL,
628 dpif_linux_enumerate,
629 dpif_linux_open,
630 dpif_linux_close,
631 dpif_linux_get_all_names,
632 dpif_linux_destroy,
633 dpif_linux_get_stats,
634 dpif_linux_get_drop_frags,
635 dpif_linux_set_drop_frags,
636 dpif_linux_port_add,
637 dpif_linux_port_del,
638 dpif_linux_port_query_by_number,
639 dpif_linux_port_query_by_name,
640 dpif_linux_port_dump_start,
641 dpif_linux_port_dump_next,
642 dpif_linux_port_dump_done,
643 dpif_linux_port_poll,
644 dpif_linux_port_poll_wait,
645 dpif_linux_flow_get,
646 dpif_linux_flow_put,
647 dpif_linux_flow_del,
648 dpif_linux_flow_flush,
649 dpif_linux_flow_dump_start,
650 dpif_linux_flow_dump_next,
651 dpif_linux_flow_dump_done,
652 dpif_linux_execute,
653 dpif_linux_recv_get_mask,
654 dpif_linux_recv_set_mask,
655 dpif_linux_get_sflow_probability,
656 dpif_linux_set_sflow_probability,
657 dpif_linux_queue_to_priority,
658 dpif_linux_recv,
659 dpif_linux_recv_wait,
660 };
661 \f
662 static int get_openvswitch_major(void);
663 static int get_major(const char *target);
664
665 static int
666 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
667 {
668 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
669 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
670 }
671
672 static int
673 lookup_internal_device(const char *name, int *dp_idx, int *port_no)
674 {
675 struct odp_port odp_port;
676 static int dp0_fd = -1;
677
678 if (dp0_fd < 0) {
679 int error;
680 char *fn;
681
682 error = make_openvswitch_device(0, &fn);
683 if (error) {
684 return error;
685 }
686
687 dp0_fd = open(fn, O_RDONLY | O_NONBLOCK);
688 if (dp0_fd < 0) {
689 VLOG_WARN_RL(&error_rl, "%s: open failed (%s)",
690 fn, strerror(errno));
691 free(fn);
692 return errno;
693 }
694 free(fn);
695 }
696
697 memset(&odp_port, 0, sizeof odp_port);
698 strncpy(odp_port.devname, name, sizeof odp_port.devname);
699 if (ioctl(dp0_fd, ODP_VPORT_QUERY, &odp_port)) {
700 if (errno != ENODEV) {
701 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
702 name, strerror(errno));
703 }
704 return errno;
705 } else if (odp_port.type == ODP_VPORT_TYPE_INTERNAL) {
706 *dp_idx = odp_port.dp_idx;
707 *port_no = odp_port.port;
708 return 0;
709 } else {
710 return EINVAL;
711 }
712 }
713
714 static int
715 lookup_minor(const char *name, int *minorp)
716 {
717 int minor, port_no;
718 int error;
719
720 error = lookup_internal_device(name, &minor, &port_no);
721 if (error) {
722 return error;
723 } else if (port_no != ODPP_LOCAL) {
724 /* This is an Open vSwitch device but not the local port. We
725 * intentionally support only using the name of the local port as the
726 * name of a datapath; otherwise, it would be too difficult to
727 * enumerate all the names of a datapath. */
728 return EOPNOTSUPP;
729 } else {
730 *minorp = minor;
731 return 0;
732 }
733 }
734
735 bool
736 dpif_linux_is_internal_device(const char *name)
737 {
738 int minor, port_no;
739
740 return !lookup_internal_device(name, &minor, &port_no);
741 }
742
743 static int
744 make_openvswitch_device(int minor, char **fnp)
745 {
746 const char dirname[] = "/dev/net";
747 int major;
748 dev_t dev;
749 struct stat s;
750 char fn[128];
751
752 *fnp = NULL;
753
754 major = get_openvswitch_major();
755 if (major < 0) {
756 return -major;
757 }
758 dev = makedev(major, minor);
759
760 sprintf(fn, "%s/dp%d", dirname, minor);
761 if (!stat(fn, &s)) {
762 if (!S_ISCHR(s.st_mode)) {
763 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
764 fn);
765 } else if (s.st_rdev != dev) {
766 VLOG_WARN_RL(&error_rl,
767 "%s is device %u:%u but should be %u:%u, fixing",
768 fn, major(s.st_rdev), minor(s.st_rdev),
769 major(dev), minor(dev));
770 } else {
771 goto success;
772 }
773 if (unlink(fn)) {
774 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
775 fn, strerror(errno));
776 return errno;
777 }
778 } else if (errno == ENOENT) {
779 if (stat(dirname, &s)) {
780 if (errno == ENOENT) {
781 if (mkdir(dirname, 0755)) {
782 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
783 dirname, strerror(errno));
784 return errno;
785 }
786 } else {
787 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
788 dirname, strerror(errno));
789 return errno;
790 }
791 }
792 } else {
793 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
794 return errno;
795 }
796
797 /* The device needs to be created. */
798 if (mknod(fn, S_IFCHR | 0700, dev)) {
799 VLOG_WARN_RL(&error_rl,
800 "%s: creating character device %u:%u failed (%s)",
801 fn, major(dev), minor(dev), strerror(errno));
802 return errno;
803 }
804
805 success:
806 *fnp = xstrdup(fn);
807 return 0;
808 }
809
810 /* Return the major device number of the Open vSwitch device. If it
811 * cannot be determined, a negative errno is returned. */
812 static int
813 get_openvswitch_major(void)
814 {
815 static int openvswitch_major = -1;
816 if (openvswitch_major < 0) {
817 openvswitch_major = get_major("openvswitch");
818 }
819 return openvswitch_major;
820 }
821
822 static int
823 get_major(const char *target)
824 {
825 const char fn[] = "/proc/devices";
826 char line[128];
827 FILE *file;
828 int ln;
829
830 file = fopen(fn, "r");
831 if (!file) {
832 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
833 return -errno;
834 }
835
836 for (ln = 1; fgets(line, sizeof line, file); ln++) {
837 char name[64];
838 int major;
839
840 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
841 /* Nothing to do. */
842 } else if (!strncmp(line, "Block", 5)) {
843 /* We only want character devices, so skip the rest of the file. */
844 break;
845 } else if (sscanf(line, "%d %63s", &major, name)) {
846 if (!strcmp(name, target)) {
847 fclose(file);
848 return major;
849 }
850 } else {
851 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
852 }
853 }
854
855 fclose(file);
856
857 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
858 return -ENODEV;
859 }
860
861 static int
862 finish_open(struct dpif *dpif_, const char *local_ifname)
863 {
864 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
865 dpif->local_ifname = xstrdup(local_ifname);
866 dpif->local_ifindex = if_nametoindex(local_ifname);
867 if (!dpif->local_ifindex) {
868 int error = errno;
869 dpif_uninit(dpif_, true);
870 VLOG_WARN("could not get ifindex of %s device: %s",
871 local_ifname, strerror(errno));
872 return error;
873 }
874 return 0;
875 }
876
877 static int
878 create_minor(const char *name, int minor, struct dpif **dpifp)
879 {
880 int error = open_minor(minor, dpifp);
881 if (!error) {
882 error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
883 if (!error) {
884 error = finish_open(*dpifp, name);
885 } else {
886 dpif_uninit(*dpifp, true);
887 }
888 }
889 return error;
890 }
891
892 static int
893 open_minor(int minor, struct dpif **dpifp)
894 {
895 int error;
896 char *fn;
897 int fd;
898
899 error = make_openvswitch_device(minor, &fn);
900 if (error) {
901 return error;
902 }
903
904 fd = open(fn, O_RDONLY | O_NONBLOCK);
905 if (fd >= 0) {
906 struct dpif_linux *dpif = xmalloc(sizeof *dpif);
907 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
908 dpif_linux_port_changed,
909 dpif);
910 if (!error) {
911 char *name;
912
913 name = xasprintf("dp%d", minor);
914 dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
915 free(name);
916
917 dpif->fd = fd;
918 dpif->local_ifname = NULL;
919 dpif->minor = minor;
920 dpif->local_ifindex = 0;
921 shash_init(&dpif->changed_ports);
922 dpif->change_error = false;
923 *dpifp = &dpif->dpif;
924 } else {
925 free(dpif);
926 }
927 } else {
928 error = errno;
929 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
930 }
931 free(fn);
932
933 return error;
934 }
935
936 static void
937 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
938 void *dpif_)
939 {
940 struct dpif_linux *dpif = dpif_;
941
942 if (change) {
943 if (change->master_ifindex == dpif->local_ifindex
944 && (change->nlmsg_type == RTM_NEWLINK
945 || change->nlmsg_type == RTM_DELLINK))
946 {
947 /* Our datapath changed, either adding a new port or deleting an
948 * existing one. */
949 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
950 }
951 } else {
952 dpif->change_error = true;
953 }
954 }