const struct mf_subfield *fields,
size_t n_fields);
-static void oftable_remove_rule(struct rule *rule) OVS_RELEASES(rule->rwlock);
+static void oftable_remove_rule(struct rule *rule) OVS_RELEASES(rule->mutex);
static void oftable_remove_rule__(struct ofproto *ofproto,
struct classifier *cls, struct rule *rule)
- OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->rwlock);
+ OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->mutex);
static void oftable_insert_rule(struct rule *);
/* A set of rules within a single OpenFlow table (oftable) that have the same
};
static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep)
- OVS_TRY_WRLOCK(true, (*rulep)->rwlock);
+ OVS_TRY_WRLOCK(true, (*rulep)->mutex);
static void ofproto_evict(struct ofproto *);
static uint32_t rule_eviction_priority(struct rule *);
static void eviction_group_add_rule(struct rule *);
const struct rule_collection *);
static void delete_flow__(struct rule *rule, struct ofopgroup *,
enum ofp_flow_removed_reason)
- OVS_RELEASES(rule->rwlock);
+ OVS_RELEASES(rule->mutex);
static enum ofperr add_group(struct ofproto *, struct ofputil_group_mod *);
static bool handle_openflow(struct ofconn *, const struct ofpbuf *);
static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *,
group = ofopgroup_create_unattached(ofproto);
ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE);
- ovs_rwlock_wrlock(&rule->rwlock);
+ ovs_mutex_lock(&rule->mutex);
oftable_remove_rule__(ofproto, cls, rule);
ofproto->ofproto_class->rule_delete(rule);
ofopgroup_submit(group);
}
}
+static bool
+any_pending_ops(const struct ofproto *p)
+{
+ return !list_is_empty(&p->pending);
+}
+
int
ofproto_run(struct ofproto *p)
{
case S_EVICT:
connmgr_run(p->connmgr, NULL);
ofproto_evict(p);
- if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
+ if (!any_pending_ops(p)) {
p->state = S_OPENFLOW;
}
break;
case S_FLUSH:
connmgr_run(p->connmgr, NULL);
ofproto_flush__(p);
- if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
+ if (!any_pending_ops(p)) {
connmgr_flushed(p->connmgr);
p->state = S_OPENFLOW;
}
case S_EVICT:
case S_FLUSH:
connmgr_wait(p->connmgr, false);
- if (list_is_empty(&p->pending) && hmap_is_empty(&p->deletions)) {
+ if (!any_pending_ops(p)) {
poll_immediate_wake();
}
break;
rule = rule_from_cls_rule(classifier_find_match_exactly(
&ofproto->tables[0].cls, match, priority));
if (rule) {
- ovs_rwlock_rdlock(&rule->rwlock);
+ ovs_mutex_lock(&rule->mutex);
must_add = !ofpacts_equal(rule->actions->ofpacts,
rule->actions->ofpacts_len,
ofpacts, ofpacts_len);
- ovs_rwlock_unlock(&rule->rwlock);
+ ovs_mutex_unlock(&rule->mutex);
} else {
must_add = true;
}
static void
ofproto_rule_destroy__(struct rule *rule)
{
- cls_rule_destroy(&rule->cr);
+ cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr));
rule_actions_unref(rule->actions);
- ovs_mutex_destroy(&rule->timeout_mutex);
- ovs_rwlock_destroy(&rule->rwlock);
+ ovs_mutex_destroy(&rule->mutex);
rule->ofproto->ofproto_class->rule_dealloc(rule);
}
/* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action
* that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */
-bool
+static bool
ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port)
{
return (port == OFPP_ANY
}
/* Returns true if 'rule' has group and equals group_id. */
-bool
+static bool
ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id)
{
return (group_id == OFPG11_ANY
* Rules with priority higher than UINT16_MAX are set up by ofproto itself
* (e.g. by in-band control) and are intentionally hidden from the
* controller. */
-bool
+static bool
ofproto_rule_is_hidden(const struct rule *rule)
{
return rule->cr.priority > UINT16_MAX;
ovs_mutex_lock(&ofproto_mutex);
cookies_remove(ofproto, rule);
- ovs_rwlock_wrlock(&rule->rwlock);
+ ovs_mutex_lock(&rule->mutex);
rule->flow_cookie = new_cookie;
- ovs_rwlock_unlock(&rule->rwlock);
+ ovs_mutex_unlock(&rule->mutex);
cookies_insert(ofproto, rule);
ovs_mutex_unlock(&ofproto_mutex);
fs.ofpacts = rule->actions->ofpacts;
fs.ofpacts_len = rule->actions->ofpacts_len;
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_lock(&rule->mutex);
fs.idle_timeout = rule->idle_timeout;
fs.hard_timeout = rule->hard_timeout;
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
fs.flags = rule->flags;
return false;
}
-static enum ofperr
-evict_rule_from_table(struct ofproto *ofproto, struct oftable *table)
+static bool
+should_evict_a_rule(struct oftable *table, unsigned int extra_space)
{
- struct rule *rule;
- size_t n_rules;
+ size_t count;
ovs_rwlock_rdlock(&table->cls.rwlock);
- n_rules = classifier_count(&table->cls);
+ count = classifier_count(&table->cls);
ovs_rwlock_unlock(&table->cls.rwlock);
- if (n_rules < table->max_flows) {
- return 0;
- } else if (!choose_rule_to_evict(table, &rule)) {
- return OFPERR_OFPFMFC_TABLE_FULL;
- } else if (rule->pending) {
- ovs_rwlock_unlock(&rule->rwlock);
- return OFPROTO_POSTPONE;
- } else {
- struct ofopgroup *group;
+ return count + extra_space > table->max_flows;
+}
- group = ofopgroup_create_unattached(ofproto);
- delete_flow__(rule, group, OFPRR_EVICTION);
- ofopgroup_submit(group);
+static enum ofperr
+evict_rules_from_table(struct ofproto *ofproto, struct oftable *table,
+ unsigned int extra_space)
+{
+ while (should_evict_a_rule(table, extra_space)) {
+ struct rule *rule;
- return 0;
+ if (!choose_rule_to_evict(table, &rule)) {
+ return OFPERR_OFPFMFC_TABLE_FULL;
+ } else if (rule->pending) {
+ ovs_mutex_unlock(&rule->mutex);
+ return OFPROTO_POSTPONE;
+ } else {
+ struct ofopgroup *group = ofopgroup_create_unattached(ofproto);
+ ofoperation_create(group, rule,
+ OFOPERATION_DELETE, OFPRR_EVICTION);
+ oftable_remove_rule(rule);
+ ofproto->ofproto_class->rule_delete(rule);
+ }
}
+
+ return 0;
}
/* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
}
/* If necessary, evict an existing rule to clear out space. */
- error = evict_rule_from_table(ofproto, table);
+ error = evict_rules_from_table(ofproto, table, 1);
if (error) {
cls_rule_destroy(&cr);
return error;
}
/* Initialize base state. */
- rule->ofproto = ofproto;
- cls_rule_move(&rule->cr, &cr);
+ *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto;
+ cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), &cr);
atomic_init(&rule->ref_count, 1);
rule->pending = NULL;
rule->flow_cookie = fm->new_cookie;
rule->created = rule->modified = rule->used = time_msec();
- ovs_mutex_init(&rule->timeout_mutex);
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_init(&rule->mutex);
+ ovs_mutex_lock(&rule->mutex);
rule->idle_timeout = fm->idle_timeout;
rule->hard_timeout = fm->hard_timeout;
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
- rule->table_id = table - ofproto->tables;
+ *CONST_CAST(uint8_t *, &rule->table_id) = table - ofproto->tables;
rule->flags = fm->flags & OFPUTIL_FF_STATE;
rule->actions = rule_actions_create(fm->ofpacts, fm->ofpacts_len);
list_init(&rule->meter_list_node);
rule->monitor_flags = 0;
rule->add_seqno = 0;
rule->modify_seqno = 0;
- ovs_rwlock_init(&rule->rwlock);
/* Construct rule, initializing derived state. */
error = ofproto->ofproto_class->rule_construct(rule);
ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie);
}
if (type == OFOPERATION_REPLACE) {
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_lock(&rule->mutex);
rule->idle_timeout = fm->idle_timeout;
rule->hard_timeout = fm->hard_timeout;
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
rule->flags = fm->flags & OFPUTIL_FF_STATE;
if (fm->idle_timeout || fm->hard_timeout) {
op->actions = rule->actions;
new_actions = rule_actions_create(fm->ofpacts, fm->ofpacts_len);
- ovs_rwlock_wrlock(&rule->rwlock);
+ ovs_mutex_lock(&rule->mutex);
rule->actions = new_actions;
- ovs_rwlock_unlock(&rule->rwlock);
+ ovs_mutex_unlock(&rule->mutex);
rule->ofproto->ofproto_class->rule_modify_actions(rule,
reset_counters);
group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX);
for (i = 0; i < rules->n; i++) {
struct rule *rule = rules->rules[i];
- ovs_rwlock_wrlock(&rule->rwlock);
+ ovs_mutex_lock(&rule->mutex);
delete_flow__(rule, group, reason);
}
ofopgroup_submit(group);
fr.table_id = rule->table_id;
calc_duration(rule->created, time_msec(),
&fr.duration_sec, &fr.duration_nsec);
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_lock(&rule->mutex);
fr.idle_timeout = rule->idle_timeout;
fr.hard_timeout = rule->hard_timeout;
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count,
&fr.byte_count);
void
ofproto_rule_reduce_timeouts(struct rule *rule,
uint16_t idle_timeout, uint16_t hard_timeout)
- OVS_EXCLUDED(ofproto_mutex, rule->timeout_mutex)
+ OVS_EXCLUDED(ofproto_mutex, rule->mutex)
{
if (!idle_timeout && !hard_timeout) {
return;
}
ovs_mutex_unlock(&ofproto_mutex);
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_lock(&rule->mutex);
reduce_timeout(idle_timeout, &rule->idle_timeout);
reduce_timeout(hard_timeout, &rule->hard_timeout);
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
}
\f
static enum ofperr
fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD)
? NXFME_ADDED : NXFME_MODIFIED);
fu.reason = 0;
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_lock(&rule->mutex);
fu.idle_timeout = rule->idle_timeout;
fu.hard_timeout = rule->hard_timeout;
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
fu.table_id = rule->table_id;
fu.cookie = rule->flow_cookie;
minimatch_expand(&rule->cr.match, &match);
}
}
} else {
- ovs_rwlock_wrlock(&rule->rwlock);
+ ovs_mutex_lock(&rule->mutex);
oftable_remove_rule(rule);
ofproto_rule_unref(rule);
}
}
} else {
ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie);
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_lock(&rule->mutex);
rule->idle_timeout = op->idle_timeout;
rule->hard_timeout = op->hard_timeout;
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
if (op->actions) {
struct rule_actions *old_actions;
- ovs_rwlock_wrlock(&rule->rwlock);
+ ovs_mutex_lock(&rule->mutex);
old_actions = rule->actions;
rule->actions = op->actions;
- ovs_rwlock_unlock(&rule->rwlock);
+ ovs_mutex_unlock(&rule->mutex);
op->actions = NULL;
rule_actions_unref(old_actions);
op->type = type;
op->reason = reason;
op->flow_cookie = rule->flow_cookie;
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_lock(&rule->mutex);
op->idle_timeout = rule->idle_timeout;
op->hard_timeout = rule->hard_timeout;
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
op->flags = rule->flags;
group->n_running++;
struct rule *rule;
HEAP_FOR_EACH (rule, evg_node, &evg->rules) {
- if (!ovs_rwlock_trywrlock(&rule->rwlock)) {
+ if (!ovs_mutex_trylock(&rule->mutex)) {
*rulep = rule;
return true;
}
static void
ofproto_evict(struct ofproto *ofproto)
{
- struct ofopgroup *group;
struct oftable *table;
- group = ofopgroup_create_unattached(ofproto);
OFPROTO_FOR_EACH_TABLE (table, ofproto) {
- while (table->eviction_fields) {
- struct rule *rule;
- size_t n_rules;
-
- ovs_rwlock_rdlock(&table->cls.rwlock);
- n_rules = classifier_count(&table->cls);
- ovs_rwlock_unlock(&table->cls.rwlock);
-
- if (n_rules <= table->max_flows) {
- break;
- }
-
- if (!choose_rule_to_evict(table, &rule)) {
- break;
- }
-
- if (rule->pending) {
- ovs_rwlock_unlock(&rule->rwlock);
- break;
- }
-
- ofoperation_create(group, rule,
- OFOPERATION_DELETE, OFPRR_EVICTION);
- oftable_remove_rule(rule);
- ofproto->ofproto_class->rule_delete(rule);
- }
+ evict_rules_from_table(ofproto, table, 0);
}
- ofopgroup_submit(group);
}
\f
/* Eviction groups. */
uint32_t expiration_offset;
/* Calculate time of expiration. */
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_lock(&rule->mutex);
hard_expiration = (rule->hard_timeout
? rule->modified + rule->hard_timeout * 1000
: LLONG_MAX);
? rule->used + rule->idle_timeout * 1000
: LLONG_MAX);
expiration = MIN(hard_expiration, idle_expiration);
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
if (expiration == LLONG_MAX) {
return 0;
}
struct oftable *table = &ofproto->tables[rule->table_id];
bool has_timeout;
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_lock(&rule->mutex);
has_timeout = rule->hard_timeout || rule->idle_timeout;
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
if (table->eviction_fields && has_timeout) {
struct eviction_group *evg;
static void
oftable_remove_rule__(struct ofproto *ofproto, struct classifier *cls,
struct rule *rule)
- OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->rwlock)
+ OVS_REQ_WRLOCK(cls->rwlock) OVS_RELEASES(rule->mutex)
{
- classifier_remove(cls, &rule->cr);
+ classifier_remove(cls, CONST_CAST(struct cls_rule *, &rule->cr));
ovs_mutex_lock(&ofproto_mutex);
cookies_remove(ofproto, rule);
list_remove(&rule->meter_list_node);
list_init(&rule->meter_list_node);
}
- ovs_rwlock_unlock(&rule->rwlock);
+ ovs_mutex_unlock(&rule->mutex);
}
static void
struct oftable *table = &ofproto->tables[rule->table_id];
bool may_expire;
- ovs_mutex_lock(&rule->timeout_mutex);
+ ovs_mutex_lock(&rule->mutex);
may_expire = rule->hard_timeout || rule->idle_timeout;
- ovs_mutex_unlock(&rule->timeout_mutex);
+ ovs_mutex_unlock(&rule->mutex);
if (may_expire) {
ovs_mutex_lock(&ofproto_mutex);
list_insert(&meter->rules, &rule->meter_list_node);
}
ovs_rwlock_wrlock(&table->cls.rwlock);
- classifier_insert(&table->cls, &rule->cr);
+ classifier_insert(&table->cls, CONST_CAST(struct cls_rule *, &rule->cr));
ovs_rwlock_unlock(&table->cls.rwlock);
eviction_group_add_rule(rule);
}