Commit Graph

220 Commits

Author SHA1 Message Date
Joris Vink 4ad50caa29 Large changes to the memory subsystem in kore.
- Change pools to use mmap() for allocating regions.
- Change kore_malloc() to use pools for commonly sized objects.
  (split into multiple of 2 buckets, starting at 8 bytes up to 8192).
- Rename kore_mem_free() to kore_free().

The preallocated pools will hold up to 128K of elements per block size.

In case a larger object is to be allocated kore_malloc() will use
malloc() instead.
2016-07-12 13:54:14 +02: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 89f81a8b5a Revert "Throw a handshake failure if we cannot find the given SNI hostname."
This reverts commit afd4182975.
2016-06-09 13:52:37 +02:00
Joris Vink afd4182975 Throw a handshake failure if we cannot find the given SNI hostname. 2016-06-08 16:46:28 +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
Joris Vink 82a9e6ef59 Formatting and unbreaking NOHTTP builds. 2016-02-01 20:02:02 +01:00
Joris Vink f4d00645ed Merge pull request #99 from oneswig/master
Add resource management as part of the kore shutdown process.
2016-02-01 19:51:27 +01:00
Joris Vink 1f5e482b8a Build option changes.
- Build with -O2 unless NOOPT is set to 1.
- Hide -g behind DEBUG instead of always building with it.
- Explicitely set the standard used to c99, use pedantic.
2016-02-01 15:33:40 +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
Stig Telfer 2ac6e7d41d Merge branch 'master' into oneswig 2016-01-24 13:46:38 +00:00
Stig Telfer ec73c35952 Add missing C library headers 2016-01-22 11:08:13 +00:00
Joris Vink fcb86ddb8b Massive rework of HTTP layer.
This commit is a flag day, your old modules will almost certainly
need to be updated in order to build properly with these changes.

Summary of changes:

