Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pathd pcep pceplib timers sockets #13

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pathd/path_pcep.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,10 @@ int pcep_module_late_init(struct thread_master *tm)

struct frr_pthread *fpt;

if (pcep_lib_initialize())
if (pcep_ctrl_initialize(tm, &fpt, pcep_main_event_handler))
return 1;

if (pcep_ctrl_initialize(tm, &fpt, pcep_main_event_handler))
if (pcep_lib_initialize(fpt))
return 1;

pcep_g->master = tm;
Expand Down
130 changes: 106 additions & 24 deletions pathd/path_pcep_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "pathd/path_pcep.h"
#include "pathd/path_pcep_controller.h"
#include "pathd/path_pcep_pcc.h"
#include "pathd/path_pcep_lib.h"
#include "pathd/path_pcep_nb.h"
#include "pathd/path_pcep_debug.h"

Expand Down Expand Up @@ -74,17 +73,6 @@ struct pcep_main_event_data {
void *payload;
};

/* Timer handling data structures */

enum pcep_ctrl_timer_type { TM_POLL = 1, TM_RECONNECT_PCC };

struct pcep_ctrl_timer_data {
struct ctrl_state *ctrl_state;
enum pcep_ctrl_timer_type type;
int pcc_id;
void *payload;
};

/* Synchronous call arguments */

