Commit Graph

134 Commits

Author SHA1 Message Date
Joris Vink 543a329ef6 run the connection timeout test per event loop. 2016-12-26 20:03:01 +01:00
Joris Vink 16166d37bd remove debug macro 2016-12-04 16:52:11 +01:00
Joris Vink 652833a979 set worker_active_connections to 0 always.
fixes a bug where if a worker died it contained garbled data...
2016-12-04 16:49:42 +01:00
Joris Vink cb62a6a8a8 Clamp netwait to maximum 100ms. 2016-12-04 12:16:27 +01:00
Joris Vink 5abb56d6fb remove unused define. 2016-11-04 09:21:09 +01:00
Joris Vink 9abf15a498 Make kore_onload() to be done per worker.
Mimics the behaviour of dso builds.
2016-07-06 21:59:17 +02:00
Joris Vink 39a5f21986 Allow "kore build" to produce single binaries.
Producing single binaries can now be done with building with
"kore build". To get started edit your build.conf and add the
following directives:

single_binary = yes
kore_source = /path/to/kore

optionally you can add kore_flavor to instruct how kore should
be built:

kore_flavor = NOTLS=1

When doing this your build.conf must also include the correct
linking options as the linking is now done fully by kore build.

The binary produced will include your configuration and takes
over a few of kore its command line flags (such as -f, -n or -r).
2016-07-06 16:16:15 +02:00
Joris Vink ae31ec01ac Separate private keys from worker processes.
Kore will now isolate RSA private keys to a separate process (keymgr).

Worker processes that require RSA signing for TLS connections will
communicate with this keymgr process in order to do so.

This behaviour cannot be disabled and is always turned on.
2016-06-08 13:56:38 +02:00
Elliot Schlegelmilch d37ecf9649 If we're iterating over rlimits, we should use rlim_t.
> error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'rlim_t' (aka 'long') [-Werror,-Wsign-compare]
2016-04-27 18:18:21 -07:00
Daniel Fahlgren 04aec1c2dd Fix typo in log message 2016-02-12 08:57:58 +01:00
Stig Telfer 7963a2deaa Added cleanup for the module handlers and validators 2016-02-01 12:13:22 +00:00
Stig Telfer bd975541dd Merge branch 'master' into oneswig 2016-02-01 10:04:59 +00:00
Joris Vink f2f5a3742f Let SIGTERM properly shutdown Kore as well. 2016-01-31 16:48:17 +01:00
Joris Vink cb489ce80f Define WAIT_ANY if not defined previously. 2016-01-31 16:47:00 +01:00
Stig Telfer 2ac6e7d41d Merge branch 'master' into oneswig 2016-01-24 13:46:38 +00:00
Stig Telfer 18d3cc032d rename *_fini to *_cleanup 2016-01-04 21:40:14 +00:00
Joris Vink c4b1206ae3 Bump copyright to 2016. 2016-01-04 12:58:51 +01:00
Stig Telfer 0c51d9da53 Add resource management as part of the kore shutdown process. 2015-12-29 19:39:39 +00:00
Joris Vink 769c78a6e8 Introduce NOHTTP=1 build option.
This basically turns off the HTTP layer for Kore. It does not
compile in anything for HTTP.

This allows Kore to be used as a network application platform as well.
Added an example for this called nohttp.

Other changes that sneaked in while hacking on this:
* Use calloc(), kill pendantic malloc option.
* Killed off SPDY/3.1 support completely, will be superseded by http2

Note that comes with massive changes to a lot of the core API
functions provided by Kore, these might break your application.
2015-11-27 16:22:50 +01:00
Joris Vink 28e48727a5 Kill TCP_NODELAY warnings for socketpair() fds. 2015-06-22 22:11:03 +02:00
Joris Vink 49ca95f390 Add our messaging framework.
With this framework apps can now send messages between worker processes.

