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
|
|
|
|
|
|
|
//#define BRICK_DEBUGGING
|
|
|
|
//#define MARS_DEBUGGING
|
|
|
|
//#define IO_DEBUGGING
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
2014-04-01 09:24:46 +00:00
|
|
|
#include <linux/jiffies.h>
|
2011-02-23 20:48:06 +00:00
|
|
|
|
|
|
|
#include "mars.h"
|
|
|
|
|
|
|
|
///////////////////////// own type definitions ////////////////////////
|
|
|
|
|
|
|
|
#include "mars_client.h"
|
|
|
|
|
2013-04-12 12:13:21 +00:00
|
|
|
#define CLIENT_HASH_MAX (PAGE_SIZE / sizeof(struct list_head))
|
|
|
|
|
2014-01-21 07:25:35 +00:00
|
|
|
int mars_client_abort = 10;
|
|
|
|
EXPORT_SYMBOL_GPL(mars_client_abort);
|
|
|
|
|
2017-04-11 08:03:13 +00:00
|
|
|
int max_client_channels = 2;
|
2014-04-01 09:24:46 +00:00
|
|
|
EXPORT_SYMBOL_GPL(max_client_channels);
|
|
|
|
|
|
|
|
int max_client_bulk = 16;
|
|
|
|
EXPORT_SYMBOL_GPL(max_client_bulk);
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
///////////////////////// own helper functions ////////////////////////
|
|
|
|
|
2017-06-01 07:08:27 +00:00
|
|
|
static atomic_t sender_count = ATOMIC_INIT(0);
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
static int thread_count = 0;
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
static
|
|
|
|
void _do_resubmit(struct client_channel *ch)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
2014-04-01 09:24:46 +00:00
|
|
|
struct client_output *output = ch->output;
|
|
|
|
|
2018-02-01 05:25:02 +00:00
|
|
|
mutex_lock(&output->mutex);
|
2014-04-01 09:24:46 +00:00
|
|
|
if (!list_empty(&ch->wait_list)) {
|
|
|
|
struct list_head *first = ch->wait_list.next;
|
|
|
|
struct list_head *last = ch->wait_list.prev;
|
|
|
|
struct list_head *old_start = output->mref_list.next;
|
|
|
|
#define list_connect __list_del // the original routine has a misleading name: in reality it is more general
|
|
|
|
list_connect(&output->mref_list, first);
|
|
|
|
list_connect(last, old_start);
|
|
|
|
INIT_LIST_HEAD(&ch->wait_list);
|
|
|
|
}
|
2018-02-01 05:25:02 +00:00
|
|
|
mutex_unlock(&output->mutex);
|
2014-04-01 09:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void _kill_thread(struct client_threadinfo *ti, const char *name)
|
|
|
|
{
|
|
|
|
struct task_struct *thread = ti->thread;
|
|
|
|
if (thread) {
|
2012-12-21 11:35:43 +00:00
|
|
|
MARS_DBG("stopping %s thread\n", name);
|
|
|
|
ti->thread = NULL;
|
2014-04-01 09:24:46 +00:00
|
|
|
brick_thread_stop(thread);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
static
|
|
|
|
void _kill_channel(struct client_channel *ch)
|
2011-08-31 11:42:04 +00:00
|
|
|
{
|
2014-04-01 09:24:46 +00:00
|
|
|
MARS_DBG("channel = %p\n", ch);
|
|
|
|
if (mars_socket_is_alive(&ch->socket)) {
|
2011-08-31 11:42:04 +00:00
|
|
|
MARS_DBG("shutdown socket\n");
|
2014-04-01 09:24:46 +00:00
|
|
|
mars_shutdown_socket(&ch->socket);
|
|
|
|
}
|
|
|
|
_kill_thread(&ch->receiver, "receiver");
|
|
|
|
if (ch->is_open) {
|
|
|
|
MARS_DBG("close socket\n");
|
|
|
|
mars_put_socket(&ch->socket);
|
2011-08-31 11:42:04 +00:00
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
ch->recv_error = 0;
|
2015-10-14 06:58:04 +00:00
|
|
|
ch->is_used = false;
|
2014-04-01 09:24:46 +00:00
|
|
|
ch->is_open = false;
|
|
|
|
ch->is_connected = false;
|
|
|
|
/* Re-Submit any waiting requests
|
|
|
|
*/
|
|
|
|
_do_resubmit(ch);
|
2011-08-31 11:42:04 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
static inline
|
|
|
|
void _kill_all_channels(struct client_bundle *bundle)
|
2011-03-07 05:55:10 +00:00
|
|
|
{
|
2014-04-01 09:24:46 +00:00
|
|
|
int i;
|
|
|
|
// first pass: shutdown in parallel without waiting
|
|
|
|
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
|
|
|
|
struct client_channel *ch =&bundle->channel[i];
|
|
|
|
if (mars_socket_is_alive(&ch->socket)) {
|
|
|
|
MARS_DBG("shutdown socket %d\n", i);
|
|
|
|
mars_shutdown_socket(&ch->socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// separate pass (may wait)
|
|
|
|
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
|
|
|
|
_kill_channel(&bundle->channel[i]);
|
2011-03-07 05:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
static
|
|
|
|
int receiver_thread(void *data);
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
static
|
|
|
|
int _setup_channel(struct client_bundle *bundle, int ch_nr)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
2014-04-01 09:24:46 +00:00
|
|
|
struct client_channel *ch = &bundle->channel[ch_nr];
|
2011-02-23 20:48:06 +00:00
|
|
|
struct sockaddr_storage sockaddr = {};
|
|
|
|
int status;
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
ch->ch_nr = ch_nr;
|
|
|
|
if (unlikely(ch->receiver.thread)) {
|
|
|
|
MARS_WRN("receiver thread %d unexpectedly not dead\n", ch_nr);
|
|
|
|
_kill_thread(&ch->receiver, "receiver");
|
2012-12-21 11:35:43 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
status = mars_create_sockaddr(&sockaddr, bundle->host);
|
2011-03-04 15:14:20 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_DBG("no sockaddr, status = %d\n", status);
|
2011-02-23 20:48:06 +00:00
|
|
|
goto done;
|
2011-03-04 15:14:20 +00:00
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
|
2018-05-10 09:16:34 +00:00
|
|
|
if (!bundle->params) {
|
|
|
|
struct sockaddr_in *_sockaddr = (void*)&sockaddr;
|
|
|
|
|
|
|
|
int offset = (int)ntohs(_sockaddr->sin_port) - mars_net_default_port;
|
|
|
|
|
|
|
|
if (offset < 0 || offset >= MARS_TRAFFIC_MAX)
|
|
|
|
offset = 0;
|
|
|
|
bundle->params = &mars_tcp_params[offset];
|
|
|
|
}
|
|
|
|
|
2018-05-10 07:25:34 +00:00
|
|
|
status = mars_create_socket(&ch->socket,
|
|
|
|
&sockaddr,
|
2018-05-10 09:16:34 +00:00
|
|
|
bundle->params,
|
2018-05-10 07:25:34 +00:00
|
|
|
false);
|
2012-01-11 11:35:09 +00:00
|
|
|
if (unlikely(status < 0)) {
|
2011-03-04 15:14:20 +00:00
|
|
|
MARS_DBG("no socket, status = %d\n", status);
|
2012-01-11 11:35:09 +00:00
|
|
|
goto really_done;
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
ch->socket.s_shutdown_on_err = true;
|
|
|
|
ch->socket.s_send_abort = mars_client_abort;
|
|
|
|
ch->socket.s_recv_abort = mars_client_abort;
|
|
|
|
ch->is_open = true;
|
|
|
|
|
|
|
|
ch->receiver.thread = brick_thread_create(receiver_thread, ch, "mars_receiver%d.%d.%d", bundle->thread_count, ch_nr, ch->thread_count++);
|
|
|
|
if (unlikely(!ch->receiver.thread)) {
|
|
|
|
MARS_ERR("cannot start receiver thread %d, status = %d\n", ch_nr, status);
|
2012-11-13 16:01:37 +00:00
|
|
|
status = -ENOENT;
|
2011-08-31 11:42:04 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
ch->is_used = true;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (status < 0) {
|
|
|
|
MARS_INF("cannot connect channel %d to remote host '%s' (status = %d) -- retrying\n",
|
|
|
|
ch_nr,
|
|
|
|
bundle->host ? bundle->host : "NULL",
|
|
|
|
status);
|
|
|
|
_kill_channel(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
really_done:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void _kill_bundle(struct client_bundle *bundle)
|
|
|
|
{
|
|
|
|
MARS_DBG("\n");
|
|
|
|
_kill_thread(&bundle->sender, "sender");
|
|
|
|
_kill_all_channels(bundle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void _maintain_bundle(struct client_bundle *bundle)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Re-open _any_ failed channel, even old ones.
|
|
|
|
* Reason: the number of channels might change during operation.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
|
|
|
|
struct client_channel *ch = &bundle->channel[i];
|
|
|
|
|
|
|
|
if (!ch->is_used ||
|
|
|
|
(!ch->recv_error && mars_socket_is_alive(&ch->socket)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
MARS_DBG("killing channel %d\n", i);
|
|
|
|
_kill_channel(ch);
|
2015-10-14 06:58:04 +00:00
|
|
|
/* Re-setup including connect optiona is done later.
|
|
|
|
*/
|
2014-04-01 09:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
struct client_channel *_get_channel(struct client_bundle *bundle, int min_channel, int max_channel)
|
|
|
|
{
|
2015-06-15 05:48:54 +00:00
|
|
|
struct client_channel *res;
|
|
|
|
long best_space;
|
|
|
|
int best_channel;
|
2014-04-01 09:24:46 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (unlikely(max_channel <= 0 || max_channel > MAX_CLIENT_CHANNELS))
|
|
|
|
max_channel = MAX_CLIENT_CHANNELS;
|
2015-06-15 05:48:54 +00:00
|
|
|
if (unlikely(min_channel < 0 || min_channel >= max_channel)) {
|
2014-04-01 09:24:46 +00:00
|
|
|
min_channel = max_channel - 1;
|
2015-06-15 05:48:54 +00:00
|
|
|
if (unlikely(min_channel < 0))
|
|
|
|
min_channel = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fast path.
|
|
|
|
* Speculate that the next channel is already usable,
|
|
|
|
* and that it has enough room.
|
|
|
|
*/
|
|
|
|
best_channel = bundle->old_channel + 1;
|
|
|
|
if (best_channel >= max_channel)
|
|
|
|
best_channel = min_channel;
|
|
|
|
res = &bundle->channel[best_channel];
|
2015-10-14 08:45:53 +00:00
|
|
|
if (res->is_connected && !res->recv_error && mars_socket_is_alive(&res->socket)) {
|
2015-06-15 05:48:54 +00:00
|
|
|
res->current_space = mars_socket_send_space_available(&res->socket);
|
|
|
|
if (res->current_space > (PAGE_SIZE + PAGE_SIZE / 4))
|
|
|
|
goto found;
|
|
|
|
}
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2015-06-15 05:48:54 +00:00
|
|
|
/* Slow path. Do all the teady work.
|
|
|
|
*/
|
|
|
|
_maintain_bundle(bundle);
|
|
|
|
|
|
|
|
res = NULL;
|
|
|
|
best_space = -1;
|
|
|
|
best_channel = -1;
|
2014-04-01 09:24:46 +00:00
|
|
|
for (i = min_channel; i < max_channel; i++) {
|
|
|
|
struct client_channel *ch = &bundle->channel[i];
|
|
|
|
long this_space;
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
// create new channels when necessary
|
|
|
|
if (unlikely(!ch->is_open)) {
|
|
|
|
int status;
|
|
|
|
// only create one new channel at a time
|
|
|
|
status = _setup_channel(bundle, i);
|
|
|
|
MARS_DBG("setup channel %d status=%d\n", i, status);
|
|
|
|
if (unlikely(status < 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
this_space = mars_socket_send_space_available(&ch->socket);
|
|
|
|
ch->current_space = this_space;
|
|
|
|
/* Always prefer the newly opened channel */
|
|
|
|
res = ch;
|
2015-06-15 05:48:54 +00:00
|
|
|
best_channel = i;
|
2014-04-01 09:24:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// select the best usable channel
|
|
|
|
this_space = mars_socket_send_space_available(&ch->socket);
|
|
|
|
ch->current_space = this_space;
|
|
|
|
if (this_space > best_space) {
|
|
|
|
best_space = this_space;
|
2015-06-15 05:48:54 +00:00
|
|
|
best_channel = i;
|
2014-04-01 09:24:46 +00:00
|
|
|
res = ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 07:50:56 +00:00
|
|
|
if (unlikely(!res)) {
|
|
|
|
MARS_WRN("cannot setup communication channel '%s' @%s\n",
|
|
|
|
bundle->path,
|
|
|
|
bundle->host);
|
2015-06-15 05:48:54 +00:00
|
|
|
goto done;
|
2015-06-15 07:50:56 +00:00
|
|
|
}
|
2015-06-15 05:48:54 +00:00
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
// send initial connect command
|
2015-06-15 05:48:54 +00:00
|
|
|
if (unlikely(!res->is_connected)) {
|
2011-02-23 20:48:06 +00:00
|
|
|
struct mars_cmd cmd = {
|
|
|
|
.cmd_code = CMD_CONNECT,
|
2014-04-01 09:24:46 +00:00
|
|
|
.cmd_str1 = bundle->path,
|
2011-02-23 20:48:06 +00:00
|
|
|
};
|
2017-04-12 07:50:19 +00:00
|
|
|
int status = mars_send_struct(&res->socket, &cmd, mars_cmd_meta, false);
|
2014-04-01 09:24:46 +00:00
|
|
|
MARS_DBG("send CMD_CONNECT status = %d\n", status);
|
2011-03-04 15:14:20 +00:00
|
|
|
if (unlikely(status < 0)) {
|
2015-06-15 07:50:56 +00:00
|
|
|
MARS_WRN("connect '%s' @%s on channel %d failed, status = %d\n",
|
|
|
|
bundle->path,
|
|
|
|
bundle->host,
|
|
|
|
best_channel,
|
|
|
|
status);
|
2014-04-01 09:24:46 +00:00
|
|
|
_kill_channel(res);
|
|
|
|
res = NULL;
|
2011-02-23 20:48:06 +00:00
|
|
|
goto done;
|
2011-03-04 15:14:20 +00:00
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
res->is_connected = true;
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
|
2015-06-15 05:48:54 +00:00
|
|
|
found:
|
|
|
|
bundle->old_channel = best_channel;
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
done:
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int _request_info(struct client_channel *ch)
|
|
|
|
{
|
|
|
|
struct mars_cmd cmd = {
|
|
|
|
.cmd_code = CMD_GETINFO,
|
|
|
|
};
|
|
|
|
int status;
|
|
|
|
|
|
|
|
MARS_DBG("\n");
|
2017-04-12 07:50:19 +00:00
|
|
|
status = mars_send_struct(&ch->socket, &cmd, mars_cmd_meta, false);
|
2014-04-01 09:24:46 +00:00
|
|
|
MARS_DBG("send CMD_GETINFO status = %d\n", status);
|
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_DBG("send of getinfo failed, status = %d\n", status);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
return status;
|
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
static int sender_thread(void *data);
|
|
|
|
|
|
|
|
static
|
|
|
|
int _setup_bundle(struct client_bundle *bundle, const char *str)
|
|
|
|
{
|
|
|
|
int status = -ENOMEM;
|
|
|
|
|
|
|
|
MARS_DBG("\n");
|
|
|
|
_kill_bundle(bundle);
|
|
|
|
brick_string_free(bundle->path);
|
|
|
|
|
|
|
|
bundle->path = brick_strdup(str);
|
|
|
|
|
|
|
|
status = -EINVAL;
|
|
|
|
bundle->host = strchr(bundle->path, '@');
|
|
|
|
if (unlikely(!bundle->host)) {
|
|
|
|
brick_string_free(bundle->path);
|
|
|
|
bundle->path = NULL;
|
|
|
|
MARS_ERR("parameter string '%s' contains no remote specifier with '@'-syntax\n", str);
|
|
|
|
goto done;
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
*bundle->host++ = '\0';
|
|
|
|
|
|
|
|
bundle->thread_count = thread_count++;
|
|
|
|
bundle->sender.thread = brick_thread_create(sender_thread, bundle, "mars_sender%d", bundle->thread_count);
|
|
|
|
if (unlikely(!bundle->sender.thread)) {
|
2015-06-15 07:50:56 +00:00
|
|
|
MARS_ERR("cannot start sender thread for '%s' @%s\n",
|
|
|
|
bundle->path,
|
|
|
|
bundle->host);
|
2014-04-01 09:24:46 +00:00
|
|
|
status = -ENOENT;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = 0;
|
|
|
|
|
|
|
|
done:
|
|
|
|
MARS_DBG("status = %d\n", status);
|
2011-02-23 20:48:06 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-01-10 11:48:15 +00:00
|
|
|
static
|
|
|
|
long _compute_timeout(struct client_brick *brick)
|
|
|
|
{
|
|
|
|
long io_timeout = brick->power.io_timeout;
|
|
|
|
|
|
|
|
if (io_timeout <= 0)
|
|
|
|
io_timeout = global_net_io_timeout;
|
|
|
|
|
|
|
|
return io_timeout;
|
|
|
|
}
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
////////////////// own brick / input / output operations //////////////////
|
|
|
|
|
|
|
|
static int client_get_info(struct client_output *output, struct mars_info *info)
|
|
|
|
{
|
2017-01-10 11:48:15 +00:00
|
|
|
struct client_brick *brick = output->brick;
|
|
|
|
long io_timeout = _compute_timeout(brick);
|
2011-02-23 20:48:06 +00:00
|
|
|
int status;
|
|
|
|
|
2011-03-07 05:55:10 +00:00
|
|
|
output->got_info = false;
|
2017-01-10 11:52:43 +00:00
|
|
|
if (!brick->power.led_on)
|
|
|
|
goto timeout;
|
|
|
|
|
2011-03-07 05:55:10 +00:00
|
|
|
output->get_info = true;
|
2014-04-01 09:24:46 +00:00
|
|
|
wake_up_interruptible_all(&output->bundle.sender_event);
|
2011-03-07 05:55:10 +00:00
|
|
|
|
2017-01-10 11:48:15 +00:00
|
|
|
wait_event_interruptible_timeout(output->info_event, output->got_info, io_timeout * HZ);
|
2017-01-10 11:52:43 +00:00
|
|
|
timeout:
|
2015-01-15 17:21:29 +00:00
|
|
|
status = -ETIME;
|
2011-02-23 20:48:06 +00:00
|
|
|
if (output->got_info && info) {
|
|
|
|
memcpy(info, &output->info, sizeof(*info));
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int client_ref_get(struct client_output *output, struct mref_object *mref)
|
|
|
|
{
|
2012-12-10 09:31:28 +00:00
|
|
|
int maxlen;
|
|
|
|
|
|
|
|
if (mref->ref_initialized) {
|
|
|
|
_mref_get(mref);
|
|
|
|
return mref->ref_len;
|
|
|
|
}
|
|
|
|
|
2011-03-22 14:36:26 +00:00
|
|
|
#if 1
|
2011-02-23 20:48:06 +00:00
|
|
|
/* Limit transfers to page boundaries.
|
|
|
|
* Currently, this is more restrictive than necessary.
|
|
|
|
* TODO: improve performance by doing better when possible.
|
|
|
|
* This needs help from the server in some efficient way.
|
|
|
|
*/
|
2012-12-10 09:31:28 +00:00
|
|
|
maxlen = PAGE_SIZE - (mref->ref_pos & (PAGE_SIZE-1));
|
2011-02-23 20:48:06 +00:00
|
|
|
if (mref->ref_len > maxlen)
|
|
|
|
mref->ref_len = maxlen;
|
2011-03-22 14:36:26 +00:00
|
|
|
#endif
|
2011-02-23 20:48:06 +00:00
|
|
|
|
|
|
|
if (!mref->ref_data) { // buffered IO
|
2011-10-03 17:31:02 +00:00
|
|
|
struct client_mref_aspect *mref_a = client_mref_get_aspect(output->brick, mref);
|
2011-02-23 20:48:06 +00:00
|
|
|
if (!mref_a)
|
|
|
|
return -EILSEQ;
|
|
|
|
|
2011-11-14 17:52:05 +00:00
|
|
|
mref->ref_data = brick_block_alloc(mref->ref_pos, (mref_a->alloc_len = mref->ref_len));
|
2011-02-23 20:48:06 +00:00
|
|
|
if (!mref->ref_data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mref_a->do_dealloc = true;
|
|
|
|
mref->ref_flags = 0;
|
|
|
|
}
|
|
|
|
|
2012-12-10 09:31:28 +00:00
|
|
|
_mref_get_first(mref);
|
2011-02-23 20:48:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void client_ref_put(struct client_output *output, struct mref_object *mref)
|
|
|
|
{
|
|
|
|
struct client_mref_aspect *mref_a;
|
2012-12-10 09:31:28 +00:00
|
|
|
if (!_mref_put(mref))
|
2011-02-23 20:48:06 +00:00
|
|
|
return;
|
2011-10-03 17:31:02 +00:00
|
|
|
mref_a = client_mref_get_aspect(output->brick, mref);
|
2011-02-23 20:48:06 +00:00
|
|
|
if (mref_a && mref_a->do_dealloc) {
|
2011-11-14 17:52:05 +00:00
|
|
|
brick_block_free(mref->ref_data, mref_a->alloc_len);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
client_free_mref(mref);
|
|
|
|
}
|
|
|
|
|
2012-12-19 11:40:52 +00:00
|
|
|
static
|
|
|
|
void _hash_insert(struct client_output *output, struct client_mref_aspect *mref_a)
|
|
|
|
{
|
|
|
|
struct mref_object *mref = mref_a->object;
|
|
|
|
int hash_index;
|
|
|
|
|
2017-12-10 21:23:48 +00:00
|
|
|
mutex_lock(&output->mutex);
|
2012-12-19 11:40:52 +00:00
|
|
|
list_del(&mref_a->io_head);
|
|
|
|
list_add_tail(&mref_a->io_head, &output->mref_list);
|
|
|
|
list_del(&mref_a->hash_head);
|
|
|
|
mref->ref_id = ++output->last_id;
|
|
|
|
hash_index = mref->ref_id % CLIENT_HASH_MAX;
|
|
|
|
list_add_tail(&mref_a->hash_head, &output->hash_table[hash_index]);
|
2017-12-10 21:23:48 +00:00
|
|
|
mutex_unlock(&output->mutex);
|
2012-12-19 11:40:52 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
static void client_ref_io(struct client_output *output, struct mref_object *mref)
|
|
|
|
{
|
|
|
|
struct client_mref_aspect *mref_a;
|
|
|
|
int error = -EINVAL;
|
|
|
|
|
2011-10-03 17:31:02 +00:00
|
|
|
mref_a = client_mref_get_aspect(output->brick, mref);
|
2011-02-23 20:48:06 +00:00
|
|
|
if (unlikely(!mref_a)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-03-29 14:40:40 +00:00
|
|
|
while (output->brick->max_flying > 0 && atomic_read(&output->fly_count) > output->brick->max_flying) {
|
2011-03-30 12:02:50 +00:00
|
|
|
MARS_IO("sleeping request pos = %lld len = %d rw = %d (flying = %d)\n", mref->ref_pos, mref->ref_len, mref->ref_rw, atomic_read(&output->fly_count));
|
|
|
|
#ifdef IO_DEBUGGING
|
2012-09-17 10:11:25 +00:00
|
|
|
brick_msleep(3000);
|
2011-03-30 12:02:50 +00:00
|
|
|
#else
|
2012-09-17 10:11:25 +00:00
|
|
|
brick_msleep(1000 * 2 / HZ);
|
2011-03-30 12:02:50 +00:00
|
|
|
#endif
|
2011-03-29 14:40:40 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
if (!output->brick->power.led_on) {
|
|
|
|
MARS_ERR("IO submission on dead instance\n");
|
|
|
|
}
|
|
|
|
|
2012-11-16 11:43:10 +00:00
|
|
|
atomic_inc(&mars_global_io_flying);
|
2011-03-29 14:40:40 +00:00
|
|
|
atomic_inc(&output->fly_count);
|
2012-12-10 09:31:28 +00:00
|
|
|
_mref_get(mref);
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2012-01-23 12:39:08 +00:00
|
|
|
mref_a->submit_jiffies = jiffies;
|
2012-12-19 11:40:52 +00:00
|
|
|
_hash_insert(output, mref_a);
|
2011-03-30 12:02:50 +00:00
|
|
|
|
|
|
|
MARS_IO("added request id = %d pos = %lld len = %d rw = %d (flying = %d)\n", mref->ref_id, mref->ref_pos, mref->ref_len, mref->ref_rw, atomic_read(&output->fly_count));
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
wake_up_interruptible_all(&output->bundle.sender_event);
|
2011-02-23 20:48:06 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
MARS_ERR("IO error = %d\n", error);
|
2011-10-04 11:34:18 +00:00
|
|
|
SIMPLE_CALLBACK(mref, error);
|
2011-02-23 20:48:06 +00:00
|
|
|
client_ref_put(output, mref);
|
|
|
|
}
|
|
|
|
|
2011-05-26 14:32:32 +00:00
|
|
|
static
|
|
|
|
int receiver_thread(void *data)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
2014-04-01 09:24:46 +00:00
|
|
|
struct client_channel *ch = data;
|
|
|
|
struct client_output *output = ch->output;
|
2011-02-23 20:48:06 +00:00
|
|
|
int status = 0;
|
|
|
|
|
2012-12-21 11:35:43 +00:00
|
|
|
while (!brick_thread_should_stop()) {
|
2011-02-23 20:48:06 +00:00
|
|
|
struct mars_cmd cmd = {};
|
|
|
|
struct list_head *tmp;
|
|
|
|
struct client_mref_aspect *mref_a = NULL;
|
|
|
|
struct mref_object *mref = NULL;
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
if (ch->recv_error) {
|
2014-11-25 16:10:48 +00:00
|
|
|
/* The protocol may be out of sync.
|
|
|
|
* Consume some data to avoid distributed deadlocks.
|
|
|
|
*/
|
2014-04-01 09:24:46 +00:00
|
|
|
(void)mars_recv_raw(&ch->socket, &cmd, 0, sizeof(cmd));
|
2014-11-25 16:10:48 +00:00
|
|
|
brick_msleep(100);
|
2014-04-01 09:24:46 +00:00
|
|
|
status = ch->recv_error;
|
2014-11-25 16:10:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
status = mars_recv_struct(&ch->socket, &cmd, mars_cmd_meta);
|
2011-03-30 12:02:50 +00:00
|
|
|
MARS_IO("got cmd = %d status = %d\n", cmd.cmd_code, status);
|
2014-04-01 09:24:46 +00:00
|
|
|
if (status <= 0) {
|
|
|
|
if (!mars_socket_is_alive(&ch->socket)) {
|
|
|
|
MARS_DBG("socket is dead\n");
|
|
|
|
brick_msleep(1000);
|
|
|
|
continue;
|
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
goto done;
|
2014-04-01 09:24:46 +00:00
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2012-08-01 10:09:49 +00:00
|
|
|
switch (cmd.cmd_code & CMD_FLAG_MASK) {
|
2012-01-17 14:37:14 +00:00
|
|
|
case CMD_NOTIFY:
|
|
|
|
mars_trigger();
|
|
|
|
break;
|
2011-02-23 20:48:06 +00:00
|
|
|
case CMD_CONNECT:
|
|
|
|
if (cmd.cmd_int1 < 0) {
|
|
|
|
status = cmd.cmd_int1;
|
2015-06-15 07:50:56 +00:00
|
|
|
MARS_ERR("remote brick connect '%s' @%s failed, remote status = %d\n",
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host,
|
|
|
|
status);
|
2011-02-23 20:48:06 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CMD_CB:
|
2011-03-30 12:02:50 +00:00
|
|
|
{
|
2011-05-19 11:36:00 +00:00
|
|
|
int hash_index = cmd.cmd_int1 % CLIENT_HASH_MAX;
|
|
|
|
|
2017-12-10 21:23:48 +00:00
|
|
|
mutex_lock(&output->mutex);
|
2011-03-30 12:02:50 +00:00
|
|
|
for (tmp = output->hash_table[hash_index].next; tmp != &output->hash_table[hash_index]; tmp = tmp->next) {
|
2011-07-01 14:07:56 +00:00
|
|
|
struct mref_object *tmp_mref;
|
2011-03-30 12:02:50 +00:00
|
|
|
mref_a = container_of(tmp, struct client_mref_aspect, hash_head);
|
2011-07-01 14:07:56 +00:00
|
|
|
tmp_mref = mref_a->object;
|
2015-06-15 07:50:56 +00:00
|
|
|
CHECK_PTR(tmp_mref, err);
|
|
|
|
if (tmp_mref->ref_id != cmd.cmd_int1)
|
|
|
|
continue;
|
|
|
|
mref = tmp_mref;
|
|
|
|
list_del_init(&mref_a->hash_head);
|
|
|
|
list_del_init(&mref_a->io_head);
|
|
|
|
break;
|
|
|
|
|
|
|
|
err:
|
2018-02-01 05:25:02 +00:00
|
|
|
mutex_unlock(&output->mutex);
|
2015-06-15 07:50:56 +00:00
|
|
|
status = -EBADR;
|
|
|
|
goto done;
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
2017-12-10 21:23:48 +00:00
|
|
|
mutex_unlock(&output->mutex);
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2012-12-19 11:40:52 +00:00
|
|
|
if (unlikely(!mref)) {
|
2015-06-15 07:50:56 +00:00
|
|
|
MARS_WRN("got unknown callback id %d on '%s' @%s\n",
|
|
|
|
cmd.cmd_int1,
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host);
|
2014-04-01 09:24:46 +00:00
|
|
|
// try to consume the corresponding payload
|
|
|
|
mref = client_alloc_mref(output->brick);
|
|
|
|
status = mars_recv_cb(&ch->socket, mref, &cmd);
|
|
|
|
client_free_mref(mref);
|
2011-02-23 20:48:06 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-03-30 12:02:50 +00:00
|
|
|
MARS_IO("got callback id = %d, old pos = %lld len = %d rw = %d\n", mref->ref_id, mref->ref_pos, mref->ref_len, mref->ref_rw);
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
status = mars_recv_cb(&ch->socket, mref, &cmd);
|
2011-03-30 12:02:50 +00:00
|
|
|
MARS_IO("new status = %d, pos = %lld len = %d rw = %d\n", status, mref->ref_pos, mref->ref_len, mref->ref_rw);
|
2012-12-19 11:40:52 +00:00
|
|
|
if (unlikely(status < 0)) {
|
2015-06-15 07:50:56 +00:00
|
|
|
MARS_WRN("interrupted data transfer during callback on '%s' @%s, status = %d\n",
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host,
|
|
|
|
status);
|
2012-12-19 11:40:52 +00:00
|
|
|
_hash_insert(output, mref_a);
|
2011-02-23 20:48:06 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
if (mref->_object_cb.cb_error < 0) {
|
|
|
|
MARS_DBG("ERROR %d\n", mref->_object_cb.cb_error);
|
|
|
|
}
|
2013-11-30 21:34:40 +00:00
|
|
|
SIMPLE_CALLBACK(mref, mref->_object_cb.cb_error);
|
2012-11-16 11:43:10 +00:00
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
client_ref_put(output, mref);
|
2012-11-16 11:43:10 +00:00
|
|
|
|
|
|
|
atomic_dec(&output->fly_count);
|
|
|
|
atomic_dec(&mars_global_io_flying);
|
2011-02-23 20:48:06 +00:00
|
|
|
break;
|
2011-03-30 12:02:50 +00:00
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
case CMD_GETINFO:
|
2014-04-01 09:24:46 +00:00
|
|
|
status = mars_recv_struct(&ch->socket, &output->info, mars_info_meta);
|
2011-02-23 20:48:06 +00:00
|
|
|
if (status < 0) {
|
2015-06-15 07:50:56 +00:00
|
|
|
MARS_WRN("got bad info from remote '%s' @%s, status = %d\n",
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host,
|
|
|
|
status);
|
2011-02-23 20:48:06 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
output->got_info = true;
|
2014-04-01 09:24:46 +00:00
|
|
|
wake_up_interruptible_all(&output->info_event);
|
2011-02-23 20:48:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-06-15 07:50:56 +00:00
|
|
|
MARS_ERR("got bad command %d from remote '%s' @%s, terminating.\n",
|
|
|
|
cmd.cmd_code,
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host);
|
2011-02-23 20:48:06 +00:00
|
|
|
status = -EBADR;
|
|
|
|
goto done;
|
|
|
|
}
|
2011-08-12 12:25:10 +00:00
|
|
|
done:
|
|
|
|
brick_string_free(cmd.cmd_str1);
|
2012-12-21 11:35:43 +00:00
|
|
|
if (unlikely(status < 0)) {
|
2014-04-01 09:24:46 +00:00
|
|
|
if (!ch->recv_error) {
|
|
|
|
MARS_DBG("signalling recv_error = %d\n", status);
|
|
|
|
ch->recv_error = status;
|
2012-12-21 11:35:43 +00:00
|
|
|
}
|
|
|
|
brick_msleep(100);
|
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
// wake up sender in any case
|
|
|
|
wake_up_interruptible_all(&output->bundle.sender_event);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 07:50:56 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_WRN("receiver thread '%s' @%s terminated with status = %d\n",
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host,
|
|
|
|
status);
|
2011-03-03 18:23:34 +00:00
|
|
|
}
|
2011-08-31 11:42:04 +00:00
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
mars_shutdown_socket(&ch->socket);
|
2011-02-23 20:48:06 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-01-23 12:39:08 +00:00
|
|
|
static
|
2015-06-15 07:50:56 +00:00
|
|
|
void _do_timeout(struct client_output *output, struct list_head *anchor, int *rounds, bool force)
|
2012-01-23 12:39:08 +00:00
|
|
|
{
|
|
|
|
struct client_brick *brick = output->brick;
|
2012-12-19 11:40:52 +00:00
|
|
|
struct list_head *tmp;
|
2019-03-13 10:29:09 +00:00
|
|
|
struct list_head *prev;
|
2012-12-19 11:40:52 +00:00
|
|
|
LIST_HEAD(tmp_list);
|
2017-01-10 11:48:15 +00:00
|
|
|
long io_timeout = _compute_timeout(brick);
|
2014-04-01 09:24:46 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (list_empty(anchor))
|
|
|
|
return;
|
2012-12-19 11:40:52 +00:00
|
|
|
|
|
|
|
if (!mars_net_is_alive)
|
|
|
|
force = true;
|
|
|
|
|
2017-01-07 10:52:26 +00:00
|
|
|
if (!force && io_timeout <= 0) {
|
2014-04-01 09:24:46 +00:00
|
|
|
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
|
|
|
|
struct client_channel *ch = &output->bundle.channel[i];
|
|
|
|
|
|
|
|
ch->socket.s_send_abort = mars_client_abort;
|
|
|
|
ch->socket.s_recv_abort = mars_client_abort;
|
|
|
|
}
|
2012-12-19 11:40:52 +00:00
|
|
|
return;
|
2017-01-07 10:52:26 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
|
|
|
|
struct client_channel *ch = &output->bundle.channel[i];
|
|
|
|
|
|
|
|
ch->socket.s_send_abort = 1;
|
|
|
|
ch->socket.s_recv_abort = 1;
|
|
|
|
}
|
2017-01-07 10:52:26 +00:00
|
|
|
|
2012-12-19 11:40:52 +00:00
|
|
|
io_timeout *= HZ;
|
|
|
|
|
2017-12-10 21:23:48 +00:00
|
|
|
mutex_lock(&output->mutex);
|
2019-03-13 10:29:09 +00:00
|
|
|
for (tmp = anchor->prev, prev = tmp->prev; tmp != anchor; tmp = prev, prev = tmp->prev) {
|
2012-01-23 12:39:08 +00:00
|
|
|
struct client_mref_aspect *mref_a;
|
|
|
|
|
|
|
|
mref_a = container_of(tmp, struct client_mref_aspect, io_head);
|
2012-12-19 11:40:52 +00:00
|
|
|
|
2012-08-21 11:43:56 +00:00
|
|
|
if (!force &&
|
2012-12-19 11:40:52 +00:00
|
|
|
!time_is_before_jiffies(mref_a->submit_jiffies + io_timeout)) {
|
2019-03-13 10:29:09 +00:00
|
|
|
break;
|
2012-01-23 12:39:08 +00:00
|
|
|
}
|
2012-12-19 11:40:52 +00:00
|
|
|
|
|
|
|
list_del_init(&mref_a->hash_head);
|
|
|
|
list_del_init(&mref_a->io_head);
|
|
|
|
list_add_tail(&mref_a->tmp_head, &tmp_list);
|
|
|
|
}
|
2017-12-10 21:23:48 +00:00
|
|
|
mutex_unlock(&output->mutex);
|
2012-12-19 11:40:52 +00:00
|
|
|
|
|
|
|
while (!list_empty(&tmp_list)) {
|
|
|
|
struct client_mref_aspect *mref_a;
|
|
|
|
struct mref_object *mref;
|
|
|
|
|
|
|
|
tmp = tmp_list.next;
|
|
|
|
list_del_init(tmp);
|
|
|
|
mref_a = container_of(tmp, struct client_mref_aspect, tmp_head);
|
|
|
|
mref = mref_a->object;
|
2012-01-23 12:39:08 +00:00
|
|
|
|
2015-06-15 07:50:56 +00:00
|
|
|
if (unlikely(!(*rounds)++)) {
|
|
|
|
MARS_WRN("'%s' @%s timeout after %ld: signalling IO error at pos = %lld len = %d\n",
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host,
|
2012-08-21 11:43:56 +00:00
|
|
|
io_timeout,
|
|
|
|
mref->ref_pos,
|
|
|
|
mref->ref_len);
|
|
|
|
}
|
2012-12-19 11:40:52 +00:00
|
|
|
|
2012-01-23 12:39:08 +00:00
|
|
|
atomic_inc(&output->timeout_count);
|
|
|
|
|
2017-01-18 08:18:10 +00:00
|
|
|
SIMPLE_CALLBACK(mref, -ETIME);
|
2012-11-16 11:43:10 +00:00
|
|
|
|
2012-01-23 12:39:08 +00:00
|
|
|
client_ref_put(output, mref);
|
2012-11-16 11:43:10 +00:00
|
|
|
|
2012-01-23 12:39:08 +00:00
|
|
|
atomic_dec(&output->fly_count);
|
2012-11-16 11:43:10 +00:00
|
|
|
atomic_dec(&mars_global_io_flying);
|
2012-01-23 12:39:08 +00:00
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void _do_timeout_all(struct client_output *output, bool force)
|
|
|
|
{
|
2015-06-15 07:50:56 +00:00
|
|
|
int rounds = 0;
|
2014-04-01 09:24:46 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
|
|
|
|
struct client_channel *ch = &output->bundle.channel[i];
|
|
|
|
|
|
|
|
if (!ch->is_used)
|
|
|
|
continue;
|
2015-06-15 07:50:56 +00:00
|
|
|
_do_timeout(output, &ch->wait_list, &rounds, force);
|
|
|
|
}
|
|
|
|
_do_timeout(output, &output->mref_list, &rounds, force);
|
|
|
|
if (unlikely(rounds > 0)) {
|
|
|
|
MARS_WRN("'%s' @%s had %d timeouts, force = %d\n",
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host,
|
|
|
|
rounds,
|
|
|
|
force);
|
2014-04-01 09:24:46 +00:00
|
|
|
}
|
2012-01-23 12:39:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
static int sender_thread(void *data)
|
|
|
|
{
|
2014-04-01 09:24:46 +00:00
|
|
|
struct client_bundle *bundle = data;
|
|
|
|
struct client_output *output = container_of(bundle, struct client_output, bundle);
|
2011-02-23 20:48:06 +00:00
|
|
|
struct client_brick *brick = output->brick;
|
2014-04-01 09:24:46 +00:00
|
|
|
struct client_channel *ch = NULL;
|
|
|
|
bool do_timeout = false;
|
2017-04-12 10:06:04 +00:00
|
|
|
bool cork = false;
|
2018-10-15 10:27:50 +00:00
|
|
|
bool old_cork = false;
|
2014-04-01 09:24:46 +00:00
|
|
|
int ch_skip = max_client_bulk;
|
2015-10-14 08:45:53 +00:00
|
|
|
int status = -ESHUTDOWN;
|
2011-05-26 14:32:32 +00:00
|
|
|
|
2017-06-01 07:08:27 +00:00
|
|
|
if (atomic_inc_return(&sender_count) == 1)
|
|
|
|
mars_limit_reset(&client_limiter);
|
|
|
|
|
2017-02-24 06:10:37 +00:00
|
|
|
while (brick->power.button && !brick_thread_should_stop()) {
|
2012-01-23 12:39:08 +00:00
|
|
|
struct list_head *tmp = NULL;
|
2011-02-23 20:48:06 +00:00
|
|
|
struct client_mref_aspect *mref_a;
|
2011-03-30 12:02:50 +00:00
|
|
|
struct mref_object *mref;
|
2014-04-01 09:24:46 +00:00
|
|
|
int min_nr;
|
|
|
|
int max_nr;
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
// timeouting is a rather expensive operation, don't do it too often
|
|
|
|
if (do_timeout) {
|
|
|
|
do_timeout = false;
|
2015-10-14 06:58:04 +00:00
|
|
|
_maintain_bundle(&output->bundle);
|
2014-04-01 09:24:46 +00:00
|
|
|
_do_timeout_all(output, false);
|
2014-09-27 07:32:20 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
wait_event_interruptible_timeout(output->bundle.sender_event,
|
2012-12-21 11:35:43 +00:00
|
|
|
!list_empty(&output->mref_list) ||
|
2014-04-01 09:24:46 +00:00
|
|
|
output->get_info,
|
|
|
|
2 * HZ);
|
2012-12-21 11:35:43 +00:00
|
|
|
|
2017-01-07 23:45:44 +00:00
|
|
|
|
2011-03-07 05:55:10 +00:00
|
|
|
if (output->get_info) {
|
2017-04-12 10:06:04 +00:00
|
|
|
if (ch && old_cork) {
|
|
|
|
/* flush old buffer */
|
|
|
|
old_cork = false;
|
|
|
|
mars_send_raw(&ch->socket, NULL, 0, false);
|
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
ch = _get_channel(bundle, 0, 1);
|
|
|
|
if (unlikely(!ch)) {
|
|
|
|
do_timeout = true;
|
|
|
|
brick_msleep(1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
status = _request_info(ch);
|
2015-06-15 07:50:56 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_WRN("cannot send info request '%s' @%s, status = %d\n",
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host,
|
|
|
|
status);
|
2014-04-01 09:24:46 +00:00
|
|
|
do_timeout = true;
|
2012-09-17 10:11:25 +00:00
|
|
|
brick_msleep(1000);
|
2015-06-15 07:50:56 +00:00
|
|
|
continue;
|
2011-03-07 05:55:10 +00:00
|
|
|
}
|
2015-06-15 07:50:56 +00:00
|
|
|
output->get_info = false;
|
2011-03-07 05:55:10 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 11:40:52 +00:00
|
|
|
/* Grab the next mref from the queue
|
|
|
|
*/
|
2017-12-10 21:23:48 +00:00
|
|
|
mutex_lock(&output->mutex);
|
2014-04-01 09:24:46 +00:00
|
|
|
tmp = output->mref_list.next;
|
|
|
|
if (tmp == &output->mref_list) {
|
2017-12-10 21:23:48 +00:00
|
|
|
mutex_unlock(&output->mutex);
|
2014-04-01 09:24:46 +00:00
|
|
|
MARS_DBG("empty %d %d\n", output->get_info, brick_thread_should_stop());
|
|
|
|
do_timeout = true;
|
2012-12-19 11:40:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
list_del_init(tmp);
|
|
|
|
// notice: hash_head remains in its list!
|
2017-04-12 07:56:40 +00:00
|
|
|
cork = !list_empty(&output->mref_list);
|
2017-12-10 21:23:48 +00:00
|
|
|
mutex_unlock(&output->mutex);
|
2012-12-19 11:40:52 +00:00
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
mref_a = container_of(tmp, struct client_mref_aspect, io_head);
|
2011-03-30 12:02:50 +00:00
|
|
|
mref = mref_a->object;
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2012-08-07 12:07:12 +00:00
|
|
|
if (brick->limit_mode) {
|
|
|
|
int amount = 0;
|
|
|
|
if (mref->ref_cs_mode < 2)
|
|
|
|
amount = (mref->ref_len - 1) / 1024 + 1;
|
|
|
|
mars_limit_sleep(&client_limiter, amount);
|
|
|
|
}
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
// try to spread reads over multiple channels....
|
|
|
|
min_nr = 0;
|
|
|
|
max_nr = max_client_channels;
|
|
|
|
if (!mref->ref_rw) {
|
|
|
|
/* optionally separate reads from writes */
|
|
|
|
if (brick->separate_reads && max_nr > 1)
|
|
|
|
min_nr = 1;
|
|
|
|
} else if (!brick->allow_permuting_writes) {
|
|
|
|
max_nr = 1;
|
|
|
|
}
|
|
|
|
if (!ch || ch->recv_error ||
|
2016-01-27 06:09:02 +00:00
|
|
|
!mars_socket_is_alive(&ch->socket))
|
|
|
|
do_timeout = true;
|
|
|
|
if (do_timeout || ch->ch_nr >= max_nr || --ch_skip < 0) {
|
2017-04-12 10:06:04 +00:00
|
|
|
if (ch && old_cork) {
|
|
|
|
/* flush old buffer */
|
|
|
|
old_cork = false;
|
|
|
|
mars_send_raw(&ch->socket, NULL, 0, false);
|
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
ch = _get_channel(bundle, min_nr, max_nr);
|
2015-06-15 07:50:56 +00:00
|
|
|
if (unlikely(!ch)) {
|
2014-04-01 09:24:46 +00:00
|
|
|
// notice: this will re-assign hash_head without harm
|
|
|
|
_hash_insert(output, mref_a);
|
|
|
|
brick_msleep(1000);
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-15 07:50:56 +00:00
|
|
|
/* estimate: add some headroom for overhead */
|
|
|
|
ch_skip = ch->current_space / PAGE_SIZE +
|
|
|
|
ch->current_space / (PAGE_SIZE * 8);
|
|
|
|
if (ch_skip > max_client_bulk)
|
|
|
|
ch_skip = max_client_bulk;
|
2014-04-01 09:24:46 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 05:25:02 +00:00
|
|
|
mutex_lock(&output->mutex);
|
2019-03-13 10:29:09 +00:00
|
|
|
list_add_tail(tmp, &ch->wait_list);
|
2014-04-01 09:24:46 +00:00
|
|
|
// notice: hash_head is already there!
|
2018-02-01 05:25:02 +00:00
|
|
|
mutex_unlock(&output->mutex);
|
2011-03-30 12:02:50 +00:00
|
|
|
|
2017-04-12 07:56:40 +00:00
|
|
|
status = mars_send_mref(&ch->socket, mref, cork);
|
2017-04-12 10:06:04 +00:00
|
|
|
old_cork = cork;
|
2011-02-23 20:48:06 +00:00
|
|
|
if (unlikely(status < 0)) {
|
2014-04-01 09:24:46 +00:00
|
|
|
_hash_insert(output, mref_a);
|
|
|
|
do_timeout = true;
|
|
|
|
ch = NULL;
|
2011-02-23 20:48:06 +00:00
|
|
|
// retry submission on next occasion..
|
2015-06-15 07:50:56 +00:00
|
|
|
MARS_WRN("mref send '%s' @%s failed, status = %d\n",
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host,
|
|
|
|
status);
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
brick_msleep(100);
|
2011-02-23 20:48:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
|
2015-06-15 07:50:56 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_WRN("sender thread '%s' @%s terminated with status = %d\n",
|
|
|
|
output->bundle.path,
|
|
|
|
output->bundle.host,
|
|
|
|
status);
|
2011-08-31 11:42:04 +00:00
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
_kill_all_channels(bundle);
|
2011-02-23 20:48:06 +00:00
|
|
|
|
2011-11-04 16:23:52 +00:00
|
|
|
/* Signal error on all pending IO requests.
|
|
|
|
* We have no other chance (except probably delaying
|
2012-01-23 12:39:08 +00:00
|
|
|
* this until destruction which is probably not what
|
2011-11-04 16:23:52 +00:00
|
|
|
* we want).
|
|
|
|
*/
|
2014-04-01 09:24:46 +00:00
|
|
|
_do_timeout_all(output, true);
|
2011-11-04 16:23:52 +00:00
|
|
|
|
2017-06-01 07:08:27 +00:00
|
|
|
if (!atomic_dec_return(&sender_count))
|
|
|
|
mars_limit_reset(&client_limiter);
|
|
|
|
|
2014-04-01 09:24:46 +00:00
|
|
|
wake_up_interruptible_all(&output->bundle.sender_event);
|
2011-11-04 16:23:52 +00:00
|
|
|
MARS_DBG("sender terminated\n");
|
2011-02-23 20:48:06 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int client_switch(struct client_brick *brick)
|
|
|
|
{
|
|
|
|
struct client_output *output = brick->outputs[0];
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
if (brick->power.button) {
|
2013-09-25 15:07:19 +00:00
|
|
|
if (brick->power.led_on)
|
|
|
|
goto done;
|
2011-02-23 20:48:06 +00:00
|
|
|
mars_power_led_off((void*)brick, false);
|
2014-04-01 09:24:46 +00:00
|
|
|
status = _setup_bundle(&output->bundle, brick->brick_name);
|
|
|
|
if (likely(status >= 0)) {
|
|
|
|
output->get_info = true;
|
2013-04-11 12:58:36 +00:00
|
|
|
brick->connection_state = 1;
|
2011-03-03 18:23:34 +00:00
|
|
|
mars_power_led_on((void*)brick, true);
|
|
|
|
}
|
2011-02-23 20:48:06 +00:00
|
|
|
} else {
|
2013-09-25 15:07:19 +00:00
|
|
|
if (brick->power.led_off)
|
|
|
|
goto done;
|
2011-02-23 20:48:06 +00:00
|
|
|
mars_power_led_on((void*)brick, false);
|
2014-04-01 09:24:46 +00:00
|
|
|
_kill_bundle(&output->bundle);
|
|
|
|
_do_timeout_all(output, true);
|
|
|
|
output->got_info = false;
|
2013-04-11 12:58:36 +00:00
|
|
|
brick->connection_state = 0;
|
2014-04-01 09:24:46 +00:00
|
|
|
mars_power_led_off((void*)brick, !output->bundle.sender.thread);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
done:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-04 15:43:23 +00:00
|
|
|
//////////////// informational / statistics ///////////////
|
|
|
|
|
|
|
|
static
|
|
|
|
char *client_statistics(struct client_brick *brick, int verbose)
|
|
|
|
{
|
|
|
|
struct client_output *output = brick->outputs[0];
|
2012-01-23 12:39:08 +00:00
|
|
|
char *res = brick_string_alloc(1024);
|
2015-10-14 06:02:39 +00:00
|
|
|
int socket_count = 0;
|
|
|
|
int i;
|
|
|
|
|
2011-11-04 15:43:23 +00:00
|
|
|
if (!res)
|
|
|
|
return NULL;
|
|
|
|
|
2015-10-14 06:02:39 +00:00
|
|
|
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
|
|
|
|
struct client_channel *ch = &output->bundle.channel[i];
|
|
|
|
if (mars_socket_is_alive(&ch->socket))
|
|
|
|
socket_count++;
|
|
|
|
}
|
2012-01-23 12:39:08 +00:00
|
|
|
snprintf(res, 1024,
|
2015-10-14 06:02:39 +00:00
|
|
|
"socket_count = %d "
|
2012-02-12 11:19:57 +00:00
|
|
|
"max_flying = %d "
|
|
|
|
"io_timeout = %d | "
|
|
|
|
"timeout_count = %d "
|
|
|
|
"fly_count = %d\n",
|
2015-10-14 06:02:39 +00:00
|
|
|
socket_count,
|
2012-02-12 11:19:57 +00:00
|
|
|
brick->max_flying,
|
2014-09-27 07:28:24 +00:00
|
|
|
brick->power.io_timeout,
|
2012-02-12 11:19:57 +00:00
|
|
|
atomic_read(&output->timeout_count),
|
|
|
|
atomic_read(&output->fly_count));
|
2012-01-23 12:39:08 +00:00
|
|
|
|
2011-11-04 15:43:23 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void client_reset_statistics(struct client_brick *brick)
|
|
|
|
{
|
2012-01-23 12:39:08 +00:00
|
|
|
struct client_output *output = brick->outputs[0];
|
|
|
|
atomic_set(&output->timeout_count, 0);
|
2011-11-04 15:43:23 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 20:48:06 +00:00
|
|
|
//////////////// object / aspect constructors / destructors ///////////////
|
|
|
|
|
2011-10-03 17:31:02 +00:00
|
|
|
static int client_mref_aspect_init_fn(struct generic_aspect *_ini)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
|
|
|
struct client_mref_aspect *ini = (void*)_ini;
|
|
|
|
INIT_LIST_HEAD(&ini->io_head);
|
2011-03-30 12:02:50 +00:00
|
|
|
INIT_LIST_HEAD(&ini->hash_head);
|
2012-12-19 11:40:52 +00:00
|
|
|
INIT_LIST_HEAD(&ini->tmp_head);
|
2011-02-23 20:48:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-03 17:31:02 +00:00
|
|
|
static void client_mref_aspect_exit_fn(struct generic_aspect *_ini)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
|
|
|
struct client_mref_aspect *ini = (void*)_ini;
|
2011-03-30 12:02:50 +00:00
|
|
|
CHECK_HEAD_EMPTY(&ini->io_head);
|
|
|
|
CHECK_HEAD_EMPTY(&ini->hash_head);
|
2011-02-23 20:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MARS_MAKE_STATICS(client);
|
|
|
|
|
|
|
|
////////////////////// brick constructors / destructors ////////////////////
|
|
|
|
|
|
|
|
static int client_brick_construct(struct client_brick *brick)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int client_output_construct(struct client_output *output)
|
|
|
|
{
|
2011-03-30 12:02:50 +00:00
|
|
|
int i;
|
2013-04-12 12:13:21 +00:00
|
|
|
|
|
|
|
output->hash_table = brick_block_alloc(0, PAGE_SIZE);
|
|
|
|
if (unlikely(!output->hash_table)) {
|
|
|
|
MARS_ERR("cannot allocate hash table\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2011-03-30 12:02:50 +00:00
|
|
|
for (i = 0; i < CLIENT_HASH_MAX; i++) {
|
|
|
|
INIT_LIST_HEAD(&output->hash_table[i]);
|
|
|
|
}
|
2014-04-01 09:24:46 +00:00
|
|
|
|
|
|
|
for (i = 0; i < MAX_CLIENT_CHANNELS; i++) {
|
|
|
|
struct client_channel *ch = &output->bundle.channel[i];
|
|
|
|
ch->output = output;
|
|
|
|
INIT_LIST_HEAD(&ch->wait_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
init_waitqueue_head(&output->bundle.sender_event);
|
2017-12-10 21:23:48 +00:00
|
|
|
mutex_init(&output->mutex);
|
2011-02-23 20:48:06 +00:00
|
|
|
INIT_LIST_HEAD(&output->mref_list);
|
|
|
|
init_waitqueue_head(&output->info_event);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int client_output_destruct(struct client_output *output)
|
|
|
|
{
|
2014-04-01 09:24:46 +00:00
|
|
|
brick_string_free(output->bundle.path);
|
|
|
|
output->bundle.path = NULL;
|
2013-04-12 12:13:21 +00:00
|
|
|
brick_block_free(output->hash_table, PAGE_SIZE);
|
2011-02-23 20:48:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////// static structs ////////////////////////
|
|
|
|
|
|
|
|
static struct client_brick_ops client_brick_ops = {
|
|
|
|
.brick_switch = client_switch,
|
2011-11-04 15:43:23 +00:00
|
|
|
.brick_statistics = client_statistics,
|
|
|
|
.reset_statistics = client_reset_statistics,
|
2011-02-23 20:48:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct client_output_ops client_output_ops = {
|
|
|
|
.mars_get_info = client_get_info,
|
|
|
|
.mref_get = client_ref_get,
|
|
|
|
.mref_put = client_ref_put,
|
|
|
|
.mref_io = client_ref_io,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct client_input_type client_input_type = {
|
|
|
|
.type_name = "client_input",
|
|
|
|
.input_size = sizeof(struct client_input),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct client_input_type *client_input_types[] = {
|
|
|
|
&client_input_type,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct client_output_type client_output_type = {
|
|
|
|
.type_name = "client_output",
|
|
|
|
.output_size = sizeof(struct client_output),
|
|
|
|
.master_ops = &client_output_ops,
|
|
|
|
.output_construct = &client_output_construct,
|
|
|
|
.output_destruct = &client_output_destruct,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct client_output_type *client_output_types[] = {
|
|
|
|
&client_output_type,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct client_brick_type client_brick_type = {
|
|
|
|
.type_name = "client_brick",
|
|
|
|
.brick_size = sizeof(struct client_brick),
|
|
|
|
.max_inputs = 0,
|
|
|
|
.max_outputs = 1,
|
|
|
|
.master_ops = &client_brick_ops,
|
2011-10-03 17:31:02 +00:00
|
|
|
.aspect_types = client_aspect_types,
|
2011-02-23 20:48:06 +00:00
|
|
|
.default_input_types = client_input_types,
|
|
|
|
.default_output_types = client_output_types,
|
|
|
|
.brick_construct = &client_brick_construct,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(client_brick_type);
|
|
|
|
|
|
|
|
////////////////// module init stuff /////////////////////////
|
|
|
|
|
2012-08-07 12:07:12 +00:00
|
|
|
struct mars_limiter client_limiter = {
|
|
|
|
.lim_max_rate = 0,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(client_limiter);
|
|
|
|
|
2012-08-14 14:12:59 +00:00
|
|
|
int global_net_io_timeout = CONFIG_MARS_NETIO_TIMEOUT;
|
|
|
|
EXPORT_SYMBOL_GPL(global_net_io_timeout);
|
|
|
|
|
2011-08-25 10:16:32 +00:00
|
|
|
int __init init_mars_client(void)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
|
|
|
MARS_INF("init_client()\n");
|
2011-02-28 18:00:32 +00:00
|
|
|
_client_brick_type = (void*)&client_brick_type;
|
2011-02-23 20:48:06 +00:00
|
|
|
return client_register_brick_type();
|
|
|
|
}
|
|
|
|
|
2014-04-23 11:16:26 +00:00
|
|
|
void exit_mars_client(void)
|
2011-02-23 20:48:06 +00:00
|
|
|
{
|
|
|
|
MARS_INF("exit_client()\n");
|
|
|
|
client_unregister_brick_type();
|
|
|
|
}
|