+/* Returns the vacancy of 'oftable', a number that ranges from 0 (if the table
+ * is full) to 100 (if the table is empty).
+ *
+ * A table without a limit on flows is considered to be empty. */
+static uint8_t
+oftable_vacancy(const struct oftable *t)
+{
+ return (!t->max_flows ? 100
+ : t->n_flows >= t->max_flows ? 0
+ : (t->max_flows - t->n_flows) * 100.0 / t->max_flows);
+}
+
+static void
+query_table_desc__(struct ofputil_table_desc *td,
+ struct ofproto *ofproto, uint8_t table_id)
+{
+ const struct oftable *t = &ofproto->tables[table_id];
+
+ td->table_id = table_id;
+ td->eviction = (t->eviction & EVICTION_OPENFLOW
+ ? OFPUTIL_TABLE_EVICTION_ON
+ : OFPUTIL_TABLE_EVICTION_OFF);
+ td->eviction_flags = OFPROTO_EVICTION_FLAGS;
+ td->vacancy = (t->vacancy_event
+ ? OFPUTIL_TABLE_VACANCY_ON
+ : OFPUTIL_TABLE_VACANCY_OFF);
+ td->table_vacancy.vacancy_down = t->vacancy_down;
+ td->table_vacancy.vacancy_up = t->vacancy_up;
+ td->table_vacancy.vacancy = oftable_vacancy(t);
+}
+
+/* This function queries the database for dumping table-desc. */
+static void
+query_tables_desc(struct ofproto *ofproto, struct ofputil_table_desc **descp)
+{
+ struct ofputil_table_desc *table_desc;
+ size_t i;
+
+ table_desc = *descp = xcalloc(ofproto->n_tables, sizeof *table_desc);
+ for (i = 0; i < ofproto->n_tables; i++) {
+ struct ofputil_table_desc *td = &table_desc[i];
+ query_table_desc__(td, ofproto, i);
+ }
+}
+
+/* Function to handle dump-table-desc request. */
+static enum ofperr
+handle_table_desc_request(struct ofconn *ofconn,
+ const struct ofp_header *request)
+{
+ struct ofproto *ofproto = ofconn_get_ofproto(ofconn);
+ struct ofputil_table_desc *table_desc;
+ struct ovs_list replies;
+ size_t i;
+
+ query_tables_desc(ofproto, &table_desc);
+ ofpmp_init(&replies, request);
+ for (i = 0; i < ofproto->n_tables; i++) {
+ if (!(ofproto->tables[i].flags & OFTABLE_HIDDEN)) {
+ ofputil_append_table_desc_reply(&table_desc[i], &replies,
+ request->version);
+ }
+ }
+ ofconn_send_replies(ofconn, &replies);
+ free(table_desc);
+ return 0;
+}
+
+/* This function determines and sends the vacancy event, based on the value
+ * of current vacancy and threshold vacancy. If the current vacancy is less
+ * than or equal to vacancy_down, vacancy up events must be enabled, and when
+ * the current vacancy is greater or equal to vacancy_up, vacancy down events
+ * must be enabled. */
+static void
+send_table_status(struct ofproto *ofproto, uint8_t table_id)
+{
+ struct oftable *t = &ofproto->tables[table_id];
+ if (!t->vacancy_event) {
+ return;
+ }
+
+ uint8_t vacancy = oftable_vacancy(t);
+ enum ofp14_table_reason event;
+ if (vacancy < t->vacancy_down) {
+ event = OFPTR_VACANCY_DOWN;
+ } else if (vacancy > t->vacancy_up) {
+ event = OFPTR_VACANCY_UP;
+ } else {
+ return;
+ }
+
+ if (event == t->vacancy_event) {
+ struct ofputil_table_desc td;
+ query_table_desc__(&td, ofproto, table_id);
+ connmgr_send_table_status(ofproto->connmgr, &td, event);
+
+ t->vacancy_event = (event == OFPTR_VACANCY_DOWN
+ ? OFPTR_VACANCY_UP
+ : OFPTR_VACANCY_DOWN);
+ }
+}
+