A new API function exists:
	int kore_msg_register(u_int8_t id, void (*cb)(const void *, u_int32_t);

This API call allows your app to register a new message callback for a given ID.

You can then send messages on this ID to other workers using:
	void kore_msg_send(u_int8_t id, void *data, u_int32_t length);

This framework will interally be used for a few things such as allowing
websocket data to broadcasted between all workers, adding unified caching
and hopefully eventually moving the access log to this as well.

Some internals have changed with this commit:
	* worker_clients has been called connections.
	* the parent now initializes the net, and event subsystems.
	* kore_worker_websocket_broadcast() is dead.
2015-06-22 21:13:32 +02:00
Joris Vink 9217fb0bd8 Fix typo. 2015-06-14 16:50:10 +02:00
Joris Vink 13795ead63 NetBSD fixes, from David Carlier. 2015-06-14 16:44:37 +02:00
Joris Vink ff2574899d small style fix. 2015-05-20 11:36:02 +02:00
Thordur Bjornsson e47df37230 Make runas behave similarly to chroot.
Add new command line knob '-r', that disables runas similar to '-n',
it's implied as well for kore command runs.

Add default runas (nobody) user and chroot (/var/empty) path, if none
are specified, fallback to these.
2015-05-20 10:28:19 +02:00
Joris Vink 5228fe1cbc Fix typo in configuration option worker_accept_treshold.
There is no backwards comptabile option available.

Fixes #53
2015-05-18 12:20:28 +02:00
Joris Vink 97bd389935 Use IPC_PRIVATE when creating our shm.
Instead of a homegrown mechanism of finding empty SHM
keys that may or may not be available.

Should fix #44
2015-04-28 10:21:15 +02:00
Joris Vink b0deac577c Add new configuration option: worker_set_affinity.
Controls wether or not worker processes sets its affinity
mask to bind themselves to a single CPU.

Defaults to on (1).
2015-04-27 10:36:33 +02:00
Joris Vink b869041a14 Introduce new config option worker_accept_treshold
This configuration option limits the maximum number
of connections a worker process can accept() in a single
event loop.

It can be used to more evenly spread out incoming connections
across workers when new connections arrive in a burst.
2015-04-23 10:24:00 +02:00
Joris Vink 4c39ac43fb Properly deal with accept() failing.
In cases where accept() failed Kore would not relinquish the
lock towards other worker processes.

This becomes evident when dealing with a high number of concurrent
connections to the point the fd table gets full. In this scenario
the worker with the full fd table will spin on attempt to accept
newer connections.

As a bonus, Kore now has allows exactly up to worker_max_connections
of connections per worker before no longer attempting to grab the
accept lock.
2015-04-20 15:17:42 +02:00
Joris Vink 097a1166df Improve very heavy load handling.
Introduces two new configuration knobs:
	* socket_backlog (backlog for listen(2))
	* http_request_limit

The second one is the most interesting one.

Before, kore would iterate over all received HTTP requests
in its queue before returning out of http_process().

Under heavy load this queue can cause Kore to spend a considerable
amount of time iterating over said queue. With the http_request_limit,
kore will process at MOST http_request_limit requests before returning
back to the event loop.

This means responses to processed requests are sent out much quicker
and allows kore to handle any other incoming requests more gracefully.
2015-04-09 15:29:44 +02:00
Joris Vink b0ff041709 call setrlimit() before we drop privs. 2015-04-09 10:06:18 +02:00
Joris Vink af865abede Bump copyright to 2015 2015-04-07 13:08:26 +02:00
Joris Vink d6ab1d7445 Add rudimentary timers to Kore.
Timers are in ms resolution and are added using
kore_timer_add(cb, interval, flags).

Both oneshot timers and continious timers are supported.
2015-04-06 18:54:35 +02:00
Joris Vink eb235b3e01 shmat() returns (void *)-1 upon error. 2015-03-25 19:42:24 +01:00
Joris Vink 2ee72657e1 Brackets would be great here. 2015-01-19 15:26:53 +01:00
Joris Vink c494126720 Make worker_clients available everywhere. 2014-12-03 20:40:44 +01:00
Joris Vink f867882f43 Add websocket support to Kore.
Introduces a few new api functions:

- kore_websocket_handshake(struct http_request *):
	Performs the handshake on an HTTP request (coming from page handler)

- kore_websocket_send(struct connection *, u_int8_t, void *, size_t):
	Sends data to a websocket connection.

- kore_websocket_broadcast(struct connection *, u_int8_t, void *, size_t, int):
	Broadcast the given websocket op and data to all connected
	websocket clients on the worker. Note that as of right now
	the WEBSOCKET_BROADCAST_GLOBAL scope option does not work
	yet and messages broadcasted will be restricted to workers
	only.

- kore_worker_websocket_broadcast(struct connection *, void *, void *):
	Backend function used by kore_websocket_broadcast().
	Could prove useful for developers to have access to.

A simple example is given under examples/websocket.

Known issues:
	Kore does not support PING or CONT frames just yet.
2014-11-24 11:08:34 +01:00
Joris Vink a1e6588085 Repopulate now when we are doing idle timer checks.
Otherwise in a weird scenario the idle timer could
be started with a more recent time then the time we
test against causing a connection to disconnect
before its idle timer was over.
2014-11-21 16:38:23 +01:00
Joris Vink 6de0f8568a Rework net, worker and some http internals.
- The net code no longer has a recv_queue, instead reuse same recv buffer.
- Introduce net_recv_reset() to reset the recv buffer when needed.
- Have the workers spread the load better between them by slightly
  delaying their next accept lock and giving them an accept treshold
  so they don't go ahead and keep accepting connections if they end
  up winning the race constantly between the workers.
- The kore_worker_acceptlock_release() is no longer available.

- Prepopulate the HTTP server response header that is added to each
  response in both normal HTTP and SPDY modes.
- The path and host members of http_request are now allocated on the heap.

These changes overall result better performance on a multicore machine,
especially the worker load changes shine through.
2014-10-22 21:16:49 +02:00
Joris Vink b49622bb1a Add CRL support.
Allow Kore to use per domain CRLs when requiring client certificates.
The require_client_cert configuration option has been renamed to a more
sane client_certificates and can optionally take a second argument
which is the CRL in pem format.

You'll need a restart in case the CRLs get updated.
2014-10-18 02:32:05 +02:00
Joris Vink 693a07250f bump copyright years 2014-10-14 16:18:23 +02:00
Joris Vink 5855a88862 Increase frequency of idle_check. 2014-10-13 16:34:42 +02:00
Joris Vink 59a1a05afe No need to set now at the start, thanks clang. 2014-09-18 08:42:41 +02:00
Joris Vink 49e97ae6f8 Call kore_connection_disconnect() when draining connections.
This way the disconnect callbacks are called properly when
workers are exiting.
2014-09-17 08:46:55 +02:00
Joris Vink 4010bdd58d Remove kore_cb and its related settings.
After revisiting why this exists in Kore I decided it
does not belong in this platform and instead of letting
it sit there staring at me I rather just kill it.
2014-08-04 19:54:32 +02:00
Joris Vink a664e2a655 Make some configuration options fallback to defaults.
* If no runas_user is given, use current user.
* If no chroot path is given, don't complain if -n
* If no workers is set, use the default 1.
2014-07-31 13:59:42 +02:00
Joris Vink 8812d6989a Allow Kore to be started in foreground mode (-f)
Sending SIGINT or Ctrl-c will reap all workers and
exit again. Useful for development.
2014-07-31 13:27:04 +02:00
Joris Vink 22e1e1c425 Add worker_rlimit_nofiles as a configurable option. 2014-07-31 09:14:03 +02:00
Joris Vink 26d4d5d63b Rework the accept lock.
Kore no longer passes the accept lock to the "next in line"
worker but instead all workers will attempt to grab the lock
if they can.

Also remember if we had the lock previous iteration of the
event loop and don't constantly disable/enable the accepting sockets.

Makes Kore scale even better across multiple cpu's.
2014-07-30 15:20:09 +02:00
Joris Vink fe0149ad24 Have workers always pass the accept lock after handling their events.
Provides a nice performance boost when scaling across multiple cpu's.
2014-07-29 13:14:34 +02:00
Joris Vink 8313a31eba If we do not own the accept lock, reduce timeout for platform wait.
This way multiple workers won't stall when passing the accept lock
around for at maximum 100 ms (time spent in epoll/kqueue waits).
2014-07-28 23:27:58 +02:00
Joris Vink 2c6b5e6b0f Normalize kore_*.h headers 2014-07-03 22:14:46 +02:00
Joris Vink 723c99b65e Move kore_pgsql.h into includes/ directly.
Much like kore_tasks.h
2014-06-30 08:42:18 +02:00
Joris Vink dbd74c4551 Don't call module onloads until after everything is initialized.
This means the onload functions for a module are now called
after a worker has started and never from the parent ever again.
2014-06-29 21:15:23 +02:00
Joris Vink cf700b34f7 Add initial stab at asynchronous background tasks.
More to follow.
2014-06-28 16:17:18 +02:00
Joris Vink 0cb983adc0 Correct snprintf() usage 2014-04-23 16:29:58 +02:00
Joris Vink 2f044cc7eb Initial stab at entering postgresql contrib code.
Has support for full async pgsql queries. Most of the logic
is hidden behind a KORE_PGSQL() macro allowing you to insert
these pgsql calls in your page handlers without blocking the
kore worker while the query is going off.

There is place for improvement here, and perhaps KORE_PGSQL won't
stay as I feel this might overcomplicate things instead of making
them simpler as I thought it would.
2014-03-30 23:54:35 +02:00
Joris Vink 02351e1ab4 Update last_cb_run after we call the cb.
Makes sure we don't fall into cascading calls to the cb
if it takes longer to complete then the interval set.
2013-12-09 11:40:59 +01:00
Joris Vink 79aea48757 Don't stop passing the accept lock even when workers are very busy.
If a worker reached worker_max_connections and it was its turn to
grab the accept lock it would've gotten stuck and no new connections
would be handled even if other workers would be less busy.

Instead, we now skip the lock if we're too busy and pass it along
in the hopes other workers are less busy.
2013-11-27 23:02:15 +01:00
Joris Vink afeb213260 Kore no longer requires root to be started. 2013-11-18 00:42:57 +01:00
Joris Vink c80e1d3767 If there's only a single worker, do not block after accept treshold has been
reached
2013-11-01 22:37:59 +01:00
Joris Vink ac025adaa7 Make kore_worker_acceptlock_release() available to loaded modules.
This allows us to move the accept lock manually to another Kore worker
in case we own it and are about to do some heavy lifting on the current
Kore worker.
2013-10-24 09:05:46 +02:00
Joris Vink 9426906225 Long overdue, let Kore pick its own shm key if the default KORE_SHM_KEY is already taken. 2013-10-14 11:26:11 +02:00
Joris Vink ee3fd3c039 Allow the user defined callback to run on workers as well. 2013-09-03 08:41:09 +02:00
Joris Vink dca6e58189 remove commented out debug defines. 2013-07-18 22:13:59 +02:00
Joris Vink 91b839f355 Add OSX support, buildable via "make osx".
Make sure you have OpenSSL 1.0.0d+ (available from Macports) installed.

Based on diff from Vaibhav Bhembre via github
2013-07-17 20:19:44 +02:00
Joris Vink ced1279f88 Properly calculate worker offset, otherwise we'll eventually run into trouble. 2013-07-15 11:24:49 +02:00
Joris Vink 1f938eb818 Only release accept lock when required. 2013-07-15 11:12:05 +02:00
Joris Vink 29fa49ba83 Add fixed size memory pools and use them throughout Kore. 2013-07-15 11:06:36 +02:00
Joris Vink c999bf5001 Kore can now disconnect SPDY session if they've been idle too long.
Configurable via spdy_idle_time in your configuration file.

Setting this to 0 will keep SPDY sessions open indefinately.
2013-07-13 20:19:01 +02:00
Joris Vink f59e94a7b6 Add spdy_session_teardown() which can properly teardown a SPDY session.
Use this throughout the spdy code to propagate session errors (if any) occur.

At the same time fix BSD's missing CONN_WRITE_BLOCK
2013-07-13 19:56:38 +02:00
Joris Vink 649e81afd7 use handler function name if available when logging a worker process that has died. 2013-07-07 14:56:50 +02:00
Joris Vink 95bacb5690 Kore will now keep track of page handlers that cause workers to die.
This is useful to track down any issues you might have in your module.

A log entry with a page handler causing issues looks like:
Jul  7 14:44:30 devbook kore[18191]: [parent]: worker 1 (18193)-> status 11
Jul  7 14:44:30 devbook kore[18191]: [parent]: worker 1 (pid: 18193) (hdlr: 0x242d9c0) gone
Jul  7 14:44:30 devbook kore[18191]: [parent]: hdlr serve_intro has caused 2 error(s)
2013-07-07 14:48:32 +02:00
Joris Vink c1723f2db5 Clean up header includes, based on a diff from Ewan Higgs via github.
And while we're messing in it, make sure bsd.c compiles again.
2013-07-06 20:55:22 +02:00
Joris Vink 51efd84b6e also set has_lock to 0 when a worker starts. 2013-07-05 21:17:08 +02:00
Joris Vink e1183e22a6 If the worker that owns the accept lock dies, make sure the next one in line can grab it. Otherwise kore comes to a halt (by not accepting new connections). 2013-07-05 20:19:50 +02:00
Joris Vink 0c08b57d3e - add idle timer for normal connections (ie: !SPDY), max 20 seconds.
- use idle timer to make sure we don't block SPDY connections indefinately
  when our window size has reached 0.
2013-07-01 12:08:51 +02:00
Joris Vink 4b2420097b - Better spread load between all worker processes.
- Introduce own memory management system on top of malloc to keep track
  of all our allocations and free's. Later we should introduce a pooling
  mechanism for fixed size allocations (http_request comes to mind).
- Introduce ssl_cipher in configuration.

Memory usage is kind of high right now, but it seems its OpenSSL
doing it rather then Kore.
2013-06-27 08:46:18 +02:00
Joris Vink b4a0330a96 - Better spread load between all worker processes.
- Introduce own memory management system on top of malloc to keep track
  of all our allocations and free's. Later we should introduce a pooling
  mechanism for fixed size allocations (http_request comes to mind).
- Introduce ssl_cipher in configuration.

Memory usage is kind of high right now, but it seems its OpenSSL
doing it rather then Kore.
2013-06-27 08:43:07 +02:00
Joris Vink 2fc5233358 Rework the way worker processes give each other the accept lock.
Instead of waiting until one worker is filled up on connections
the workers find the next lowest loaded worker and will hand
over the lock to them instead. This will cause a nicer spread of load.

Instead of running one accept per event loop, we attempt to accept
as many as worker_max_connections allows.

Refactor net sending/recv code a bit.
2013-06-27 00:22:48 +02:00
Joris Vink 124f3ecad9 no need to use kore_log() in a debug context 2013-06-26 15:59:42 +02:00
Joris Vink 0dda6f996f Add a form of synchronization between what worker will be accepting
new connections and which ones will not be notified for it.

Fixes the thundering herd problem, and nicely spreads out load between
all the workers equally. A configuration option (workers_max_connections)
is available to tweak how many connections a worker will have before
giving up the accept lock.

Two ways are added to this commit for access locking:
- Locking via semaphores.
- Locking via GCC's builtin atomic methods.

The default is running with semaphores disabled (OpenBSD cannot do
sem_init() with pshared set to 1, which is required).

If you want to use semaphores add KORE_USE_SEMAPHORES to CFLAGS,
and -lpthread to LDFLAGS in the Makefile.

Other fixes:
- BSD: add a timeout to kevent().
- Merge kore_worker_wait together, linux knows waitpid() as well.
- Send the correct SIGQUIT signal to workers instead of SIGINT.
- Fix kore_time_ms().
- Log fatal worker messages in syslog.
- Refactor code even more.
- Do not free our own kore_worker structure.
2013-06-26 16:37:22 +02:00
Joris Vink 3e5c17b8a3 refactor code quite a bit. 2013-06-26 11:18:32 +02:00