+/* Synchronizes 'dpif->channels' with the set of vports currently in 'dpif' in
+ * the kernel, by adding a new channel for any kernel vport that lacks one and
+ * deleting any channels that have no backing kernel vports. */
+static int
+dpif_linux_refresh_channels(struct dpif *dpif_)
+{
+ struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+ unsigned long int *keep_channels;
+ struct dpif_linux_vport vport;
+ size_t keep_channels_nbits;
+ struct nl_dump dump;
+ int retval = 0;
+ size_t i;
+
+ /* To start with, we need an epoll fd. */
+ if (dpif->epoll_fd < 0) {
+ dpif->epoll_fd = epoll_create(10);
+ if (dpif->epoll_fd < 0) {
+ return errno;
+ }
+ }
+
+ keep_channels_nbits = dpif->uc_array_size;
+ keep_channels = bitmap_allocate(keep_channels_nbits);
+
+ dpif->n_events = dpif->event_offset = 0;
+
+ dpif_linux_port_dump_start__(dpif_, &dump);
+ while (!dpif_linux_port_dump_next__(dpif_, &dump, &vport)) {
+ uint32_t port_no = odp_to_u32(vport.port_no);
+ struct nl_sock *sock = (port_no < dpif->uc_array_size
+ ? dpif->channels[port_no].sock
+ : NULL);
+ bool new_sock = !sock;
+ int error;
+
+ if (new_sock) {
+ error = nl_sock_create(NETLINK_GENERIC, &sock);
+ if (error) {
+ retval = error;
+ goto error;
+ }
+ }
+
+ /* Configure the vport to deliver misses to 'sock'. */
+ if (!vport.upcall_pid || *vport.upcall_pid != nl_sock_pid(sock)) {
+ uint32_t upcall_pid = nl_sock_pid(sock);
+ struct dpif_linux_vport vport_request;
+
+ dpif_linux_vport_init(&vport_request);
+ vport_request.cmd = OVS_VPORT_CMD_SET;
+ vport_request.dp_ifindex = dpif->dp_ifindex;
+ vport_request.port_no = vport.port_no;
+ vport_request.upcall_pid = &upcall_pid;
+ error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
+ if (!error) {
+ VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
+ dpif_name(&dpif->dpif), vport_request.port_no,
+ upcall_pid);
+ } else {
+ VLOG_WARN_RL(&error_rl,
+ "%s: failed to set upcall pid on port: %s",
+ dpif_name(&dpif->dpif), ovs_strerror(error));
+
+ if (error != ENODEV && error != ENOENT) {
+ retval = error;
+ } else {
+ /* The vport isn't really there, even though the dump says
+ * it is. Probably we just hit a race after a port
+ * disappeared. */
+ }
+ goto error;
+ }
+ }