2017-07-11 18:47:37 +02:00
|
|
|
/* * Copyright (c) 2017, The Tor Project, Inc. */
|
2013-08-25 17:45:07 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file scheduler.h
|
2017-07-11 18:47:37 +02:00
|
|
|
* \brief Header file for scheduler*.c
|
2013-08-25 17:45:07 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#ifndef TOR_SCHEDULER_H
|
|
|
|
#define TOR_SCHEDULER_H
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
#include "channel.h"
|
2013-12-13 15:27:00 +01:00
|
|
|
#include "testsupport.h"
|
2013-08-25 17:45:07 +02:00
|
|
|
|
2017-09-21 19:15:51 +02:00
|
|
|
/** Scheduler type, we build an ordered list with those values from the
|
|
|
|
* parsed strings in Schedulers. The reason to do such a thing is so we can
|
|
|
|
* quickly and without parsing strings select the scheduler at anytime. */
|
|
|
|
typedef enum {
|
|
|
|
SCHEDULER_NONE = -1,
|
|
|
|
SCHEDULER_VANILLA = 1,
|
|
|
|
SCHEDULER_KIST = 2,
|
|
|
|
SCHEDULER_KIST_LITE = 3,
|
|
|
|
} scheduler_types_t;
|
|
|
|
|
|
|
|
/**
|
2017-07-11 18:47:37 +02:00
|
|
|
* A scheduler implementation is a collection of function pointers. If you
|
|
|
|
* would like to add a new scheduler called foo, create scheduler_foo.c,
|
|
|
|
* implement at least the mandatory ones, and implement get_foo_scheduler()
|
|
|
|
* that returns a complete scheduler_t for your foo scheduler. See
|
|
|
|
* scheduler_kist.c for an example.
|
|
|
|
*
|
|
|
|
* These function pointers SHOULD NOT be used anywhere outside of the
|
|
|
|
* scheduling source files. The rest of Tor should communicate with the
|
|
|
|
* scheduling system through the functions near the bottom of this file, and
|
|
|
|
* those functions will call into the current scheduler implementation as
|
|
|
|
* necessary.
|
|
|
|
*
|
|
|
|
* If your scheduler doesn't need to implement something (for example: it
|
|
|
|
* doesn't create any state for itself, thus it has nothing to free when Tor
|
|
|
|
* is shutting down), then set that function pointer to NULL.
|
|
|
|
*/
|
|
|
|
typedef struct scheduler_s {
|
2017-09-21 19:15:51 +02:00
|
|
|
/* Scheduler type. This is used for logging when the scheduler is switched
|
|
|
|
* during runtime. */
|
|
|
|
scheduler_types_t type;
|
|
|
|
|
2017-07-11 18:47:37 +02:00
|
|
|
/* (Optional) To be called when we want to prepare a scheduler for use.
|
|
|
|
* Perhaps Tor just started and we are the lucky chosen scheduler, or
|
|
|
|
* perhaps Tor is switching to this scheduler. No matter the case, this is
|
|
|
|
* where we would prepare any state and initialize parameters. You might
|
|
|
|
* think of this as the opposite of free_all(). */
|
|
|
|
void (*init)(void);
|
|
|
|
|
|
|
|
/* (Optional) To be called when we want to tell the scheduler to delete all
|
|
|
|
* of its state (if any). Perhaps Tor is shutting down or perhaps we are
|
|
|
|
* switching schedulers. */
|
|
|
|
void (*free_all)(void);
|
|
|
|
|
|
|
|
/* (Mandatory) Libevent controls the main event loop in Tor, and this is
|
|
|
|
* where we register with libevent the next execution of run_sched_ev [which
|
|
|
|
* ultimately calls run()]. */
|
|
|
|
void (*schedule)(void);
|
|
|
|
|
|
|
|
/* (Mandatory) This is the heart of a scheduler! This is where the
|
|
|
|
* excitement happens! Here libevent has given us the chance to execute, and
|
|
|
|
* we should do whatever we need to do in order to move some cells from
|
|
|
|
* their circuit queues to output buffers in an intelligent manner. We
|
|
|
|
* should do this quickly. When we are done, we'll try to schedule() ourself
|
2017-09-20 02:03:17 +02:00
|
|
|
* if more work needs to be done to setup the next scheduling run. */
|
2017-07-11 18:47:37 +02:00
|
|
|
void (*run)(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* External event not related to the scheduler but that can influence it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* (Optional) To be called whenever Tor finds out about a new consensus.
|
|
|
|
* First the scheduling system as a whole will react to the new consensus
|
2017-09-13 04:12:24 +02:00
|
|
|
* and change the scheduler if needed. After that, the current scheduler
|
|
|
|
* (which might be new) will call this so it has the chance to react to the
|
|
|
|
* new consensus too. If there's a consensus parameter that your scheduler
|
|
|
|
* wants to keep an eye on, this is where you should check for it. */
|
2018-01-31 20:15:02 +01:00
|
|
|
void (*on_new_consensus)(void);
|
2017-07-11 18:47:37 +02:00
|
|
|
|
|
|
|
/* (Optional) To be called when a channel is being freed. Sometimes channels
|
|
|
|
* go away (for example: the relay on the other end is shutting down). If
|
|
|
|
* the scheduler keeps any channel-specific state and has memory to free
|
|
|
|
* when channels go away, implement this and free it here. */
|
|
|
|
void (*on_channel_free)(const channel_t *);
|
|
|
|
|
|
|
|
/* (Optional) To be called whenever Tor is reloading configuration options.
|
|
|
|
* For example: SIGHUP was issued and Tor is rereading its torrc. A
|
|
|
|
* scheduler should use this as an opportunity to parse and cache torrc
|
|
|
|
* options so that it doesn't have to call get_options() all the time. */
|
|
|
|
void (*on_new_options)(void);
|
|
|
|
} scheduler_t;
|
|
|
|
|
2017-09-13 18:47:02 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* Globally visible scheduler variables/values
|
|
|
|
*
|
|
|
|
* These are variables/constants that all of Tor should be able to see.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/* Default interval that KIST runs (in ms). */
|
|
|
|
#define KIST_SCHED_RUN_INTERVAL_DEFAULT 10
|
|
|
|
/* Minimum interval that KIST runs. This value disables KIST. */
|
|
|
|
#define KIST_SCHED_RUN_INTERVAL_MIN 0
|
|
|
|
/* Maximum interval that KIST runs (in ms). */
|
|
|
|
#define KIST_SCHED_RUN_INTERVAL_MAX 100
|
|
|
|
|
2017-07-11 18:47:37 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* Globally visible scheduler functions
|
|
|
|
*
|
|
|
|
* These functions are how the rest of Tor communicates with the scheduling
|
|
|
|
* system.
|
|
|
|
*****************************************************************************/
|
2013-08-25 17:45:07 +02:00
|
|
|
|
|
|
|
void scheduler_init(void);
|
2017-07-11 18:47:37 +02:00
|
|
|
void scheduler_free_all(void);
|
|
|
|
void scheduler_conf_changed(void);
|
2018-01-31 20:15:02 +01:00
|
|
|
void scheduler_notify_networkstatus_changed(void);
|
2017-07-11 18:47:37 +02:00
|
|
|
MOCK_DECL(void, scheduler_release_channel, (channel_t *chan));
|
2017-07-10 22:10:50 +02:00
|
|
|
|
2017-07-11 18:47:37 +02:00
|
|
|
/*
|
|
|
|
* Ways for a channel to interact with the scheduling system. A channel only
|
|
|
|
* really knows (i) whether or not it has cells it wants to send, and
|
|
|
|
* (ii) whether or not it would like to write.
|
|
|
|
*/
|
|
|
|
void scheduler_channel_wants_writes(channel_t *chan);
|
|
|
|
MOCK_DECL(void, scheduler_channel_doesnt_want_writes, (channel_t *chan));
|
|
|
|
MOCK_DECL(void, scheduler_channel_has_waiting_cells, (channel_t *chan));
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Private scheduler functions
|
|
|
|
*
|
|
|
|
* These functions are only visible to the scheduling system, the current
|
|
|
|
* scheduler implementation, and tests.
|
|
|
|
*****************************************************************************/
|
2014-01-24 06:13:26 +01:00
|
|
|
#ifdef SCHEDULER_PRIVATE_
|
2017-07-11 18:47:37 +02:00
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* Defined in scheduler.c
|
|
|
|
*********************************/
|
2017-12-07 15:28:02 +01:00
|
|
|
|
2017-12-11 14:56:03 +01:00
|
|
|
void scheduler_set_channel_state(channel_t *chan, int new_state);
|
2017-12-11 15:32:47 +01:00
|
|
|
const char *get_scheduler_state_string(int scheduler_state);
|
2017-10-03 19:58:22 +02:00
|
|
|
|
|
|
|
/* Triggers a BUG() and extra information with chan if available. */
|
|
|
|
#define SCHED_BUG(cond, chan) \
|
|
|
|
(PREDICT_UNLIKELY(cond) ? \
|
|
|
|
((BUG(cond)) ? (scheduler_bug_occurred(chan), 1) : 0) : 0)
|
|
|
|
|
|
|
|
void scheduler_bug_occurred(const channel_t *chan);
|
|
|
|
|
2017-07-11 18:47:37 +02:00
|
|
|
smartlist_t *get_channels_pending(void);
|
|
|
|
MOCK_DECL(int, scheduler_compare_channels,
|
2014-01-29 02:25:37 +01:00
|
|
|
(const void *c1_v, const void *c2_v));
|
2017-10-03 16:44:45 +02:00
|
|
|
void scheduler_ev_active(void);
|
2017-09-14 20:22:22 +02:00
|
|
|
void scheduler_ev_add(const struct timeval *next_run);
|
2016-06-02 15:46:12 +02:00
|
|
|
|
|
|
|
#ifdef TOR_UNIT_TESTS
|
|
|
|
extern smartlist_t *channels_pending;
|
2017-10-03 16:44:45 +02:00
|
|
|
extern struct mainloop_event_t *run_sched_ev;
|
2017-09-14 20:47:59 +02:00
|
|
|
extern const scheduler_t *the_scheduler;
|
2017-07-11 18:47:37 +02:00
|
|
|
void scheduler_touch_channel(channel_t *chan);
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(TOR_UNIT_TESTS) */
|
2017-07-11 18:47:37 +02:00
|
|
|
|
|
|
|
/*********************************
|
|
|
|
* Defined in scheduler_kist.c
|
|
|
|
*********************************/
|
|
|
|
|
2017-09-15 15:06:11 +02:00
|
|
|
#ifdef SCHEDULER_KIST_PRIVATE
|
|
|
|
|
2017-09-20 02:03:17 +02:00
|
|
|
/* Socket table entry which holds information of a channel's socket and kernel
|
2017-07-11 18:47:37 +02:00
|
|
|
* TCP information. Only used by KIST. */
|
|
|
|
typedef struct socket_table_ent_s {
|
|
|
|
HT_ENTRY(socket_table_ent_s) node;
|
|
|
|
const channel_t *chan;
|
|
|
|
/* Amount written this scheduling run */
|
|
|
|
uint64_t written;
|
|
|
|
/* Amount that can be written this scheduling run */
|
|
|
|
uint64_t limit;
|
|
|
|
/* TCP info from the kernel */
|
|
|
|
uint32_t cwnd;
|
|
|
|
uint32_t unacked;
|
|
|
|
uint32_t mss;
|
|
|
|
uint32_t notsent;
|
|
|
|
} socket_table_ent_t;
|
|
|
|
|
|
|
|
typedef HT_HEAD(outbuf_table_s, outbuf_table_ent_s) outbuf_table_t;
|
|
|
|
|
|
|
|
MOCK_DECL(int, channel_should_write_to_kernel,
|
|
|
|
(outbuf_table_t *table, channel_t *chan));
|
|
|
|
MOCK_DECL(void, channel_write_to_kernel, (channel_t *chan));
|
|
|
|
MOCK_DECL(void, update_socket_info_impl, (socket_table_ent_t *ent));
|
|
|
|
|
2017-09-14 19:23:43 +02:00
|
|
|
int scheduler_can_use_kist(void);
|
|
|
|
void scheduler_kist_set_full_mode(void);
|
|
|
|
void scheduler_kist_set_lite_mode(void);
|
2017-07-11 18:47:37 +02:00
|
|
|
scheduler_t *get_kist_scheduler(void);
|
2018-01-31 20:15:02 +01:00
|
|
|
int kist_scheduler_run_interval(void);
|
2017-07-11 18:47:37 +02:00
|
|
|
|
|
|
|
#ifdef TOR_UNIT_TESTS
|
|
|
|
extern int32_t sched_run_interval;
|
|
|
|
#endif /* TOR_UNIT_TESTS */
|
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(SCHEDULER_KIST_PRIVATE) */
|
2017-09-15 15:06:11 +02:00
|
|
|
|
2017-07-11 18:47:37 +02:00
|
|
|
/*********************************
|
|
|
|
* Defined in scheduler_vanilla.c
|
|
|
|
*********************************/
|
|
|
|
|
|
|
|
scheduler_t *get_vanilla_scheduler(void);
|
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(SCHEDULER_PRIVATE_) */
|
2014-01-24 06:13:26 +01:00
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(TOR_SCHEDULER_H) */
|
2013-08-25 17:45:07 +02:00
|
|
|
|