struct get_counters_args {
Expand All @@ -110,12 +98,21 @@ static int pcep_thread_get_counters_callback(struct thread *t);
static int pcep_thread_send_report_callback(struct thread *t);

/* Controller Thread Timer Handler */
static int schedule_thread(struct ctrl_state *ctrl_state, int pcc_id,
enum pcep_ctrl_timer_type type, uint32_t delay,
void *payload, struct thread **thread);
static int schedule_thread_timer(struct ctrl_state *ctrl_state, int pcc_id,
enum pcep_ctrl_timer_type type, uint32_t delay,
void *payload, struct thread **thread);
static int schedule_thread_timer_with_cb(struct ctrl_state *ctrl_state,
int pcc_id, enum pcep_ctrl_timer_type type, uint32_t delay,
void *payload, struct thread **thread, pcep_ctrl_thread_callback timer_cb);
static int pcep_thread_timer_handler(struct thread *thread);
static int pcep_thread_timer_poll(struct ctrl_state *ctrl_state);

/* Controller Thread Socket read/write Handler */
static int schedule_thread_socket(struct ctrl_state *ctrl_state, int pcc_id,
enum pcep_ctrl_socket_type type, bool is_read,
void *payload, int fd, struct thread **thread,
pcep_ctrl_thread_callback cb);

/* Controller Thread Event Handler */
static int send_to_thread(struct ctrl_state *ctrl_state, int pcc_id,
enum pcep_ctrl_event_type type, uint32_t sub_type,
Expand Down Expand Up @@ -316,10 +313,39 @@ void pcep_thread_schedule_reconnect(struct ctrl_state *ctrl_state, int pcc_id,
uint32_t delay = backoff_delay(MAX_RECONNECT_DELAY, 1, retry_count);
PCEP_DEBUG("Schedule reconnection in %us (retry %u)", delay,
retry_count);
schedule_thread(ctrl_state, pcc_id, TM_RECONNECT_PCC, delay, NULL,
thread);
schedule_thread_timer(ctrl_state, pcc_id, TM_RECONNECT_PCC, delay, NULL,
thread);
}

void pcep_thread_schedule_pceplib_timer(struct ctrl_state *ctrl_state,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that be moved to pathd_pcep_lib.c alongside all the pceplib specific functions ? Or is it just some old code ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but then we would need to make schedule_thread_timer_with_cb() and schedule_thread_timer() public and accessible from outside path_pcep_controller, and it seems like you wanted that encapsulated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, it is fine

int delay, void *payload, struct thread **thread,
pcep_ctrl_thread_callback timer_cb)
{
PCEP_DEBUG("Schedule pceplib timer for %us", delay);
schedule_thread_timer_with_cb(
ctrl_state, 0, TM_PCEPLIB_TIMER, delay, payload, thread,
timer_cb);
}

void pcep_thread_cancel_pceplib_timer(struct thread **thread)
{
PCEP_DEBUG("Cancel pceplib timer");

if (thread == NULL) {
return;
}

struct pcep_ctrl_timer_data *data = THREAD_ARG(*thread);
if (data != NULL) {
XFREE(MTYPE_PCEP, data);
}

if ((*thread)->master->owner == pthread_self()) {
thread_cancel(*thread);
} else {
thread_cancel_async((*thread)->master, thread, NULL);
}
}

/* ------------ Internal Functions Called From Controller Thread ------------ */

Expand Down Expand Up @@ -351,8 +377,8 @@ int pcep_thread_finish_event_handler(struct thread *thread)
void pcep_thread_schedule_poll(struct ctrl_state *ctrl_state)
{
assert(ctrl_state->t_poll == NULL);
schedule_thread(ctrl_state, 0, TM_POLL, POLL_INTERVAL, NULL,
&ctrl_state->t_poll);
schedule_thread_timer(ctrl_state, 0, TM_POLL, POLL_INTERVAL, NULL,
&ctrl_state->t_poll);
}

int pcep_thread_get_counters_callback(struct thread *t)
Expand Down Expand Up @@ -397,9 +423,10 @@ int pcep_thread_send_report_callback(struct thread *t)

/* ------------ Controller Thread Timer Handler ------------ */

int schedule_thread(struct ctrl_state *ctrl_state, int pcc_id,
enum pcep_ctrl_timer_type type, uint32_t delay,
void *payload, struct thread **thread)
int schedule_thread_timer_with_cb(struct ctrl_state *ctrl_state, int pcc_id,
enum pcep_ctrl_timer_type type, uint32_t delay,
void *payload, struct thread **thread,
pcep_ctrl_thread_callback timer_cb)
{
assert(thread != NULL);

Expand All @@ -411,12 +438,21 @@ int schedule_thread(struct ctrl_state *ctrl_state, int pcc_id,
data->pcc_id = pcc_id;
data->payload = payload;

thread_add_timer(ctrl_state->self, pcep_thread_timer_handler,
thread_add_timer(ctrl_state->self, timer_cb,
(void *)data, delay, thread);

return 0;
}

int schedule_thread_timer(struct ctrl_state *ctrl_state, int pcc_id,
enum pcep_ctrl_timer_type type, uint32_t delay,
void *payload, struct thread **thread)
{
return schedule_thread_timer_with_cb(
ctrl_state, pcc_id, type, delay, payload, thread,
pcep_thread_timer_handler);
}

int pcep_thread_timer_handler(struct thread *thread)
{
/* data unpacking */
Expand Down Expand Up @@ -448,7 +484,6 @@ int pcep_thread_timer_handler(struct thread *thread)
return ret;
}


int pcep_thread_timer_poll(struct ctrl_state *ctrl_state)
{
int i;
Expand All @@ -473,6 +508,53 @@ int pcep_thread_timer_poll(struct ctrl_state *ctrl_state)
return 0;
}

/* ------------ Controller Thread Socket Functions ------------ */

int schedule_thread_socket(struct ctrl_state *ctrl_state, int pcc_id,
enum pcep_ctrl_socket_type type, bool is_read,
void *payload, int fd, struct thread **thread,
pcep_ctrl_thread_callback socket_cb)
{
assert(thread != NULL);

struct pcep_ctrl_socket_data *data;

data = XCALLOC(MTYPE_PCEP, sizeof(*data));
data->ctrl_state = ctrl_state;
data->type = type;
data->is_read = is_read;
data->fd = fd;
data->pcc_id = pcc_id;
data->payload = payload;

if (is_read) {
thread_add_read(ctrl_state->self, socket_cb,
(void *)data, fd, thread);
} else {
thread_add_write(ctrl_state->self, socket_cb,
(void *)data, fd, thread);
}

return 0;
}

int pcep_thread_socket_write(void *fpt, void **thread, int fd, void *payload,
pcep_ctrl_thread_callback socket_cb)
{
struct ctrl_state *ctrl_state = ((struct frr_pthread *)fpt)->data;

return schedule_thread_socket(ctrl_state, 0, SOCK_PCEPLIB, false,
payload, fd, (struct thread **)thread, socket_cb);
}

int pcep_thread_socket_read(void *fpt, void **thread, int fd, void *payload,
pcep_ctrl_thread_callback socket_cb)
{
struct ctrl_state *ctrl_state = ((struct frr_pthread *)fpt)->data;

return schedule_thread_socket(ctrl_state, 0, SOCK_PCEPLIB, true,
payload, fd, (struct thread **)thread, socket_cb);
}

/* ------------ Controller Thread Event Handler ------------ */

Expand Down
35 changes: 35 additions & 0 deletions pathd/path_pcep_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ struct ctrl_state {
struct pcc_state *pcc[MAX_PCC];
};

/* Timer handling data structures */

enum pcep_ctrl_timer_type { TM_POLL = 1, TM_RECONNECT_PCC, TM_PCEPLIB_TIMER };

struct pcep_ctrl_timer_data {
struct ctrl_state *ctrl_state;
enum pcep_ctrl_timer_type type;
int pcc_id;
void *payload;
};

/* Socket handling data structures */

enum pcep_ctrl_socket_type { SOCK_PCEPLIB = 1 };

struct pcep_ctrl_socket_data {
struct ctrl_state *ctrl_state;
enum pcep_ctrl_socket_type type;
bool is_read;
int fd;
int pcc_id;
void *payload;
};

typedef int (*pcep_ctrl_thread_callback)(struct thread *);

/* Functions called from the main thread */
int pcep_ctrl_initialize(struct thread_master *main_thread,
struct frr_pthread **fpt,
Expand Down Expand Up @@ -77,4 +103,13 @@ void pcep_thread_update_path(struct ctrl_state *ctrl_state, int pcc_id,
void pcep_thread_schedule_reconnect(struct ctrl_state *ctrl_state, int pcc_id,
int retry_count, struct thread **thread);

void pcep_thread_schedule_pceplib_timer(struct ctrl_state *ctrl_state,
int delay, void *payload, struct thread **thread,
pcep_ctrl_thread_callback cb);
void pcep_thread_cancel_pceplib_timer(struct thread **thread);
int pcep_thread_socket_read(void *fpt, void **thread, int fd,
void *payload, pcep_ctrl_thread_callback cb);
int pcep_thread_socket_write(void *fpt, void **thread, int fd,
void *payload, pcep_ctrl_thread_callback cb);

#endif // _PATH_PCEP_CONTROLLER_H_
Loading