Merge branch 'master' into next
[sliver-openvswitch.git] / lib / dpif-linux.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 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 #include "dpif.h"
19
20 #include <assert.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <net/if.h>
26 #include <linux/ethtool.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sockios.h>
29 #include <stdlib.h>
30 #include <sys/ioctl.h>
31 #include <unistd.h>
32
33 #include "dpif-provider.h"
34 #include "ofpbuf.h"
35 #include "poll-loop.h"
36 #include "rtnetlink.h"
37 #include "svec.h"
38 #include "util.h"
39
40 #include "vlog.h"
41 #define THIS_MODULE VLM_dpif_linux
42
43 /* Datapath interface for the openvswitch Linux kernel module. */
44 struct dpif_linux {
45     struct dpif dpif;
46     int fd;
47
48     /* Used by dpif_linux_get_all_names(). */
49     char *local_ifname;
50     int minor;
51
52     /* Change notification. */
53     int local_ifindex;          /* Ifindex of local port. */
54     struct svec changed_ports;  /* Ports that have changed. */
55     struct rtnetlink_notifier port_notifier;
56     bool change_error;
57 };
58
59 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
60
61 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
62 static int lookup_minor(const char *name, int *minor);
63 static int finish_open(struct dpif *, const char *local_ifname);
64 static int get_openvswitch_major(void);
65 static int create_minor(const char *name, int minor, struct dpif **dpifp);
66 static int open_minor(int minor, struct dpif **dpifp);
67 static int make_openvswitch_device(int minor, char **fnp);
68 static void dpif_linux_port_changed(const struct rtnetlink_change *,
69                                     void *dpif);
70
71 static struct dpif_linux *
72 dpif_linux_cast(const struct dpif *dpif)
73 {
74     dpif_assert_class(dpif, &dpif_linux_class);
75     return CONTAINER_OF(dpif, struct dpif_linux, dpif);
76 }
77
78 static int
79 dpif_linux_enumerate(struct svec *all_dps)
80 {
81     int major;
82     int error;
83     int i;
84
85     /* Check that the Open vSwitch module is loaded. */
86     major = get_openvswitch_major();
87     if (major < 0) {
88         return -major;
89     }
90
91     error = 0;
92     for (i = 0; i < ODP_MAX; i++) {
93         struct dpif *dpif;
94         char devname[16];
95         int retval;
96
97         sprintf(devname, "dp%d", i);
98         retval = dpif_open(devname, "system", &dpif);
99         if (!retval) {
100             svec_add(all_dps, devname);
101             dpif_uninit(dpif, true);
102         } else if (retval != ENODEV && !error) {
103             error = retval;
104         }
105     }
106     return error;
107 }
108
109 static int
110 dpif_linux_open(const char *name, const char *type UNUSED, bool create,
111                 struct dpif **dpifp)
112 {
113     int minor;
114
115     minor = !strncmp(name, "dp", 2)
116             && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
117     if (create) {
118         if (minor >= 0) {
119             return create_minor(name, minor, dpifp);
120         } else {
121             /* Scan for unused minor number. */
122             for (minor = 0; minor < ODP_MAX; minor++) {
123                 int error = create_minor(name, minor, dpifp);
124                 if (error != EBUSY) {
125                     return error;
126                 }
127             }
128
129             /* All datapath numbers in use. */
130             return ENOBUFS;
131         }
132     } else {
133         struct dpif_linux *dpif;
134         struct odp_port port;
135         int error;
136
137         if (minor < 0) {
138             error = lookup_minor(name, &minor);
139             if (error) {
140                 return error;
141             }
142         }
143
144         error = open_minor(minor, dpifp);
145         if (error) {
146             return error;
147         }
148         dpif = dpif_linux_cast(*dpifp);
149
150         /* We need the local port's ifindex for the poll function.  Start by
151          * getting the local port's name. */
152         memset(&port, 0, sizeof port);
153         port.port = ODPP_LOCAL;
154         if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
155             error = errno;
156             if (error != ENODEV) {
157                 VLOG_WARN("%s: probe returned unexpected error: %s",
158                           dpif_name(*dpifp), strerror(error));
159             }
160             dpif_uninit(*dpifp, true);
161             return error;
162         }
163
164         /* Then use that to finish up opening. */
165         return finish_open(&dpif->dpif, port.devname);
166     }
167 }
168
169 static void
170 dpif_linux_close(struct dpif *dpif_)
171 {
172     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
173     rtnetlink_notifier_unregister(&dpif->port_notifier);
174     svec_destroy(&dpif->changed_ports);
175     free(dpif->local_ifname);
176     close(dpif->fd);
177     free(dpif);
178 }
179
180 static int
181 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
182 {
183     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
184
185     svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
186     svec_add(all_names, dpif->local_ifname);
187     return 0;
188 }
189
190 static int
191 dpif_linux_delete(struct dpif *dpif_)
192 {
193     return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
194 }
195
196 static int
197 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
198 {
199     memset(stats, 0, sizeof *stats);
200     return do_ioctl(dpif_, ODP_DP_STATS, stats);
201 }
202
203 static int
204 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
205 {
206     int drop_frags;
207     int error;
208
209     error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
210     if (!error) {
211         *drop_fragsp = drop_frags & 1;
212     }
213     return error;
214 }
215
216 static int
217 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
218 {
219     int drop_frags_int = drop_frags;
220     return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
221 }
222
223 static int
224 dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
225                     uint16_t *port_no)
226 {
227     struct odp_port port;
228     int error;
229
230     memset(&port, 0, sizeof port);
231     strncpy(port.devname, devname, sizeof port.devname);
232     port.flags = flags;
233     error = do_ioctl(dpif_, ODP_PORT_ADD, &port);
234     if (!error) {
235         *port_no = port.port;
236     }
237     return error;
238 }
239
240 static int
241 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
242 {
243     int tmp = port_no;
244     return do_ioctl(dpif_, ODP_PORT_DEL, &tmp);
245 }
246
247 static int
248 dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
249                           struct odp_port *port)
250 {
251     memset(port, 0, sizeof *port);
252     port->port = port_no;
253     return do_ioctl(dpif_, ODP_PORT_QUERY, port);
254 }
255
256 static int
257 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
258                               struct odp_port *port)
259 {
260     memset(port, 0, sizeof *port);
261     strncpy(port->devname, devname, sizeof port->devname);
262     return do_ioctl(dpif_, ODP_PORT_QUERY, port);
263 }
264
265 static int
266 dpif_linux_flow_flush(struct dpif *dpif_)
267 {
268     return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
269 }
270
271 static int
272 dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
273 {
274     struct odp_portvec pv;
275     int error;
276
277     pv.ports = ports;
278     pv.n_ports = n;
279     error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
280     return error ? -error : pv.n_ports;
281 }
282
283 static int
284 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
285 {
286     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
287
288     if (dpif->change_error) {
289         dpif->change_error = false;
290         svec_clear(&dpif->changed_ports);
291         return ENOBUFS;
292     } else if (dpif->changed_ports.n) {
293         *devnamep = dpif->changed_ports.names[--dpif->changed_ports.n];
294         return 0;
295     } else {
296         return EAGAIN;
297     }
298 }
299
300 static void
301 dpif_linux_port_poll_wait(const struct dpif *dpif_)
302 {
303     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
304     if (dpif->changed_ports.n || dpif->change_error) {
305         poll_immediate_wake();
306     } else {
307         rtnetlink_notifier_wait();
308     }
309 }
310
311 static int
312 dpif_linux_port_group_get(const struct dpif *dpif_, int group,
313                           uint16_t ports[], int n)
314 {
315     struct odp_port_group pg;
316     int error;
317
318     assert(n <= UINT16_MAX);
319     pg.group = group;
320     pg.ports = ports;
321     pg.n_ports = n;
322     error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
323     return error ? -error : pg.n_ports;
324 }
325
326 static int
327 dpif_linux_port_group_set(struct dpif *dpif_, int group,
328                           const uint16_t ports[], int n)
329 {
330     struct odp_port_group pg;
331
332     assert(n <= UINT16_MAX);
333     pg.group = group;
334     pg.ports = (uint16_t *) ports;
335     pg.n_ports = n;
336     return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
337 }
338
339 static int
340 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
341 {
342     struct odp_flowvec fv;
343     fv.flows = flows;
344     fv.n_flows = n;
345     return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
346 }
347
348 static int
349 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
350 {
351     return do_ioctl(dpif_, ODP_FLOW_PUT, put);
352 }
353
354 static int
355 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
356 {
357     return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
358 }
359
360 static int
361 dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
362 {
363     struct odp_flowvec fv;
364     int error;
365
366     fv.flows = flows;
367     fv.n_flows = n;
368     error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
369     return error ? -error : fv.n_flows;
370 }
371
372 static int
373 dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
374                    const union odp_action actions[], int n_actions,
375                    const struct ofpbuf *buf)
376 {
377     struct odp_execute execute;
378     memset(&execute, 0, sizeof execute);
379     execute.in_port = in_port;
380     execute.actions = (union odp_action *) actions;
381     execute.n_actions = n_actions;
382     execute.data = buf->data;
383     execute.length = buf->size;
384     return do_ioctl(dpif_, ODP_EXECUTE, &execute);
385 }
386
387 static int
388 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
389 {
390     return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
391 }
392
393 static int
394 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
395 {
396     return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
397 }
398
399 static int
400 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
401                                  uint32_t *probability)
402 {
403     return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
404 }
405
406 static int
407 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
408 {
409     return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
410 }
411
412 static int
413 dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
414 {
415     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
416     struct ofpbuf *buf;
417     int retval;
418     int error;
419
420     buf = ofpbuf_new(65536);
421     retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
422     if (retval < 0) {
423         error = errno;
424         if (error != EAGAIN) {
425             VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
426                          dpif_name(dpif_), strerror(error));
427         }
428     } else if (retval >= sizeof(struct odp_msg)) {
429         struct odp_msg *msg = buf->data;
430         if (msg->length <= retval) {
431             buf->size += retval;
432             *bufp = buf;
433             return 0;
434         } else {
435             VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
436                          "from %"PRIu32" bytes to %d",
437                          dpif_name(dpif_), msg->length, retval);
438             error = ERANGE;
439         }
440     } else if (!retval) {
441         VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
442         error = EPROTO;
443     } else {
444         VLOG_WARN_RL(&error_rl,
445                      "%s: discarding too-short message (%d bytes)",
446                      dpif_name(dpif_), retval);
447         error = ERANGE;
448     }
449
450     *bufp = NULL;
451     ofpbuf_delete(buf);
452     return error;
453 }
454
455 static void
456 dpif_linux_recv_wait(struct dpif *dpif_)
457 {
458     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
459     poll_fd_wait(dpif->fd, POLLIN);
460 }
461
462 const struct dpif_class dpif_linux_class = {
463     "system",
464     NULL,
465     NULL,
466     dpif_linux_enumerate,
467     dpif_linux_open,
468     dpif_linux_close,
469     dpif_linux_get_all_names,
470     dpif_linux_delete,
471     dpif_linux_get_stats,
472     dpif_linux_get_drop_frags,
473     dpif_linux_set_drop_frags,
474     dpif_linux_port_add,
475     dpif_linux_port_del,
476     dpif_linux_port_query_by_number,
477     dpif_linux_port_query_by_name,
478     dpif_linux_port_list,
479     dpif_linux_port_poll,
480     dpif_linux_port_poll_wait,
481     dpif_linux_port_group_get,
482     dpif_linux_port_group_set,
483     dpif_linux_flow_get,
484     dpif_linux_flow_put,
485     dpif_linux_flow_del,
486     dpif_linux_flow_flush,
487     dpif_linux_flow_list,
488     dpif_linux_execute,
489     dpif_linux_recv_get_mask,
490     dpif_linux_recv_set_mask,
491     dpif_linux_get_sflow_probability,
492     dpif_linux_set_sflow_probability,
493     dpif_linux_recv,
494     dpif_linux_recv_wait,
495 };
496 \f
497 static int get_openvswitch_major(void);
498 static int get_major(const char *target);
499
500 static int
501 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
502 {
503     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
504     return ioctl(dpif->fd, cmd, arg) ? errno : 0;
505 }
506
507 static int
508 lookup_minor(const char *name, int *minorp)
509 {
510     struct ethtool_drvinfo drvinfo;
511     int minor, port_no;
512     struct ifreq ifr;
513     int error;
514     int sock;
515
516     sock = socket(AF_INET, SOCK_DGRAM, 0);
517     if (sock < 0) {
518         VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno));
519         error = errno;
520         goto error;
521     }
522
523     memset(&ifr, 0, sizeof ifr);
524     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
525     ifr.ifr_data = (caddr_t) &drvinfo;
526
527     memset(&drvinfo, 0, sizeof drvinfo);
528     drvinfo.cmd = ETHTOOL_GDRVINFO;
529     if (ioctl(sock, SIOCETHTOOL, &ifr)) {
530         VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
531         error = errno;
532         goto error_close_sock;
533     }
534
535     if (strcmp(drvinfo.driver, "openvswitch")) {
536         VLOG_WARN("%s is not an openvswitch device", name);
537         error = EOPNOTSUPP;
538         goto error_close_sock;
539     }
540
541     if (sscanf(drvinfo.bus_info, "%d.%d", &minor, &port_no) != 2) {
542         VLOG_WARN("%s ethtool bus_info has unexpected format", name);
543         error = EPROTOTYPE;
544         goto error_close_sock;
545     } else if (port_no != ODPP_LOCAL) {
546         /* This is an Open vSwitch device but not the local port.  We
547          * intentionally support only using the name of the local port as the
548          * name of a datapath; otherwise, it would be too difficult to
549          * enumerate all the names of a datapath. */
550         error = EOPNOTSUPP;
551         goto error_close_sock;
552     }
553
554     *minorp = minor;
555     close(sock);
556     return 0;
557
558 error_close_sock:
559     close(sock);
560 error:
561     return error;
562 }
563
564 static int
565 make_openvswitch_device(int minor, char **fnp)
566 {
567     const char dirname[] = "/dev/net";
568     int major;
569     dev_t dev;
570     struct stat s;
571     char fn[128];
572
573     *fnp = NULL;
574
575     major = get_openvswitch_major();
576     if (major < 0) {
577         return -major;
578     }
579     dev = makedev(major, minor);
580
581     sprintf(fn, "%s/dp%d", dirname, minor);
582     if (!stat(fn, &s)) {
583         if (!S_ISCHR(s.st_mode)) {
584             VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
585                          fn);
586         } else if (s.st_rdev != dev) {
587             VLOG_WARN_RL(&error_rl,
588                          "%s is device %u:%u but should be %u:%u, fixing",
589                          fn, major(s.st_rdev), minor(s.st_rdev),
590                          major(dev), minor(dev));
591         } else {
592             goto success;
593         }
594         if (unlink(fn)) {
595             VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
596                          fn, strerror(errno));
597             return errno;
598         }
599     } else if (errno == ENOENT) {
600         if (stat(dirname, &s)) {
601             if (errno == ENOENT) {
602                 if (mkdir(dirname, 0755)) {
603                     VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
604                                  dirname, strerror(errno));
605                     return errno;
606                 }
607             } else {
608                 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
609                              dirname, strerror(errno));
610                 return errno;
611             }
612         }
613     } else {
614         VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
615         return errno;
616     }
617
618     /* The device needs to be created. */
619     if (mknod(fn, S_IFCHR | 0700, dev)) {
620         VLOG_WARN_RL(&error_rl,
621                      "%s: creating character device %u:%u failed (%s)",
622                      fn, major(dev), minor(dev), strerror(errno));
623         return errno;
624     }
625
626 success:
627     *fnp = xstrdup(fn);
628     return 0;
629 }
630
631 /* Return the major device number of the Open vSwitch device.  If it
632  * cannot be determined, a negative errno is returned. */
633 static int
634 get_openvswitch_major(void)
635 {
636     static int openvswitch_major = -1;
637     if (openvswitch_major < 0) {
638         openvswitch_major = get_major("openvswitch");
639     }
640     return openvswitch_major;
641 }
642
643 static int
644 get_major(const char *target)
645 {
646     const char fn[] = "/proc/devices";
647     char line[128];
648     FILE *file;
649     int ln;
650
651     file = fopen(fn, "r");
652     if (!file) {
653         VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
654         return -errno;
655     }
656
657     for (ln = 1; fgets(line, sizeof line, file); ln++) {
658         char name[64];
659         int major;
660
661         if (!strncmp(line, "Character", 9) || line[0] == '\0') {
662             /* Nothing to do. */
663         } else if (!strncmp(line, "Block", 5)) {
664             /* We only want character devices, so skip the rest of the file. */
665             break;
666         } else if (sscanf(line, "%d %63s", &major, name)) {
667             if (!strcmp(name, target)) {
668                 fclose(file);
669                 return major;
670             }
671         } else {
672             static bool warned;
673             if (!warned) {
674                 VLOG_WARN("%s:%d: syntax error", fn, ln);
675             }
676             warned = true;
677         }
678     }
679
680     VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
681     return -ENODEV;
682 }
683
684 static int
685 finish_open(struct dpif *dpif_, const char *local_ifname)
686 {
687     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
688     dpif->local_ifname = xstrdup(local_ifname);
689     dpif->local_ifindex = if_nametoindex(local_ifname);
690     if (!dpif->local_ifindex) {
691         int error = errno;
692         dpif_uninit(dpif_, true);
693         VLOG_WARN("could not get ifindex of %s device: %s",
694                   local_ifname, strerror(errno));
695         return error;
696     }
697     return 0;
698 }
699
700 static int
701 create_minor(const char *name, int minor, struct dpif **dpifp)
702 {
703     int error = open_minor(minor, dpifp);
704     if (!error) {
705         error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
706         if (!error) {
707             error = finish_open(*dpifp, name);
708         } else {
709             dpif_uninit(*dpifp, true);
710         }
711     }
712     return error;
713 }
714
715 static int
716 open_minor(int minor, struct dpif **dpifp)
717 {
718     int error;
719     char *fn;
720     int fd;
721
722     error = make_openvswitch_device(minor, &fn);
723     if (error) {
724         return error;
725     }
726
727     fd = open(fn, O_RDONLY | O_NONBLOCK);
728     if (fd >= 0) {
729         struct dpif_linux *dpif = xmalloc(sizeof *dpif);
730         error = rtnetlink_notifier_register(&dpif->port_notifier,
731                                            dpif_linux_port_changed, dpif);
732         if (!error) {
733             char *name;
734
735             name = xasprintf("dp%d", minor);
736             dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
737             free(name);
738
739             dpif->fd = fd;
740             dpif->local_ifname = NULL;
741             dpif->minor = minor;
742             dpif->local_ifindex = 0;
743             svec_init(&dpif->changed_ports);
744             dpif->change_error = false;
745             *dpifp = &dpif->dpif;
746         } else {
747             free(dpif);
748         }
749     } else {
750         error = errno;
751         VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
752     }
753     free(fn);
754
755     return error;
756 }
757
758 static void
759 dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
760 {
761     struct dpif_linux *dpif = dpif_;
762
763     if (change) {
764         if (change->master_ifindex == dpif->local_ifindex
765             && (change->nlmsg_type == RTM_NEWLINK
766                 || change->nlmsg_type == RTM_DELLINK))
767         {
768             /* Our datapath changed, either adding a new port or deleting an
769              * existing one. */
770             if (!svec_contains(&dpif->changed_ports, change->ifname)) {
771                 svec_add(&dpif->changed_ports, change->ifname);
772                 svec_sort(&dpif->changed_ports);
773             }
774         }
775     } else {
776         dpif->change_error = true;
777     }
778 }