- Offload HTTP bodies to disk if they are large (inspired by #100).
  (disabled by default)
- The http_argument_get* macros now takes an explicit http_request parameter.
- Kore will now throw 404 errors almost immediately after an HTTP request
  has come in instead of waiting until all data has arrived.

API changes:

- http_argument_get* macros now require an explicit http_request parameter.
  (no more magic invokations).
- http_generic_404() is gone
- http_populate_arguments() is gone
- http_body_bytes() is gone
- http_body_text() is gone
- http_body_read() has been added
- http_populate_post() has been added
- http_populate_get() has been added
- http_file_read() has been added
- http_file_rewind() has been added
- http_file_lookup() no longer takes name, fname, data and len parameters.
- http_file_lookup() now returns a struct http_file pointer.
- http_populate_multipart_form() no longer takes an secondary parameter.

New configuration options:

- http_body_disk_offload:
	Number of bytes after which Kore will offload the HTTP body to
	disk instead of retaining it in memory. If 0 this feature is
	disabled. (Default: 0)

- http_body_disk_path:
	The path where Kore will store temporary HTTP body files.
	(this directory does not get created if http_body_disk_offload is 0).

New example:

The upload example has been added, demonstrating how to deal with file
uploads from a multipart form.
2016-01-18 11:30:22 +01: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 a64808c6b0 Improvements to our message framework.
Change the callback prototypes to:
	void callback(struct kore_msg *msg, const void *data);

This allows the callbacks to receive the full kore_msg data structure
as sent over the wire (including length and id). Useful for future
additions to the kore_msg structure (such as worker origin).

Several other improvements:
	* Accesslog now uses the msg framework as well.
	* Websocket WEBSOCKET_BROADCAST_GLOBAL now works.

Small websocket improvement in this commit:
	* Build the frame to be sent only once when broadcasting
	 instead of per connection we are broadcasting towards.
2015-06-23 18:17:14 +02: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 db198578c6 Allow Kore to function under DragonflyBSD.
From David Carlier
2015-06-14 16:54:44 +02:00
Joris Vink 854b916597 Update usage text. 2015-05-28 22:20:43 +02:00
Geenz 0f5b39c977 Small tweak to kore's output when running a module 2015-05-27 09:34:47 -04:00
Geenz 817b916e1f Call it NOTLS instead.
Per @jorisvink's feedback.
2015-05-25 09:42:34 -04:00
Geenz c44de3f629 Rename BENCHMARK to reflect its actual function.
NO_SSL makes a bit more sense, especially for people who proxy their requests via nginx, apache, or similar.
2015-05-25 09:28:13 -04:00
Joris Vink 02e06b8bb6 Stop client initiated TLS renegotiations completely. 2015-05-20 16:36:13 +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 384bc8fdd6 Default to only TLSv1.2 from now on.
Add configuration setting tls_version to specify if you
either want TLSv1.2 or TLSv1.0 or both.

The configuration options ssl_cipher and ssl_dhparam
have changed name to tls_cipher and tls_dhparam. There is
no fallback so you might have to update your configs.
2015-05-06 10:59:43 +02:00
Joris Vink b035dea3ff Make socket error messages more understandable. 2015-04-26 21:09:46 +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 af865abede Bump copyright to 2015 2015-04-07 13:08:26 +02:00
Joris Vink dbe301e58c Add a patch level to our version number. 2014-12-12 13:06:41 +01: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 8bcfb6cbb3 Be consistent with the help text 2014-08-03 17:11:02 +02:00
Joris Vink 6495e84005 Show the uri to the running kore instance in foreground mode 2014-08-01 20:00:09 +02:00
Joris Vink e29a5461ca Allow kore build to compile static files 2014-08-01 15:55:09 +02:00
Joris Vink 61f56b8da0 Bump copyright 2014-08-01 14:14:46 +02:00
Joris Vink ea5b89d20b Move orbit functionality into kore directly.
Makes more sense and reads easier:

kore create myapp
kore build myapp
kore run myapp

Note that kore retains its cli options (if no command was given),
meaning you can still start kore in the traditional way as well.

The command options are simply to make development easier.
2014-08-01 13:59:47 +02:00
Joris Vink c2e4d55235 Add a BENCHMARK compile option which compiles without OpenSSL.
Personally use this for testing Kore its performance without
letting the OpenSSL stack get in the way too much.

Note that it leaves data structures as is, and just removes
any calls to OpenSSL (and removes the linking vs OpenSSL).
2014-08-01 10:22:32 +02:00
Joris Vink 8ff870a352 Remove the ability to pass libs on the cli again.
Sorry, this no longer makes sense in terms of what
orbit is suppose to be doing.
2014-08-01 09:39:09 +02:00
Joris Vink b17eefde1f Allow developers to pass the lib to load on the cli.
When running in -f (foreground) you can now specify
the library Kore needs to load on the command line:

kore -fnc module.conf myapp.so

This has the benefit that your configuration file no
longer needs the load directive when hacking on your code.

Note that you can still specify load in your config file
regardless, if you so chose.

All of this is being done in order to try and move away
from the backwards way of getting up and running with Kore.
2014-07-31 17:15:51 +02:00
Joris Vink ef49a0d4e1 Don't use pidfile if we're in foreground mode 2014-07-31 14:02:33 +02:00
Joris Vink 9b76afc5fb Whoops, remove KORE_BENCHMARK that sneaked in 2014-07-31 13:35:39 +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 423d57b08d Add -v to dump out version and compiled in features 2014-07-04 09:25:18 +02:00
Joris Vink 1ad9f039ab Add pgsql_conn_max configuration parameter.
Allows you to tune how many pgsql connections kore
will make at one time.
2014-07-04 09:14:05 +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 9a8092bf41 Add authentication blocks for Kore.
Using authentication blocks one can define "authentication" mechanisms
in Kore for page handlers.

This can be used to require a session cookie (validated by your own validator)
for certain page handlers, and hopefully in the future provide a framework
for adding more authentication things (like HTTP Auth).

Right now only cookie checking is available.
2014-01-22 22:55:10 +01:00
Joris Vink 21b148e3a5 Allow Kore to load multiple modules at once. 2013-12-15 01:11:56 +01:00
Joris Vink f2aa206f3b Add support for client certificates 2013-12-14 16:31:07 +01:00
Joris Vink fc243835c9 Update last_cb_run after we call cb here as well 2013-12-09 11:42:22 +01:00
Joris Vink afeb213260 Kore no longer requires root to be started. 2013-11-18 00:42:57 +01:00
Joris Vink 34c2f31a93 Add validators to kore, specified in the configuration using 'validator' keyword.
Example:
	validator	v_id	function	v_id_function
	validator	v_url	regex		^/url/path/[a-z]*$

You can then call these using kore_validator_run(char *, char *), example:

	if (!kore_validator_run("v_url", req->path))
		[req->path is bad];
2013-11-09 16:21:52 +01: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 95c8b8e126 Add a callback that Kore can call in your module every given interval.
The callback is run from the parent process (which runs as root).

Adds kore_cb and kore_cb_interval configuration options.
2013-09-02 08:52:16 +02:00
Joris Vink 659e19f92f add IPv6 support and support for multiple listeners. 2013-07-27 20:56:15 +02:00
Joris Vink 2c1352b226 remove versioning numbers, i hate them. 2013-07-16 15:30:20 +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 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 eb43fd31e1 remove unused stuff 2013-07-05 16:02:03 +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 bf1940225a everybody loves to tout their own horn.. so introduce a server response header 2013-06-26 16:58:01 +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
Joris Vink 6026a6d4ee add SNI support, and change domain configuration a bit. 2013-06-24 11:32:45 +02:00
Joris Vink a1b400c400 Add access logging to Kore. 2013-06-24 09:36:40 +02:00
Joris Vink 4dff38ebb0 ignore sigpipe in workers 2013-06-19 22:41:00 +02:00
Joris Vink 25f1ab9865 Add BSD kqueue(2) support. Compile with make bsd (or make linux for linux) 2013-06-17 23:39:17 +02:00
Joris Vink e170e916ce Reload the module in the main process as well when SIGHUP is received.
Otherwise new worker processes will not receive the updated module.
2013-06-05 13:50:50 +02:00
Joris Vink a74fffe40c Introduce certfile and certkey in the configuration to specify where the certificate file and keys are located on a system.
Free unused vars in the main process after starting.
2013-06-05 09:47:08 +02:00
Joris Vink 32a2035ce9 move kore_server_sslstart() into main process, workers will inherit. 2013-06-05 09:32:53 +02:00
Joris Vink b9f47b0f86 configuration files are now passed using the -c option.
allow debug output when the -d flag is specified.
2013-06-05 08:55:07 +02:00
Joris Vink ade34a26c1 move chroot() to workers instead, parent process doesn't require it plus it cannot unlink pidfile otherwise.
better logging in syslog
2013-06-05 08:45:51 +02:00
Joris Vink 90e1b3a7da use syslog() for informative messages from all parts of kore. 2013-06-04 23:24:47 +02:00
Joris Vink 49b77d3b0e better proctitles 2013-06-04 17:04:28 +02:00
Joris Vink 276d8c5a82 oops bring back chroot 2013-06-04 17:01:06 +02:00
Joris Vink ca437a6cef set process title 2013-06-04 16:58:13 +02:00
Joris Vink 9ef669ff6f write main process pid to /var/run/kore.pid (changable in configuration) 2013-06-04 16:53:30 +02:00
Joris Vink 443b1c8c5f format 2013-06-04 16:33:35 +02:00
Joris Vink e7db5ee6b1 rename kore_log to kore_debug, and allow one to turn it off. 2013-06-04 16:30:53 +02:00
Joris Vink 11fca19923 be less spammy 2013-06-04 16:17:42 +02:00
Joris Vink ab0dc25c61 use sched_setaffinity() to set what CPU each worker process should run on. 2013-06-04 13:54:16 +02:00
Joris Vink 8f8ab92521 upon quit time, workers will not accept new connections but will
handle all outstanding http requests.
2013-06-04 13:43:11 +02:00
Joris Vink 0de28488a6 move from multithreads to single threaded worker processes. 2013-06-04 11:55:38 +02:00
Joris Vink b65cc93426 allow handlers to return KORE_RESULT_RETRY. This will tell the worker to reschedule the page request again at the end of its list. (Allows module creators to write truely nonblocking modules). 2013-05-31 00:40:06 +02:00
Joris Vink 7dfa7e6ec0 be carefull when we reload the module to not reload it when workers are inside the module callbacks.
do this by implementing a pthread rwlock, and locking it for reading when going into a callback and locking it for writing when we need to reload the mod.
2013-05-31 00:06:54 +02:00
Joris Vink fecbd058cb rework the worker thread so there's actually time to schedule more then one request at a time on them. 2013-05-31 14:24:00 +02:00
Joris Vink e428886e16 decrement the worker load on HTTP_REQUEST_DELETE 2013-05-31 13:30:51 +02:00
Joris Vink bb4001d119 ok that was wrong, disconnect http clients whne we're done sending or upon error, not immediately after queueing everything. 2013-05-30 21:57:14 +02:00
Joris Vink ca1c884e43 attempt to clear out send buffer before we shutdown ssl connection.
remove superfleaous debug
2013-05-30 21:39:01 +02:00
Joris Vink ec5ac40706 wake up the workers once in a while to process anything that is waiting. 2013-05-30 20:55:50 +02:00
Joris Vink f9b3cfcee4 reschedule events if we cannot lock the connection at the time being. 2013-05-30 20:38:25 +02:00
Joris Vink 9ad263e287 do not remove disconnected connections until we actually are ready to disconnect them. 2013-05-30 20:07:06 +02:00
Joris Vink 9243f409cc move to a worker based threading approach where we delegate http requests to workers in a round robin basis (later this should be swapped to find the laziest worker and assign the request to that instead). 2013-05-30 19:36:42 +02:00
Joris Vink 8478d8df54 add chroot and runas directives so we can chroot and drop privilegs properly 2013-05-04 22:18:27 +02:00
Joris Vink ce729010f7 - simplify header building using kore_buf_appendf() for normal HTTP requests.
- make sure we dont free nb->buf if its NULL. (semantics).
- remove some superfluffy debug.
- make sure we call [inflate|deflate]End when we dc a client that used zlib.
2013-05-04 20:44:16 +02:00
Joris Vink 088d877715 switch dynamic handlers to regex based patterns, much easier. 2013-05-29 14:29:46 +02:00
Joris Vink 0be748ee0f use SSL_CTX_use_certificate_chain_file instead when loading certificates so we can properly use our intermediate authority. 2013-05-29 12:27:30 +02:00
Joris Vink 90588913d4 allow sending SIGHUP to kore, which will then reload its content module 2013-05-03 00:04:06 +02:00
Joris Vink f61bbe8ff4 move buf stuff back into kore.h, wont need it separately 2013-05-02 15:14:00 +02:00
Joris Vink 84428f7133 move buf stuff into its own header, so we can use it in spdy.h 2013-05-02 14:55:57 +02:00
Joris Vink 60ed3e0fd2 the net qeueuing functions don't need to return status codes, simplifies a lot of other code that revolves around those.
prepare for POST support
2013-05-02 14:47:02 +02:00
Joris Vink f1fa108f98 begin processing window_update messages 2013-05-02 10:10:03 +02:00
Joris Vink 364dc58219 rework disconnecting clients and fix bug where spdy ocnnections could segfault after disconnecting 2013-05-02 09:10:35 +02:00
Joris Vink f46bba50ef add very basic support for HTTP/1.1. 2013-05-02 03:51:04 +02:00
Joris Vink 5f983d575b add flags to net_recv_queue() and net_send_queue(),.
NETBUF_CALL_CB_ALWAYS flag, calls the registered cb everytime data has
been received or sent.
2013-05-02 00:28:49 +02:00
Joris Vink a228cdba0e introduce kore_split_string() to properly split strings apart.
introduce kore_date_to_time() to conver http-date formatted strings to time_t.
2013-05-01 20:10:45 +02:00
Joris Vink d9bbb0b1f1 initial module support 2013-05-01 16:03:48 +02:00
Joris Vink a074258dcc rework the way events trigger and the way we handle them. follow the manual for once, as edge trigger does not refire until we got an EAGAIN.
introduce net_send_flush() and net_recv_flush() for this purpose, we attempt to make as much headway as possible until we reach EAGAIN or until we can simply pickup again later.

should merge all the stuff in http_response() into a single send buffer, **out is in place in net_send_queue() for that purpose.
2013-05-01 13:43:47 +02:00
Joris Vink d59847c448 SPDY works, we can serve content. Now for dynamic modules so we can
get stuff on the road.

TODO: add plain HTTP support if SPDY isnt working in the browser.
2013-05-01 12:23:21 +02:00
Joris Vink 848704f74b lots of new stuff, including processing of http requests and an attempt
to build an initial spdy response (SYN frame + header block content).
2013-05-01 08:09:04 +02:00
Joris Vink bb3b804222 add beginnings of zlib inflation, not really working. nap time. 2013-04-29 23:35:36 +02:00
Joris Vink c8b422d29d allow us to expand receive buffers automatically so we can keep chaining data into the same netbuf. This gives us the possibility to retain the ctrl_frame by the time we reach the proper cb for the actual frame message. 2013-04-28 23:42:13 +02:00
Joris Vink 23c0ec67c6 begin with the ability to read control frames. something feels fishy with epoll() and its triggering of events. I probably got it wrong. 2013-04-28 19:11:44 +02:00
Joris Vink c6c6b1823f read next protocol properly and remember it for later. 2013-04-21 20:35:47 +02:00
Joris Vink 68d0523817 Add initial SSL stuff + bare bones NPN advertisement. So far chrome connects and
shows our connection as SPDY/3, good start.
2013-04-21 20:21:46 +02:00
Joris Vink 857c3f9121 first commit 2013-04-17 22:34:27 +02:00