+/* Type functions. */
+
+static int
+type_run(const char *type)
+{
+ struct dpif_backer *backer;
+ char *devname;
+ int error;
+
+ backer = shash_find_data(&all_dpif_backers, type);
+ if (!backer) {
+ /* This is not necessarily a problem, since backers are only
+ * created on demand. */
+ return 0;
+ }
+
+ dpif_run(backer->dpif);
+
+ if (timer_expired(&backer->next_expiration)) {
+ int delay = expire(backer);
+ timer_set_duration(&backer->next_expiration, delay);
+ }
+
+ /* Check for port changes in the dpif. */
+ while ((error = dpif_port_poll(backer->dpif, &devname)) == 0) {
+ struct ofproto_dpif *ofproto = NULL;
+ struct dpif_port port;
+
+ /* Don't report on the datapath's device. */
+ if (!strcmp(devname, dpif_base_name(backer->dpif))) {
+ continue;
+ }
+
+ HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
+ &all_ofproto_dpifs) {
+ if (sset_contains(&ofproto->ports, devname)) {
+ break;
+ }
+ }
+
+ if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
+ /* The port was removed. If we know the datapath,
+ * report it through poll_set(). If we don't, it may be
+ * notifying us of a removal we initiated, so ignore it.
+ * If there's a pending ENOBUFS, let it stand, since
+ * everything will be reevaluated. */
+ if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
+ sset_add(&ofproto->port_poll_set, devname);
+ ofproto->port_poll_errno = 0;
+ }
+ dpif_port_destroy(&port);
+ } else if (!ofproto) {
+ /* The port was added, but we don't know with which
+ * ofproto we should associate it. Delete it. */
+ dpif_port_del(backer->dpif, port.port_no);
+ }
+
+ free(devname);
+ }
+
+ if (error != EAGAIN) {
+ struct ofproto_dpif *ofproto;
+
+ /* There was some sort of error, so propagate it to all
+ * ofprotos that use this backer. */
+ HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
+ &all_ofproto_dpifs) {
+ if (ofproto->backer == backer) {
+ sset_clear(&ofproto->port_poll_set);
+ ofproto->port_poll_errno = error;
+ }
+ }
+ }
+
+ return 0;
+}
+
+static int
+type_run_fast(const char *type)
+{
+ struct dpif_backer *backer;
+ unsigned int work;
+
+ backer = shash_find_data(&all_dpif_backers, type);
+ if (!backer) {
+ /* This is not necessarily a problem, since backers are only
+ * created on demand. */
+ return 0;
+ }
+
+ /* Handle one or more batches of upcalls, until there's nothing left to do
+ * or until we do a fixed total amount of work.
+ *
+ * We do work in batches because it can be much cheaper to set up a number
+ * of flows and fire off their patches all at once. We do multiple batches
+ * because in some cases handling a packet can cause another packet to be
+ * queued almost immediately as part of the return flow. Both
+ * optimizations can make major improvements on some benchmarks and
+ * presumably for real traffic as well. */
+ work = 0;
+ while (work < FLOW_MISS_MAX_BATCH) {
+ int retval = handle_upcalls(backer, FLOW_MISS_MAX_BATCH - work);
+ if (retval <= 0) {
+ return -retval;
+ }
+ work += retval;
+ }
+
+ return 0;
+}
+
+static void
+type_wait(const char *type)
+{
+ struct dpif_backer *backer;
+
+ backer = shash_find_data(&all_dpif_backers, type);
+ if (!backer) {
+ /* This is not necessarily a problem, since backers are only
+ * created on demand. */
+ return;
+ }
+
+ timer_wait(&backer->next_expiration);
+}
+\f