2012-09-07 10:07:03 -07:00
|
|
|
/* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
|
2009-11-04 15:11:44 -08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "trigger.h"
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
2016-07-12 16:37:34 -05:00
|
|
|
#include "openvswitch/json.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
#include "jsonrpc.h"
|
|
|
|
#include "ovsdb.h"
|
|
|
|
#include "poll-loop.h"
|
2011-07-26 10:17:36 -07:00
|
|
|
#include "server.h"
|
2009-11-04 15:11:44 -08:00
|
|
|
|
2011-07-26 10:17:36 -07:00
|
|
|
static bool ovsdb_trigger_try(struct ovsdb_trigger *, long long int now);
|
2009-11-04 15:11:44 -08:00
|
|
|
static void ovsdb_trigger_complete(struct ovsdb_trigger *);
|
|
|
|
|
|
|
|
void
|
2012-09-07 10:07:03 -07:00
|
|
|
ovsdb_trigger_init(struct ovsdb_session *session, struct ovsdb *db,
|
2011-07-26 10:17:36 -07:00
|
|
|
struct ovsdb_trigger *trigger,
|
|
|
|
struct json *request, long long int now)
|
2009-11-04 15:11:44 -08:00
|
|
|
{
|
2011-07-26 10:17:36 -07:00
|
|
|
trigger->session = session;
|
2012-09-07 10:07:03 -07:00
|
|
|
trigger->db = db;
|
2016-03-25 14:10:22 -07:00
|
|
|
ovs_list_push_back(&trigger->db->triggers, &trigger->node);
|
2009-11-04 15:11:44 -08:00
|
|
|
trigger->request = request;
|
|
|
|
trigger->result = NULL;
|
|
|
|
trigger->created = now;
|
|
|
|
trigger->timeout_msec = LLONG_MAX;
|
2011-07-26 10:17:36 -07:00
|
|
|
ovsdb_trigger_try(trigger, now);
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ovsdb_trigger_destroy(struct ovsdb_trigger *trigger)
|
|
|
|
{
|
2016-03-25 14:10:22 -07:00
|
|
|
ovs_list_remove(&trigger->node);
|
2009-11-04 15:11:44 -08:00
|
|
|
json_destroy(trigger->request);
|
|
|
|
json_destroy(trigger->result);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ovsdb_trigger_is_complete(const struct ovsdb_trigger *trigger)
|
|
|
|
{
|
|
|
|
return trigger->result != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct json *
|
|
|
|
ovsdb_trigger_steal_result(struct ovsdb_trigger *trigger)
|
|
|
|
{
|
|
|
|
struct json *result = trigger->result;
|
|
|
|
trigger->result = NULL;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ovsdb_trigger_run(struct ovsdb *db, long long int now)
|
|
|
|
{
|
|
|
|
struct ovsdb_trigger *t, *next;
|
|
|
|
bool run_triggers;
|
|
|
|
|
|
|
|
run_triggers = db->run_triggers;
|
|
|
|
db->run_triggers = false;
|
2010-09-17 10:33:10 -07:00
|
|
|
LIST_FOR_EACH_SAFE (t, next, node, &db->triggers) {
|
2009-11-04 15:11:44 -08:00
|
|
|
if (run_triggers || now - t->created >= t->timeout_msec) {
|
2011-07-26 10:17:36 -07:00
|
|
|
ovsdb_trigger_try(t, now);
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ovsdb_trigger_wait(struct ovsdb *db, long long int now)
|
|
|
|
{
|
|
|
|
if (db->run_triggers) {
|
|
|
|
poll_immediate_wake();
|
|
|
|
} else {
|
|
|
|
long long int deadline = LLONG_MAX;
|
|
|
|
struct ovsdb_trigger *t;
|
|
|
|
|
2010-09-17 10:33:10 -07:00
|
|
|
LIST_FOR_EACH (t, node, &db->triggers) {
|
2009-11-04 15:11:44 -08:00
|
|
|
if (t->created < LLONG_MAX - t->timeout_msec) {
|
|
|
|
long long int t_deadline = t->created + t->timeout_msec;
|
|
|
|
if (deadline > t_deadline) {
|
|
|
|
deadline = t_deadline;
|
|
|
|
if (now >= deadline) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deadline < LLONG_MAX) {
|
2010-05-12 12:53:07 -07:00
|
|
|
poll_timer_wait_until(deadline);
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2011-07-26 10:17:36 -07:00
|
|
|
ovsdb_trigger_try(struct ovsdb_trigger *t, long long int now)
|
2009-11-04 15:11:44 -08:00
|
|
|
{
|
2012-09-07 10:07:03 -07:00
|
|
|
t->result = ovsdb_execute(t->db, t->session,
|
2011-07-26 10:24:17 -07:00
|
|
|
t->request, now - t->created, &t->timeout_msec);
|
2009-11-04 15:11:44 -08:00
|
|
|
if (t->result) {
|
|
|
|
ovsdb_trigger_complete(t);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ovsdb_trigger_complete(struct ovsdb_trigger *t)
|
|
|
|
{
|
2012-11-06 13:14:55 -08:00
|
|
|
ovs_assert(t->result != NULL);
|
2016-03-25 14:10:22 -07:00
|
|
|
ovs_list_remove(&t->node);
|
|
|
|
ovs_list_push_back(&t->session->completions, &t->node);
|
2009-11-04 15:11:44 -08:00
|
|
|
}
|