2014-11-21 10:51:34 +00:00
|
|
|
/*
|
|
|
|
* MARS Long Distance Replication Software
|
|
|
|
*
|
|
|
|
* This file is part of MARS project: http://schoebel.github.io/mars/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2014 Thomas Schoebel-Theuer
|
|
|
|
* Copyright (C) 2011-2014 1&1 Internet AG
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
|
|
|
|
// Server brick (just for demonstration)
|
|
|
|
|
|
|
|
//#define BRICK_DEBUGGING
|
|
|
|
//#define MARS_DEBUGGING
|
|
|
|
//#define IO_DEBUGGING
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
|
|
|
|
#define _STRATEGY
|
|
|
|
#include "mars.h"
|
2012-02-20 09:34:42 +00:00
|
|
|
#include "mars_bio.h"
|
|
|
|
#include "mars_aio.h"
|
|
|
|
#include "mars_sio.h"
|
2011-02-23 20:48:06 +00:00
|
|
|
|
|
|
|
///////////////////////// own type definitions ////////////////////////
|
|
|
|
|
|
|
|
#include "mars_server.h"
|
|
|
|
|
2018-05-10 07:56:54 +00:00
|
|
|
struct server_cookie {
|
|
|
|
struct mars_socket server_socket;
|
|
|
|
struct mars_tcp_params *server_params;
|
|
|
|
};
|
|
|
|
|
2018-05-10 08:04:28 +00:00
|
|
|
static struct server_cookie server_cookie[MARS_TRAFFIC_MAX] = {
|
|
|
|
[MARS_TRAFFIC_META] = {
|
2018-05-10 09:16:34 +00:00
|
|
|
.server_params = &mars_tcp_params[MARS_TRAFFIC_META],
|
2018-05-10 07:56:54 +00:00
|
|
|
},
|
2018-05-10 08:04:28 +00:00
|
|
|
[MARS_TRAFFIC_REPLICATION] = {
|
2018-05-10 09:16:34 +00:00
|
|
|
.server_params = &mars_tcp_params[MARS_TRAFFIC_REPLICATION],
|
2018-05-10 07:56:54 +00:00
|
|
|
},
|
2018-05-10 08:04:28 +00:00
|
|
|
[MARS_TRAFFIC_SYNC] = {
|
2018-05-10 09:16:34 +00:00
|
|
|
.server_params = &mars_tcp_params[MARS_TRAFFIC_SYNC],
|
2018-05-10 07:56:54 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-05-10 08:04:28 +00:00
|
|
|
static struct task_struct *server_thread[MARS_TRAFFIC_MAX] = {};
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2013-01-09 18:20:10 +00:00
|
|
|
atomic_t server_handler_count = ATOMIC_INIT(0);
|
|
|
|
EXPORT_SYMBOL_GPL(server_handler_count);
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
///////////////////////// own helper functions ////////////////////////
|
|
|
|
|
2020-01-23 05:15:19 +00:00
|
|
|
#define HANDLER_LIMIT 1024
|
2017-09-07 09:03:13 +00:00
|
|
|
|
|
|
|
int handler_limit = HANDLER_LIMIT;
|
|
|
|
int handler_nr = HANDLER_LIMIT;
|
|
|
|
static struct semaphore handler_limit_sem = __SEMAPHORE_INITIALIZER(handler_limit_sem, HANDLER_LIMIT);
|
|
|
|
|
2017-09-09 19:24:47 +00:00
|
|
|
#define DENT_LIMIT 2
|
2017-09-11 13:29:09 +00:00
|
|
|
#define DENT_RETRY 5
|
2017-09-07 08:56:44 +00:00
|
|
|
|
|
|
|
int dent_limit = DENT_LIMIT;
|
|
|
|
int dent_nr = DENT_LIMIT;
|
|
|
|
static struct semaphore dent_limit_sem = __SEMAPHORE_INITIALIZER(dent_limit_sem, DENT_LIMIT);
|
2017-09-11 13:29:09 +00:00
|
|
|
int dent_retry = DENT_RETRY;
|
2017-09-07 08:56:44 +00:00
|
|
|
|
|
|
|
static
|
|
|
|
void change_sem(struct semaphore *sem, int *limit, int *nr)
|
|
|
|
{
|
|
|
|
if (unlikely(*nr < *limit)) {
|
|
|
|
up(sem);
|
|
|
|
(*nr)++;
|
|
|
|
} else if (unlikely(*nr > *limit)) {
|
|
|
|
if (!down_trylock(sem))
|
|
|
|
(*nr)--;
|
|
|
|
}
|
|
|
|
}
|
2011-03-03 09:02:10 +00:00
|
|
|
|
2011-05-26 14:32:32 +00:00
|
|
|
static
|
|
|
|
int cb_thread(void *data)
|
|
|
|
{
|
|
|
|
struct server_brick *brick = data;
|
2012-01-11 11:35:09 +00:00
|
|
|
struct mars_socket *sock = &brick->handler_socket;
|
2012-01-26 10:33:16 +00:00
|
|
|
bool aborted = false;
|
2012-01-11 11:35:09 +00:00
|
|
|
bool ok = mars_get_socket(sock);
|
2011-05-26 14:32:32 +00:00
|
|
|
int status = -EINVAL;
|
|
|
|
|
2012-01-11 11:35:09 +00:00
|
|
|
MARS_DBG("--------------- cb_thread starting on socket #%d, ok = %d\n", sock->s_debug_nr, ok);
|
|
|
|
if (!ok)
|
|
|
|
goto done;
|
2011-05-26 14:32:32 +00:00
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
brick->cb_running = true;
|
|
|
|
wake_up_interruptible(&brick->startup_event);
|
|
|
|
|
2012-11-13 16:01:37 +00:00
|
|
|
while (!brick_thread_should_stop() || !list_empty(&brick->cb_read_list) || !list_empty(&brick->cb_write_list) || atomic_read(&brick->in_flight) > 0) {
|
2011-05-26 14:32:32 +00:00
|
|
|
struct server_mref_aspect *mref_a;
|
|
|
|
struct mref_object *mref;
|
|
|
|
struct list_head *tmp;
|
2017-04-12 07:59:57 +00:00
|
|
|
bool cork;
|
2011-05-26 14:32:32 +00:00
|
|
|
unsigned long flags;
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2011-05-26 14:32:32 +00:00
|
|
|
wait_event_interruptible_timeout(
|
|
|
|
brick->cb_event,
|
|
|
|
!list_empty(&brick->cb_read_list) ||
|
2012-01-26 10:33:16 +00:00
|
|
|
!list_empty(&brick->cb_write_list),
|
|
|
|
1 * HZ);
|
2011-05-26 14:32:32 +00:00
|
|
|
|
|
|
|
traced_lock(&brick->cb_lock, flags);
|
|
|
|
tmp = brick->cb_write_list.next;
|
|
|
|
if (tmp == &brick->cb_write_list) {
|
|
|
|
tmp = brick->cb_read_list.next;
|
|
|
|
if (tmp == &brick->cb_read_list) {
|
|
|
|
traced_unlock(&brick->cb_lock, flags);
|
2012-09-17 10:11:25 +00:00
|
|
|
brick_msleep(1000 / HZ);
|
2011-05-26 14:32:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list_del_init(tmp);
|
2017-04-12 07:59:57 +00:00
|
|
|
cork = !list_empty(&brick->cb_write_list);
|
2011-05-26 14:32:32 +00:00
|
|
|
traced_unlock(&brick->cb_lock, flags);
|
|
|
|
|
|
|
|
mref_a = container_of(tmp, struct server_mref_aspect, cb_head);
|
|
|
|
mref = mref_a->object;
|
2011-08-31 11:42:04 +00:00
|
|
|
status = -EINVAL;
|
2011-05-26 14:32:32 +00:00
|
|
|
CHECK_PTR(mref, err);
|
|
|
|
|
2013-04-15 12:21:07 +00:00
|
|
|
status = 0;
|
2015-02-10 09:45:40 +00:00
|
|
|
/* Report a remote error when consistency cannot be guaranteed,
|
|
|
|
* e.g. emergency mode during sync.
|
|
|
|
*/
|
|
|
|
if (brick->conn_brick && brick->conn_brick->mode_ptr && *brick->conn_brick->mode_ptr < 0
|
|
|
|
&& mref->object_cb)
|
|
|
|
mref->object_cb->cb_error = *brick->conn_brick->mode_ptr;
|
2013-04-11 14:50:47 +00:00
|
|
|
if (!aborted) {
|
2020-03-30 20:31:33 +00:00
|
|
|
mref->ref_flags |= enabled_net_compressions;
|
2013-04-11 14:50:47 +00:00
|
|
|
down(&brick->socket_sem);
|
2017-04-12 07:59:57 +00:00
|
|
|
status = mars_send_cb(sock, mref, cork);
|
2013-04-11 14:50:47 +00:00
|
|
|
up(&brick->socket_sem);
|
|
|
|
}
|
2011-05-26 14:32:32 +00:00
|
|
|
|
2011-08-31 11:42:04 +00:00
|
|
|
err:
|
2012-01-26 10:33:16 +00:00
|
|
|
if (unlikely(status < 0) && !aborted) {
|
|
|
|
aborted = true;
|
2011-08-31 11:42:04 +00:00
|
|
|
MARS_WRN("cannot send response, status = %d\n", status);
|
2012-01-26 10:33:16 +00:00
|
|
|
/* Just shutdown the socket and forget all pending
|
|
|
|
* requests.
|
|
|
|
* The _client_ is responsible for resending
|
|
|
|
* any lost operations.
|
|
|
|
*/
|
2011-08-31 11:42:04 +00:00
|
|
|
mars_shutdown_socket(sock);
|
2011-05-26 14:32:32 +00:00
|
|
|
}
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2015-10-13 14:00:31 +00:00
|
|
|
if (mref_a->data) {
|
|
|
|
brick_block_free(mref_a->data, mref_a->len);
|
2020-06-15 13:42:01 +00:00
|
|
|
mref_a->data = NULL;
|
2015-10-13 14:00:31 +00:00
|
|
|
mref->ref_data = NULL;
|
|
|
|
}
|
2014-03-21 06:31:55 +00:00
|
|
|
if (mref_a->do_put) {
|
|
|
|
GENERIC_INPUT_CALL(brick->inputs[0], mref_put, mref);
|
|
|
|
atomic_dec(&brick->in_flight);
|
|
|
|
} else {
|
|
|
|
mars_free_mref(mref);
|
|
|
|
}
|
2011-05-26 14:32:32 +00:00
|
|
|
}
|
2013-01-22 11:13:09 +00:00
|
|
|
|
|
|
|
mars_shutdown_socket(sock);
|
|
|
|
mars_put_socket(sock);
|
|
|
|
|
2012-01-11 11:35:09 +00:00
|
|
|
done:
|
2011-05-26 14:32:32 +00:00
|
|
|
MARS_DBG("---------- cb_thread terminating, status = %d\n", status);
|
|
|
|
wake_up_interruptible(&brick->startup_event);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void server_endio(struct generic_callback *cb)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
|
|
|
struct server_mref_aspect *mref_a;
|
|
|
|
struct mref_object *mref;
|
|
|
|
struct server_brick *brick;
|
2011-05-26 14:32:32 +00:00
|
|
|
unsigned long flags;
|
2011-02-23 20:48:06 +00:00
|
|
|
|
|
|
|
mref_a = cb->cb_private;
|
|
|
|
CHECK_PTR(mref_a, err);
|
2011-05-26 14:32:32 +00:00
|
|
|
mref = mref_a->object;
|
|
|
|
CHECK_PTR(mref, err);
|
2014-03-23 09:57:56 +00:00
|
|
|
LAST_CALLBACK(cb);
|
|
|
|
if (unlikely(cb != &mref->_object_cb)) {
|
|
|
|
MARS_ERR("bad cb pointer %p != %p\n", cb, &mref->_object_cb);
|
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2011-08-31 11:42:04 +00:00
|
|
|
brick = mref_a->brick;
|
2014-03-23 09:57:56 +00:00
|
|
|
if (unlikely(!brick)) {
|
2011-08-31 11:42:04 +00:00
|
|
|
MARS_WRN("late IO callback -- cannot do anything\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:32:32 +00:00
|
|
|
traced_lock(&brick->cb_lock, flags);
|
2019-03-14 20:11:51 +00:00
|
|
|
if (mref->ref_flags & MREF_WRITE) {
|
2011-05-26 14:32:32 +00:00
|
|
|
list_add_tail(&mref_a->cb_head, &brick->cb_write_list);
|
|
|
|
} else {
|
|
|
|
list_add_tail(&mref_a->cb_head, &brick->cb_read_list);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2011-05-26 14:32:32 +00:00
|
|
|
traced_unlock(&brick->cb_lock, flags);
|
|
|
|
|
|
|
|
wake_up_interruptible(&brick->cb_event);
|
2011-02-23 20:48:06 +00:00
|
|
|
return;
|
|
|
|
err:
|
|
|
|
MARS_FAT("cannot handle callback - giving up\n");
|
|
|
|
}
|
|
|
|
|
2014-03-21 06:31:55 +00:00
|
|
|
static
|
2012-08-01 10:09:49 +00:00
|
|
|
int server_io(struct server_brick *brick, struct mars_socket *sock, struct mars_cmd *cmd)
|
2011-03-22 14:36:26 +00:00
|
|
|
{
|
|
|
|
struct mref_object *mref;
|
|
|
|
struct server_mref_aspect *mref_a;
|
2012-08-07 12:07:12 +00:00
|
|
|
int amount;
|
2011-05-26 14:32:32 +00:00
|
|
|
int status = -ENOTRECOVERABLE;
|
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
if (!brick->cb_running || !brick->handler_running || !mars_socket_is_alive(sock))
|
2011-05-26 14:32:32 +00:00
|
|
|
goto done;
|
|
|
|
|
2012-02-02 15:25:43 +00:00
|
|
|
mref = server_alloc_mref(brick);
|
2011-03-22 14:36:26 +00:00
|
|
|
status = -ENOMEM;
|
|
|
|
if (!mref)
|
|
|
|
goto done;
|
|
|
|
|
2011-10-03 17:31:02 +00:00
|
|
|
mref_a = server_mref_get_aspect(brick, mref);
|
2011-03-22 14:36:26 +00:00
|
|
|
if (unlikely(!mref_a)) {
|
|
|
|
mars_free_mref(mref);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-08-01 10:09:49 +00:00
|
|
|
status = mars_recv_mref(sock, mref, cmd);
|
2011-03-22 14:36:26 +00:00
|
|
|
if (status < 0) {
|
|
|
|
mars_free_mref(mref);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
mref_a->brick = brick;
|
2015-10-13 14:00:31 +00:00
|
|
|
mref_a->data = mref->ref_data;
|
|
|
|
mref_a->len = mref->ref_len;
|
2011-10-04 11:34:18 +00:00
|
|
|
SETUP_CALLBACK(mref, server_endio, mref_a);
|
2012-08-07 12:07:12 +00:00
|
|
|
|
|
|
|
amount = 0;
|
2019-03-25 08:12:45 +00:00
|
|
|
if (!(mref->ref_flags & MREF_NODATA))
|
2012-08-07 12:07:12 +00:00
|
|
|
amount = (mref->ref_len - 1) / 1024 + 1;
|
|
|
|
mars_limit_sleep(&server_limiter, amount);
|
2011-03-22 14:36:26 +00:00
|
|
|
|
|
|
|
status = GENERIC_INPUT_CALL(brick->inputs[0], mref_get, mref);
|
2012-01-22 17:15:30 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_WRN("mref_get execution error = %d\n", status);
|
2011-10-04 11:34:18 +00:00
|
|
|
SIMPLE_CALLBACK(mref, status);
|
2011-03-22 14:36:26 +00:00
|
|
|
status = 0; // continue serving requests
|
|
|
|
goto done;
|
|
|
|
}
|
2014-03-21 06:31:55 +00:00
|
|
|
mref_a->do_put = true;
|
2012-01-22 17:15:30 +00:00
|
|
|
atomic_inc(&brick->in_flight);
|
2011-03-22 14:36:26 +00:00
|
|
|
GENERIC_INPUT_CALL(brick->inputs[0], mref_io, mref);
|
|
|
|
|
|
|
|
done:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:32:32 +00:00
|
|
|
static
|
|
|
|
void _clean_list(struct server_brick *brick, struct list_head *start)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
struct server_mref_aspect *mref_a;
|
|
|
|
struct mref_object *mref;
|
|
|
|
struct list_head *tmp = start->next;
|
|
|
|
if (tmp == start)
|
|
|
|
break;
|
|
|
|
|
|
|
|
list_del_init(tmp);
|
|
|
|
|
|
|
|
mref_a = container_of(tmp, struct server_mref_aspect, cb_head);
|
2011-08-31 11:42:04 +00:00
|
|
|
mref_a->brick = NULL;
|
2011-05-26 14:32:32 +00:00
|
|
|
mref = mref_a->object;
|
|
|
|
if (!mref)
|
|
|
|
continue;
|
|
|
|
|
2014-03-21 06:31:55 +00:00
|
|
|
if (mref_a->do_put) {
|
|
|
|
GENERIC_INPUT_CALL(brick->inputs[0], mref_put, mref);
|
|
|
|
atomic_dec(&brick->in_flight);
|
|
|
|
} else {
|
|
|
|
mars_free_mref(mref);
|
|
|
|
}
|
2011-05-26 14:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-20 09:34:42 +00:00
|
|
|
static
|
|
|
|
int _set_server_sio_params(struct mars_brick *_brick, void *private)
|
|
|
|
{
|
|
|
|
struct sio_brick *sio_brick = (void*)_brick;
|
|
|
|
if (_brick->type != (void*)_sio_brick_type) {
|
|
|
|
MARS_ERR("bad brick type\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
sio_brick->o_direct = false;
|
|
|
|
sio_brick->o_fdsync = false;
|
|
|
|
MARS_INF("name = '%s' path = '%s'\n", _brick->brick_name, _brick->brick_path);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int _set_server_aio_params(struct mars_brick *_brick, void *private)
|
|
|
|
{
|
|
|
|
struct aio_brick *aio_brick = (void*)_brick;
|
|
|
|
if (_brick->type == (void*)_sio_brick_type) {
|
|
|
|
return _set_server_sio_params(_brick, private);
|
|
|
|
}
|
|
|
|
if (_brick->type != (void*)_aio_brick_type) {
|
|
|
|
MARS_ERR("bad brick type\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2012-12-26 18:35:49 +00:00
|
|
|
aio_brick->o_creat = false;
|
2012-02-20 09:34:42 +00:00
|
|
|
aio_brick->o_direct = false;
|
|
|
|
aio_brick->o_fdsync = false;
|
|
|
|
MARS_INF("name = '%s' path = '%s'\n", _brick->brick_name, _brick->brick_path);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int _set_server_bio_params(struct mars_brick *_brick, void *private)
|
|
|
|
{
|
|
|
|
struct bio_brick *bio_brick;
|
|
|
|
if (_brick->type == (void*)_aio_brick_type) {
|
|
|
|
return _set_server_aio_params(_brick, private);
|
|
|
|
}
|
|
|
|
if (_brick->type == (void*)_sio_brick_type) {
|
|
|
|
return _set_server_sio_params(_brick, private);
|
|
|
|
}
|
|
|
|
if (_brick->type != (void*)_bio_brick_type) {
|
|
|
|
MARS_ERR("bad brick type\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
bio_brick = (void*)_brick;
|
2012-08-07 12:07:12 +00:00
|
|
|
bio_brick->ra_pages = 0;
|
2012-02-20 09:34:42 +00:00
|
|
|
bio_brick->do_sync = true;
|
|
|
|
bio_brick->do_unplug = true;
|
|
|
|
MARS_INF("name = '%s' path = '%s'\n", _brick->brick_name, _brick->brick_path);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:32:32 +00:00
|
|
|
static
|
|
|
|
int handler_thread(void *data)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
2019-08-13 10:17:20 +00:00
|
|
|
struct mars_global *handler_global = alloc_mars_global();
|
2014-11-28 12:25:29 +00:00
|
|
|
struct task_struct *thread = NULL;
|
2011-02-23 20:48:06 +00:00
|
|
|
struct server_brick *brick = data;
|
2012-01-11 11:35:09 +00:00
|
|
|
struct mars_socket *sock = &brick->handler_socket;
|
2013-01-22 11:13:09 +00:00
|
|
|
bool ok = mars_get_socket(sock);
|
2014-11-28 12:25:29 +00:00
|
|
|
unsigned long statist_jiffies = jiffies;
|
2012-11-30 09:19:50 +00:00
|
|
|
int debug_nr;
|
2019-07-12 08:10:28 +00:00
|
|
|
int old_proto_level = 0;
|
2013-01-22 11:13:09 +00:00
|
|
|
int status = -EINVAL;
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
MARS_DBG("#%d --------------- handler_thread starting on socket %p\n", sock->s_debug_nr, sock);
|
|
|
|
if (!ok)
|
|
|
|
goto done;
|
|
|
|
|
2014-11-28 12:25:29 +00:00
|
|
|
thread = brick_thread_create(cb_thread, brick, "mars_cb%d", brick->version);
|
|
|
|
if (unlikely(!thread)) {
|
|
|
|
MARS_ERR("cannot create cb thread\n");
|
|
|
|
status = -ENOENT;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
brick->cb_thread = thread;
|
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
brick->handler_running = true;
|
2011-02-23 20:48:06 +00:00
|
|
|
wake_up_interruptible(&brick->startup_event);
|
|
|
|
|
2019-08-13 10:17:20 +00:00
|
|
|
while (!list_empty(&handler_global->brick_anchor) ||
|
2014-11-28 12:25:29 +00:00
|
|
|
mars_socket_is_alive(sock)) {
|
2011-02-23 20:48:06 +00:00
|
|
|
struct mars_cmd cmd = {};
|
|
|
|
|
2019-08-13 10:17:20 +00:00
|
|
|
handler_global->global_version++;
|
2014-11-28 12:25:29 +00:00
|
|
|
|
2019-08-13 10:17:20 +00:00
|
|
|
if (!list_empty(&handler_global->brick_anchor)) {
|
2014-11-28 12:25:29 +00:00
|
|
|
if (server_show_statist && !time_is_before_jiffies(statist_jiffies + 10 * HZ)) {
|
2019-08-13 10:17:20 +00:00
|
|
|
show_statistics(handler_global, "handler");
|
2014-11-28 12:25:29 +00:00
|
|
|
statist_jiffies = jiffies;
|
|
|
|
}
|
|
|
|
if (!mars_socket_is_alive(sock) &&
|
|
|
|
atomic_read(&brick->in_flight) <= 0 &&
|
|
|
|
brick->conn_brick) {
|
2016-12-31 22:03:09 +00:00
|
|
|
if (mars_disconnect((void*)brick->inputs[0]) >= 0)
|
2014-11-28 12:25:29 +00:00
|
|
|
brick->conn_brick = NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-13 10:17:20 +00:00
|
|
|
status = mars_kill_brick_when_possible(handler_global,
|
2020-03-07 05:31:35 +00:00
|
|
|
NULL, true);
|
2014-11-28 12:25:29 +00:00
|
|
|
MARS_DBG("kill handler bricks (when possible) = %d\n", status);
|
|
|
|
}
|
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
status = -EINTR;
|
2013-01-09 18:20:10 +00:00
|
|
|
if (unlikely(!mars_global || !mars_global->global_power.button)) {
|
|
|
|
MARS_DBG("system is not alive\n");
|
2014-12-06 18:15:29 +00:00
|
|
|
goto clean;
|
|
|
|
}
|
|
|
|
if (unlikely(brick_thread_should_stop())) {
|
|
|
|
goto clean;
|
|
|
|
}
|
|
|
|
if (unlikely(!mars_socket_is_alive(sock))) {
|
|
|
|
/* Dont read any data anymore, the protocol
|
|
|
|
* may be screwed up completely.
|
|
|
|
*/
|
|
|
|
MARS_DBG("#%d is dead\n", sock->s_debug_nr);
|
|
|
|
goto clean;
|
2013-01-09 18:20:10 +00:00
|
|
|
}
|
2017-09-07 09:03:13 +00:00
|
|
|
if (down_trylock(&handler_limit_sem)) {
|
|
|
|
MARS_DBG("#%d handler limit reached\n", sock->s_debug_nr);
|
|
|
|
status = -EUSERS;
|
|
|
|
goto clean;
|
|
|
|
}
|
2013-01-09 18:20:10 +00:00
|
|
|
|
2019-07-11 07:34:43 +00:00
|
|
|
status = mars_recv_cmd(sock, &cmd);
|
2012-11-30 09:19:50 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_WRN("#%d recv cmd status = %d\n", sock->s_debug_nr, status);
|
2017-09-07 09:03:13 +00:00
|
|
|
goto clean_unlock;
|
2012-11-30 09:19:50 +00:00
|
|
|
}
|
2014-12-06 18:15:29 +00:00
|
|
|
|
|
|
|
MARS_IO("#%d cmd = %d\n", sock->s_debug_nr, cmd.cmd_code);
|
|
|
|
|
|
|
|
if (unlikely(!brick->global || !mars_global || !mars_global->global_power.button)) {
|
|
|
|
MARS_WRN("#%d system is not alive\n", sock->s_debug_nr);
|
2013-01-22 11:13:09 +00:00
|
|
|
status = -EINTR;
|
2017-09-07 09:03:13 +00:00
|
|
|
goto clean_unlock;
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status = -EPROTO;
|
2012-08-01 10:09:49 +00:00
|
|
|
switch (cmd.cmd_code & CMD_FLAG_MASK) {
|
2011-02-23 20:48:06 +00:00
|
|
|
case CMD_NOP:
|
|
|
|
status = 0;
|
2013-05-11 20:52:11 +00:00
|
|
|
MARS_DBG("#%d got NOP operation\n", sock->s_debug_nr);
|
2011-02-23 20:48:06 +00:00
|
|
|
break;
|
2012-01-17 14:37:14 +00:00
|
|
|
case CMD_NOTIFY:
|
2013-05-11 20:52:11 +00:00
|
|
|
status = 0;
|
|
|
|
from_remote_trigger();
|
2011-02-23 20:48:06 +00:00
|
|
|
break;
|
|
|
|
case CMD_GETINFO:
|
|
|
|
{
|
|
|
|
struct mars_info info = {};
|
|
|
|
status = GENERIC_INPUT_CALL(brick->inputs[0], mars_get_info, &info);
|
|
|
|
if (status < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2011-05-27 13:02:50 +00:00
|
|
|
down(&brick->socket_sem);
|
2019-07-11 07:34:43 +00:00
|
|
|
status = mars_send_cmd(sock, &cmd, true);
|
2019-07-12 08:10:28 +00:00
|
|
|
old_proto_level = sock->s_common_proto_level;
|
2013-01-14 20:19:45 +00:00
|
|
|
if (status >= 0) {
|
2017-04-12 07:50:19 +00:00
|
|
|
status = mars_send_struct(sock, &info, mars_info_meta, false);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2011-05-27 13:02:50 +00:00
|
|
|
up(&brick->socket_sem);
|
2011-02-23 20:48:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_GETENTS:
|
|
|
|
{
|
2017-01-31 07:58:02 +00:00
|
|
|
char *path = cmd.cmd_str1 ? cmd.cmd_str1 : "/mars";
|
2017-09-11 13:29:09 +00:00
|
|
|
int max_retry = dent_retry;
|
|
|
|
|
|
|
|
while (down_trylock(&dent_limit_sem)) {
|
|
|
|
if (max_retry-- <= 0) {
|
|
|
|
MARS_DBG("#%d dent limit reached\n", sock->s_debug_nr);
|
|
|
|
status = -EUSERS;
|
2017-09-17 07:08:19 +00:00
|
|
|
goto clean_unlock;
|
2017-09-11 13:29:09 +00:00
|
|
|
}
|
|
|
|
brick_msleep(1000);
|
2017-09-07 08:56:44 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 08:10:28 +00:00
|
|
|
/* New protocol.
|
|
|
|
* We cannot send/recv intermediate cmds at the
|
|
|
|
* old protocol.
|
|
|
|
* For compatibility, the old protocol must be
|
|
|
|
* used until the fist cmd response has been sent.
|
|
|
|
*/
|
|
|
|
if (sock->s_common_proto_level > 0 &&
|
|
|
|
old_proto_level > 0) {
|
|
|
|
down(&brick->socket_sem);
|
|
|
|
status = mars_send_cmd(sock, &cmd, true);
|
|
|
|
old_proto_level = sock->s_common_proto_level;
|
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_WRN("#%d could not send inter_cmd, status = %d\n",
|
|
|
|
sock->s_debug_nr, status);
|
|
|
|
}
|
|
|
|
up(&brick->socket_sem);
|
|
|
|
}
|
|
|
|
|
2020-03-06 18:00:39 +00:00
|
|
|
status = mars_get_dent_list(
|
2019-08-13 10:17:20 +00:00
|
|
|
handler_global,
|
2017-01-31 07:58:02 +00:00
|
|
|
path,
|
|
|
|
sizeof(struct mars_dent),
|
|
|
|
main_checker,
|
|
|
|
3);
|
2012-12-18 05:31:24 +00:00
|
|
|
|
2017-09-07 08:56:44 +00:00
|
|
|
up(&dent_limit_sem);
|
|
|
|
|
2011-05-27 13:02:50 +00:00
|
|
|
down(&brick->socket_sem);
|
2019-08-13 10:17:20 +00:00
|
|
|
status = mars_send_dent_list(sock, &handler_global->dent_anchor);
|
2011-05-27 13:02:50 +00:00
|
|
|
up(&brick->socket_sem);
|
2011-06-10 13:57:52 +00:00
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
if (status < 0) {
|
2012-11-30 09:19:50 +00:00
|
|
|
MARS_WRN("#%d could not send dentry information, status = %d\n", sock->s_debug_nr, status);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2012-12-18 05:31:24 +00:00
|
|
|
|
2019-08-13 10:17:20 +00:00
|
|
|
mars_free_dent_all(handler_global, &handler_global->dent_anchor);
|
2011-02-23 20:48:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_CONNECT:
|
|
|
|
{
|
|
|
|
struct mars_brick *prev;
|
2011-03-01 18:00:14 +00:00
|
|
|
const char *path = cmd.cmd_str1;
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2011-03-01 18:00:14 +00:00
|
|
|
status = -EINVAL;
|
|
|
|
CHECK_PTR(path, err);
|
2011-09-02 12:17:40 +00:00
|
|
|
CHECK_PTR_NULL(_bio_brick_type, err);
|
2011-03-01 18:00:14 +00:00
|
|
|
|
2011-05-26 14:32:32 +00:00
|
|
|
prev = make_brick_all(
|
2019-08-13 10:17:20 +00:00
|
|
|
handler_global,
|
2011-05-26 14:32:32 +00:00
|
|
|
NULL,
|
2012-02-20 09:34:42 +00:00
|
|
|
_set_server_bio_params,
|
2011-05-26 14:32:32 +00:00
|
|
|
NULL,
|
|
|
|
path,
|
|
|
|
(const struct generic_brick_type*)_bio_brick_type,
|
|
|
|
(const struct generic_brick_type*[]){},
|
2013-05-10 06:15:00 +00:00
|
|
|
2, // start always
|
2011-05-26 14:32:32 +00:00
|
|
|
path,
|
|
|
|
(const char *[]){},
|
|
|
|
0);
|
2011-03-01 18:00:14 +00:00
|
|
|
if (likely(prev)) {
|
2016-12-31 22:03:09 +00:00
|
|
|
status = mars_connect((void *)brick->inputs[0], prev->outputs[0]);
|
2013-01-22 11:10:12 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_ERR("#%d cannot connect to '%s'\n", sock->s_debug_nr, path);
|
|
|
|
}
|
|
|
|
prev->killme = true;
|
2014-11-28 12:25:29 +00:00
|
|
|
brick->conn_brick = prev;
|
2011-02-23 20:48:06 +00:00
|
|
|
} else {
|
2012-11-30 09:19:50 +00:00
|
|
|
MARS_ERR("#%d cannot find brick '%s'\n", sock->s_debug_nr, path);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2011-03-01 18:00:14 +00:00
|
|
|
|
|
|
|
err:
|
2011-02-23 20:48:06 +00:00
|
|
|
cmd.cmd_int1 = status;
|
2011-05-27 13:02:50 +00:00
|
|
|
down(&brick->socket_sem);
|
2019-07-11 07:34:43 +00:00
|
|
|
status = mars_send_cmd(sock, &cmd, false);
|
2019-07-12 08:10:28 +00:00
|
|
|
old_proto_level = sock->s_common_proto_level;
|
2011-05-27 13:02:50 +00:00
|
|
|
up(&brick->socket_sem);
|
2011-02-23 20:48:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_MREF:
|
2012-02-08 18:12:57 +00:00
|
|
|
{
|
2012-08-01 10:09:49 +00:00
|
|
|
status = server_io(brick, sock, &cmd);
|
2011-02-23 20:48:06 +00:00
|
|
|
break;
|
2012-02-08 18:12:57 +00:00
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
case CMD_CB:
|
2012-11-30 09:19:50 +00:00
|
|
|
MARS_ERR("#%d oops, as a server I should never get CMD_CB; something is wrong here - attack attempt??\n", sock->s_debug_nr);
|
2011-02-23 20:48:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-11-30 09:19:50 +00:00
|
|
|
MARS_ERR("#%d unknown command %d\n", sock->s_debug_nr, cmd.cmd_code);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2017-09-07 09:03:13 +00:00
|
|
|
clean_unlock:
|
|
|
|
up(&handler_limit_sem);
|
2013-01-22 11:13:09 +00:00
|
|
|
clean:
|
2011-08-25 10:16:32 +00:00
|
|
|
brick_string_free(cmd.cmd_str1);
|
2014-12-06 18:15:29 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
mars_shutdown_socket(sock);
|
|
|
|
brick_msleep(1000);
|
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 11:42:04 +00:00
|
|
|
mars_shutdown_socket(sock);
|
2013-01-22 11:13:09 +00:00
|
|
|
mars_put_socket(sock);
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
done:
|
2012-11-30 09:19:50 +00:00
|
|
|
MARS_DBG("#%d handler_thread terminating, status = %d\n", sock->s_debug_nr, status);
|
2011-05-26 14:32:32 +00:00
|
|
|
|
2019-08-13 10:17:20 +00:00
|
|
|
mars_kill_brick_all(handler_global, &handler_global->brick_anchor, false);
|
2014-11-28 12:25:29 +00:00
|
|
|
|
|
|
|
if (thread) {
|
|
|
|
brick->cb_thread = NULL;
|
|
|
|
brick->cb_running = false;
|
|
|
|
MARS_DBG("#%d stopping callback thread....\n", sock->s_debug_nr);
|
|
|
|
brick_thread_stop(thread);
|
|
|
|
}
|
|
|
|
|
2012-11-30 09:19:50 +00:00
|
|
|
debug_nr = sock->s_debug_nr;
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2012-11-30 09:19:50 +00:00
|
|
|
MARS_DBG("#%d done.\n", debug_nr);
|
2013-01-09 18:20:10 +00:00
|
|
|
atomic_dec(&server_handler_count);
|
2013-01-22 11:13:09 +00:00
|
|
|
brick->killme = true;
|
2019-08-13 10:17:20 +00:00
|
|
|
brick_mem_free(handler_global);
|
2011-08-31 11:42:04 +00:00
|
|
|
return status;
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////// own brick / input / output operations //////////////////
|
|
|
|
|
|
|
|
static int server_get_info(struct server_output *output, struct mars_info *info)
|
|
|
|
{
|
|
|
|
struct server_input *input = output->brick->inputs[0];
|
|
|
|
return GENERIC_INPUT_CALL(input, mars_get_info, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int server_ref_get(struct server_output *output, struct mref_object *mref)
|
|
|
|
{
|
|
|
|
struct server_input *input = output->brick->inputs[0];
|
|
|
|
return GENERIC_INPUT_CALL(input, mref_get, mref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_ref_put(struct server_output *output, struct mref_object *mref)
|
|
|
|
{
|
|
|
|
struct server_input *input = output->brick->inputs[0];
|
|
|
|
GENERIC_INPUT_CALL(input, mref_put, mref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void server_ref_io(struct server_output *output, struct mref_object *mref)
|
|
|
|
{
|
|
|
|
struct server_input *input = output->brick->inputs[0];
|
|
|
|
GENERIC_INPUT_CALL(input, mref_io, mref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int server_switch(struct server_brick *brick)
|
|
|
|
{
|
2013-01-22 11:13:09 +00:00
|
|
|
struct mars_socket *sock = &brick->handler_socket;
|
2011-08-31 11:42:04 +00:00
|
|
|
int status = 0;
|
2013-01-22 11:13:09 +00:00
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
if (brick->power.button) {
|
2011-08-31 11:42:04 +00:00
|
|
|
static int version = 0;
|
2013-09-25 15:07:19 +00:00
|
|
|
bool ok;
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2013-09-25 15:07:19 +00:00
|
|
|
if (brick->power.led_on)
|
2013-09-25 15:12:11 +00:00
|
|
|
goto done;
|
2013-09-25 15:07:19 +00:00
|
|
|
|
|
|
|
ok = mars_get_socket(sock);
|
2013-01-22 11:13:09 +00:00
|
|
|
if (unlikely(!ok)) {
|
|
|
|
status = -ENOENT;
|
|
|
|
goto err;
|
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
mars_power_led_off((void*)brick, false);
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2014-11-28 12:25:29 +00:00
|
|
|
brick->version = version++;
|
|
|
|
brick->handler_thread = brick_thread_create(handler_thread, brick, "mars_handler%d", brick->version);
|
2012-11-13 16:01:37 +00:00
|
|
|
if (unlikely(!brick->handler_thread)) {
|
|
|
|
MARS_ERR("cannot create handler thread\n");
|
|
|
|
status = -ENOENT;
|
2011-08-31 11:42:04 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
mars_power_led_on((void*)brick, true);
|
2013-09-25 15:07:19 +00:00
|
|
|
} else if (!brick->power.led_off) {
|
2011-08-31 11:42:04 +00:00
|
|
|
struct task_struct *thread;
|
2011-02-23 20:48:06 +00:00
|
|
|
mars_power_led_on((void*)brick, false);
|
2013-04-15 16:11:22 +00:00
|
|
|
|
2012-01-11 11:35:09 +00:00
|
|
|
mars_shutdown_socket(sock);
|
2013-04-15 16:11:22 +00:00
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
thread = brick->handler_thread;
|
2011-08-31 11:42:04 +00:00
|
|
|
if (thread) {
|
|
|
|
brick->handler_thread = NULL;
|
2013-01-22 11:13:09 +00:00
|
|
|
brick->handler_running = false;
|
|
|
|
MARS_DBG("#%d stopping handler thread....\n", sock->s_debug_nr);
|
2012-11-13 16:01:37 +00:00
|
|
|
brick_thread_stop(thread);
|
2011-08-31 11:42:04 +00:00
|
|
|
}
|
2013-04-15 16:11:22 +00:00
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
mars_put_socket(sock);
|
|
|
|
MARS_DBG("#%d socket s_count = %d\n", sock->s_debug_nr, atomic_read(&sock->s_count));
|
2013-04-15 16:11:22 +00:00
|
|
|
|
|
|
|
// do this only after _both_ threads have stopped...
|
|
|
|
_clean_list(brick, &brick->cb_read_list);
|
|
|
|
_clean_list(brick, &brick->cb_write_list);
|
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
mars_power_led_off((void*)brick, true);
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
if (unlikely(status < 0)) {
|
2011-09-02 12:17:40 +00:00
|
|
|
mars_power_led_off((void*)brick, true);
|
2013-09-25 15:12:11 +00:00
|
|
|
mars_shutdown_socket(sock);
|
|
|
|
mars_put_socket(sock);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2013-09-25 15:12:11 +00:00
|
|
|
done:
|
2011-08-31 11:42:04 +00:00
|
|
|
return status;
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
|
2012-01-22 17:15:30 +00:00
|
|
|
//////////////// informational / statistics ///////////////
|
|
|
|
|
|
|
|
static
|
|
|
|
char *server_statistics(struct server_brick *brick, int verbose)
|
|
|
|
{
|
|
|
|
char *res = brick_string_alloc(1024);
|
|
|
|
if (!res)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
snprintf(res, 1024,
|
2012-02-12 11:19:57 +00:00
|
|
|
"cb_running = %d "
|
2013-01-22 11:13:09 +00:00
|
|
|
"handler_running = %d "
|
2012-02-12 11:19:57 +00:00
|
|
|
"in_flight = %d\n",
|
|
|
|
brick->cb_running,
|
2013-01-22 11:13:09 +00:00
|
|
|
brick->handler_running,
|
2012-01-22 17:15:30 +00:00
|
|
|
atomic_read(&brick->in_flight));
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void server_reset_statistics(struct server_brick *brick)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
//////////////// object / aspect constructors / destructors ///////////////
|
|
|
|
|
2011-10-03 17:31:02 +00:00
|
|
|
static int server_mref_aspect_init_fn(struct generic_aspect *_ini)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
|
|
|
struct server_mref_aspect *ini = (void*)_ini;
|
2011-05-26 14:32:32 +00:00
|
|
|
INIT_LIST_HEAD(&ini->cb_head);
|
2011-02-23 20:48:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-03 17:31:02 +00:00
|
|
|
static void server_mref_aspect_exit_fn(struct generic_aspect *_ini)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
|
|
|
struct server_mref_aspect *ini = (void*)_ini;
|
2011-05-26 14:32:32 +00:00
|
|
|
CHECK_HEAD_EMPTY(&ini->cb_head);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MARS_MAKE_STATICS(server);
|
|
|
|
|
|
|
|
////////////////////// brick constructors / destructors ////////////////////
|
|
|
|
|
|
|
|
static int server_brick_construct(struct server_brick *brick)
|
|
|
|
{
|
|
|
|
init_waitqueue_head(&brick->startup_event);
|
2011-05-26 14:32:32 +00:00
|
|
|
init_waitqueue_head(&brick->cb_event);
|
2011-02-23 20:48:06 +00:00
|
|
|
sema_init(&brick->socket_sem, 1);
|
2011-05-26 14:32:32 +00:00
|
|
|
spin_lock_init(&brick->cb_lock);
|
|
|
|
INIT_LIST_HEAD(&brick->cb_read_list);
|
|
|
|
INIT_LIST_HEAD(&brick->cb_write_list);
|
2011-02-23 20:48:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-19 18:30:06 +00:00
|
|
|
static int server_brick_destruct(struct server_brick *brick)
|
|
|
|
{
|
|
|
|
CHECK_HEAD_EMPTY(&brick->cb_read_list);
|
|
|
|
CHECK_HEAD_EMPTY(&brick->cb_write_list);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
static int server_output_construct(struct server_output *output)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////// static structs ////////////////////////
|
|
|
|
|
|
|
|
static struct server_brick_ops server_brick_ops = {
|
|
|
|
.brick_switch = server_switch,
|
2012-01-22 17:15:30 +00:00
|
|
|
.brick_statistics = server_statistics,
|
|
|
|
.reset_statistics = server_reset_statistics,
|
2011-02-23 20:48:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct server_output_ops server_output_ops = {
|
|
|
|
.mars_get_info = server_get_info,
|
|
|
|
.mref_get = server_ref_get,
|
|
|
|
.mref_put = server_ref_put,
|
|
|
|
.mref_io = server_ref_io,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct server_input_type server_input_type = {
|
|
|
|
.type_name = "server_input",
|
|
|
|
.input_size = sizeof(struct server_input),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct server_input_type *server_input_types[] = {
|
|
|
|
&server_input_type,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct server_output_type server_output_type = {
|
|
|
|
.type_name = "server_output",
|
|
|
|
.output_size = sizeof(struct server_output),
|
|
|
|
.master_ops = &server_output_ops,
|
|
|
|
.output_construct = &server_output_construct,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct server_output_type *server_output_types[] = {
|
|
|
|
&server_output_type,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct server_brick_type server_brick_type = {
|
|
|
|
.type_name = "server_brick",
|
|
|
|
.brick_size = sizeof(struct server_brick),
|
|
|
|
.max_inputs = 1,
|
|
|
|
.max_outputs = 0,
|
|
|
|
.master_ops = &server_brick_ops,
|
2011-10-03 17:31:02 +00:00
|
|
|
.aspect_types = server_aspect_types,
|
2011-02-23 20:48:06 +00:00
|
|
|
.default_input_types = server_input_types,
|
|
|
|
.default_output_types = server_output_types,
|
|
|
|
.brick_construct = &server_brick_construct,
|
2013-01-19 18:30:06 +00:00
|
|
|
.brick_destruct = &server_brick_destruct,
|
2011-02-23 20:48:06 +00:00
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(server_brick_type);
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// strategy layer
|
|
|
|
|
2013-04-12 07:59:44 +00:00
|
|
|
int server_show_statist = 0;
|
|
|
|
EXPORT_SYMBOL_GPL(server_show_statist);
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
static int _server_thread(void *data)
|
|
|
|
{
|
2019-08-13 10:21:30 +00:00
|
|
|
struct mars_global *server_global = alloc_mars_global();
|
2018-05-10 07:56:54 +00:00
|
|
|
struct server_cookie *cookie = data;
|
|
|
|
struct mars_socket *my_socket = &cookie->server_socket;
|
|
|
|
struct mars_tcp_params *my_params = cookie->server_params;
|
2011-02-23 20:48:06 +00:00
|
|
|
char *id = my_id();
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
MARS_INF("-------- server starting on host '%s' ----------\n", id);
|
|
|
|
|
2012-11-13 16:01:37 +00:00
|
|
|
while (!brick_thread_should_stop() &&
|
2011-10-18 08:20:10 +00:00
|
|
|
(!mars_global || !mars_global->global_power.button)) {
|
2011-11-03 16:23:34 +00:00
|
|
|
MARS_DBG("system did not start up\n");
|
2012-09-17 10:11:25 +00:00
|
|
|
brick_msleep(5000);
|
2011-10-18 08:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MARS_INF("-------- server now working on host '%s' ----------\n", id);
|
|
|
|
|
2019-08-13 10:21:30 +00:00
|
|
|
while (!brick_thread_should_stop() || !list_empty(&server_global->brick_anchor)) {
|
2013-01-22 11:13:09 +00:00
|
|
|
struct server_brick *brick = NULL;
|
|
|
|
struct mars_socket handler_socket = {};
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2017-09-07 09:03:13 +00:00
|
|
|
change_sem(&handler_limit_sem, &handler_limit, &handler_nr);
|
2017-09-07 08:56:44 +00:00
|
|
|
change_sem(&dent_limit_sem, &dent_limit, &dent_nr);
|
|
|
|
|
2019-08-13 10:21:30 +00:00
|
|
|
server_global->global_version++;
|
2017-06-01 07:15:00 +00:00
|
|
|
mars_limit(&server_limiter, 0);
|
2013-01-22 11:13:09 +00:00
|
|
|
|
2013-04-12 07:59:44 +00:00
|
|
|
if (server_show_statist)
|
2019-08-13 10:21:30 +00:00
|
|
|
show_statistics(server_global, "server");
|
2013-01-22 11:13:09 +00:00
|
|
|
|
2019-08-13 10:21:30 +00:00
|
|
|
status = mars_kill_brick_when_possible(server_global,
|
2020-03-07 05:31:35 +00:00
|
|
|
NULL, true);
|
2013-05-09 14:34:17 +00:00
|
|
|
MARS_DBG("kill server bricks (when possible) = %d\n", status);
|
2013-01-22 11:13:09 +00:00
|
|
|
|
|
|
|
if (!mars_global || !mars_global->global_power.button) {
|
2013-05-11 20:52:11 +00:00
|
|
|
brick_msleep(1000);
|
2013-01-22 11:13:09 +00:00
|
|
|
continue;
|
2012-01-11 11:35:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 07:29:06 +00:00
|
|
|
status = mars_accept_socket(&handler_socket,
|
|
|
|
my_socket,
|
2018-05-10 07:56:54 +00:00
|
|
|
my_params);
|
2013-01-22 11:13:09 +00:00
|
|
|
if (unlikely(status < 0 || !mars_socket_is_alive(&handler_socket))) {
|
2012-09-17 10:11:25 +00:00
|
|
|
brick_msleep(500);
|
2011-02-23 20:48:06 +00:00
|
|
|
if (status == -EAGAIN)
|
|
|
|
continue; // without error message
|
2011-08-31 11:42:04 +00:00
|
|
|
MARS_WRN("accept status = %d\n", status);
|
2013-05-11 20:52:11 +00:00
|
|
|
brick_msleep(1000);
|
2011-02-23 20:48:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-01-22 11:13:09 +00:00
|
|
|
handler_socket.s_shutdown_on_err = true;
|
|
|
|
|
|
|
|
MARS_DBG("got new connection #%d\n", handler_socket.s_debug_nr);
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2019-08-13 10:21:30 +00:00
|
|
|
brick = (void*)mars_make_brick(server_global, NULL,
|
|
|
|
&server_brick_type,
|
|
|
|
"handler", "handler");
|
2013-01-22 11:13:09 +00:00
|
|
|
if (!brick) {
|
|
|
|
MARS_ERR("cannot create server instance\n");
|
|
|
|
mars_shutdown_socket(&handler_socket);
|
|
|
|
mars_put_socket(&handler_socket);
|
|
|
|
brick_msleep(2000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy(&brick->handler_socket, &handler_socket, sizeof(struct mars_socket));
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2013-01-09 18:20:10 +00:00
|
|
|
atomic_inc(&server_handler_count);
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
/* TODO: check authorization.
|
|
|
|
*/
|
|
|
|
|
2011-08-31 11:42:04 +00:00
|
|
|
brick->power.button = true;
|
|
|
|
status = server_switch(brick);
|
2012-01-11 11:35:09 +00:00
|
|
|
if (unlikely(status < 0)) {
|
2011-08-31 11:42:04 +00:00
|
|
|
MARS_ERR("cannot switch on server brick, status = %d\n", status);
|
2011-02-23 20:48:06 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2011-05-26 14:32:32 +00:00
|
|
|
|
2013-01-22 11:13:09 +00:00
|
|
|
// further references are usually held by the threads
|
|
|
|
mars_put_socket(&brick->handler_socket);
|
|
|
|
|
|
|
|
/* fire and forget....
|
|
|
|
* the new instance is now responsible for itself.
|
|
|
|
*/
|
2012-01-11 11:35:09 +00:00
|
|
|
brick = NULL;
|
2013-01-22 11:13:09 +00:00
|
|
|
brick_msleep(100);
|
2011-02-23 20:48:06 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
err:
|
2012-01-11 11:35:09 +00:00
|
|
|
if (brick) {
|
2013-01-22 11:13:09 +00:00
|
|
|
mars_shutdown_socket(&brick->handler_socket);
|
2012-01-11 11:35:09 +00:00
|
|
|
mars_put_socket(&brick->handler_socket);
|
|
|
|
status = mars_kill_brick((void*)brick);
|
|
|
|
if (status < 0) {
|
|
|
|
BRICK_ERR("kill status = %d, giving up\n", status);
|
|
|
|
}
|
2012-01-19 15:57:10 +00:00
|
|
|
brick = NULL;
|
2013-01-09 18:20:10 +00:00
|
|
|
atomic_dec(&server_handler_count);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2013-05-11 20:52:11 +00:00
|
|
|
brick_msleep(2000);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MARS_INF("-------- cleaning up ----------\n");
|
|
|
|
|
2019-08-13 10:21:30 +00:00
|
|
|
mars_kill_brick_all(server_global, &server_global->brick_anchor, false);
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
//cleanup_mm();
|
|
|
|
|
|
|
|
MARS_INF("-------- done status = %d ----------\n", status);
|
2019-08-13 10:21:30 +00:00
|
|
|
brick_mem_free(server_global);
|
2011-02-23 20:48:06 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////// module init stuff /////////////////////////
|
|
|
|
|
2012-08-07 12:07:12 +00:00
|
|
|
struct mars_limiter server_limiter = {
|
2016-01-19 08:15:44 +00:00
|
|
|
/* Let all be zero */
|
2012-08-07 12:07:12 +00:00
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(server_limiter);
|
|
|
|
|
2014-04-23 11:16:26 +00:00
|
|
|
void exit_mars_server(void)
|
2013-04-09 13:03:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
MARS_INF("exit_server()\n");
|
|
|
|
server_unregister_brick_type();
|
|
|
|
|
2018-05-10 08:04:28 +00:00
|
|
|
for (i = 0; i < MARS_TRAFFIC_MAX; i++) {
|
2013-04-09 13:03:55 +00:00
|
|
|
if (server_thread[i]) {
|
|
|
|
MARS_INF("stopping server thread %d...\n", i);
|
|
|
|
brick_thread_stop(server_thread[i]);
|
|
|
|
}
|
|
|
|
MARS_INF("closing server socket %d...\n", i);
|
2018-05-10 07:56:54 +00:00
|
|
|
mars_put_socket(&server_cookie[i].server_socket);
|
2013-04-09 13:03:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 10:16:32 +00:00
|
|
|
int __init init_mars_server(void)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
2013-04-09 13:03:55 +00:00
|
|
|
int i;
|
2011-02-23 20:48:06 +00:00
|
|
|
|
|
|
|
MARS_INF("init_server()\n");
|
2011-08-26 10:31:56 +00:00
|
|
|
|
2018-05-10 08:04:28 +00:00
|
|
|
for (i = 0; i < MARS_TRAFFIC_MAX; i++) {
|
2013-04-09 13:03:55 +00:00
|
|
|
struct sockaddr_storage sockaddr = {};
|
|
|
|
char tmp[16];
|
|
|
|
int status;
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2013-04-09 13:03:55 +00:00
|
|
|
sprintf(tmp, ":%d", mars_net_default_port + i);
|
|
|
|
status = mars_create_sockaddr(&sockaddr, tmp);
|
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
exit_mars_server();
|
|
|
|
return status;
|
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2018-05-10 07:56:54 +00:00
|
|
|
status = mars_create_socket(&server_cookie[i].server_socket,
|
2018-05-10 07:25:34 +00:00
|
|
|
&sockaddr,
|
2018-05-10 07:56:54 +00:00
|
|
|
server_cookie[i].server_params,
|
2018-05-10 07:25:34 +00:00
|
|
|
true);
|
2013-04-09 13:03:55 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_ERR("could not create server socket %d, status = %d\n", i, status);
|
|
|
|
exit_mars_server();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-05-10 07:56:54 +00:00
|
|
|
server_thread[i] = brick_thread_create(_server_thread,
|
|
|
|
&server_cookie[i],
|
|
|
|
"mars_server_%d", i);
|
2013-04-09 13:03:55 +00:00
|
|
|
if (unlikely(!server_thread[i] || IS_ERR(server_thread[i]))) {
|
|
|
|
MARS_ERR("could not create server thread %d\n", i);
|
|
|
|
exit_mars_server();
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return server_register_brick_type();
|
|
|
|
}
|