/*
 * Copyright (C) 2013-2021 Canonical, Ltd.
 * Copyright (C) 2022-2026 Colin Ian King.
 *
 * 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.
 *
 */
#include "stress-ng.h"
#include "core-affinity.h"
#include "core-builtin.h"
#include "core-killpid.h"
#include "core-net.h"
#include "core-signal.h"

#if defined(HAVE_SYS_UN_H)
#include <sys/un.h>
#else
UNEXPECTED
#endif

#if defined(HAVE_NETINET_SCTP_H)
#include <netinet/sctp.h>
#else
UNEXPECTED
#endif

#if defined(HAVE_NETINET_TCP_H)
#include <netinet/tcp.h>
#else
UNEXPECTED
#endif

#define MIN_SCTP_MAX_SIZE	(16)
#define MAX_SCTP_MAX_SIZE	(65536)
#define DEFAULT_SCTP_MAX_SIZE	(8192)

typedef struct {
	const int	sched_type;
	const char 	*name;
} stress_sctp_sched_t;

static const stress_help_t help[] = {
	{ NULL,	"sctp N",	   "start N workers performing SCTP send/receives " },
	{ NULL,	"sctp-domain D",   "specify sctp domain, default is ipv4" },
	{ NULL,	"sctp-if I",	   "use network interface I, e.g. lo, eth0, etc." },
	{ NULL, "sctp-max-size N", "specify maximum size of sctp data" },
	{ NULL,	"sctp-ops N",	   "stop after N SCTP bogo operations" },
	{ NULL,	"sctp-port P",	   "use SCTP ports P to P + number of workers - 1" },
	{ NULL, "sctp-sched S",	   "specify sctp scheduler" },
	{ NULL,	NULL,              NULL }
};

#if defined(HAVE_LIB_SCTP) &&	\
    defined(HAVE_NETINET_SCTP_H)

#if !defined(LOCALTIME_STREAM)
#define LOCALTIME_STREAM        0
#endif

static uint64_t	sigpipe_count;
#else
UNEXPECTED
#endif

/* See include/uapi/linux/sctp.h */
enum shim_sctp_sched_type {
	SHIM_SCTP_SS_FCFS,
	SHIM_SCTP_SS_PRIO,
	SHIM_SCTP_SS_RR,
	SHIM_SCTP_SS_FC,
	SHIM_SCTP_SS_WFQ,
};

static const stress_sctp_sched_t stress_sctp_scheds[] = {
	{ (int)SHIM_SCTP_SS_FC,		"fc" },
	{ (int)SHIM_SCTP_SS_FCFS,	"fcfs" },
	{ (int)SHIM_SCTP_SS_PRIO,	"prio" },
	{ (int)SHIM_SCTP_SS_RR,		"rr" },
	{ (int)SHIM_SCTP_SS_WFQ,	"wfq" },
};

static const char *stress_sctp_sched(const size_t i)
{
	return (i < SIZEOF_ARRAY(stress_sctp_scheds)) ? stress_sctp_scheds[i].name : NULL;
}

static int sctp_domain_mask = DOMAIN_INET | DOMAIN_INET6;

static const stress_opt_t opts[] = {
	{ OPT_sctp_domain,   "sctp-domain",   TYPE_ID_INT_DOMAIN, 0, 0, &sctp_domain_mask },
	{ OPT_sctp_if,       "sctp-if",       TYPE_ID_STR, 0, 0, NULL },
	{ OPT_sctp_max_size, "sctp-max-size", TYPE_ID_SIZE_T, MIN_SCTP_MAX_SIZE, MAX_SCTP_MAX_SIZE, NULL },
	{ OPT_sctp_port,     "sctp-port",     TYPE_ID_INT_PORT, MIN_PORT, MAX_PORT, NULL },
	{ OPT_sctp_sched,    "sctp-sched",    TYPE_ID_SIZE_T_METHOD, 0, 0, stress_sctp_sched },
	END_OPT,
};

#if defined(HAVE_LIB_SCTP) &&	\
    defined(HAVE_NETINET_SCTP_H)

#define STRESS_SCTP_SOCKOPT(opt, type)			\
{							\
	type info;					\
	socklen_t opt_len = sizeof(info);		\
	int ret;					\
							\
	ret = getsockopt(fd, IPPROTO_SCTP, opt,		\
		 &info, &opt_len);			\
	if (LIKELY(ret == 0)) {				\
		VOID_RET(int, setsockopt(fd,		\
			IPPROTO_SCTP, opt, &info,	\
			opt_len));			\
	}						\
}

/*
 *  stress_sctp_sockopts()
 *	exercise some SCTP specific sockopts
 */
static void stress_sctp_sockopts(const int fd)
{
#if defined(SCTP_RTOINFO) && 	\
    defined(HAVE_SCTP_RTOINFO)
	STRESS_SCTP_SOCKOPT(SCTP_RTOINFO, struct sctp_rtoinfo)
#else
	UNEXPECTED
#endif
#if defined(SCTP_ASSOCINFO) &&	\
    defined(HAVE_SCTP_ASSOCPARAMS)
	STRESS_SCTP_SOCKOPT(SCTP_ASSOCINFO, struct sctp_assocparams)
#else
	UNEXPECTED
#endif
#if defined(SCTP_INITMSG) &&	\
    defined(HAVE_SCTP_INITMSG)
	STRESS_SCTP_SOCKOPT(SCTP_INITMSG, struct sctp_initmsg)
#else
	UNEXPECTED
#endif
#if defined(SCTP_NODELAY)
	STRESS_SCTP_SOCKOPT(SCTP_NODELAY, int)
#else
	UNEXPECTED
#endif
#if defined(SCTP_PRIMARY_ADDR) &&	\
    defined(HAVE_SCTP_PRIM)
	STRESS_SCTP_SOCKOPT(SCTP_PRIMARY_ADDR, struct sctp_prim)
#else
	UNEXPECTED
#endif
#if defined(SCTP_PEER_ADDR_PARAMS) &&	\
    defined(HAVE_SCTP_PADDRPARAMS)
	STRESS_SCTP_SOCKOPT(SCTP_PEER_ADDR_PARAMS, struct sctp_paddrparams)
#else
	UNEXPECTED
#endif
#if defined(SCTP_EVENTS) &&	\
    defined(HAVE_SCTP_EVENT_SUBSCRIBE)
	STRESS_SCTP_SOCKOPT(SCTP_EVENTS, struct sctp_event_subscribe)
#else
	UNEXPECTED
#endif
#if defined(SCTP_MAXSEG)
	{
		/*
		 *  try using the wrong type, kernel will
		 *  complain, do this once
		 */
		static bool once = false;

		if (once == false) {
			STRESS_SCTP_SOCKOPT(SCTP_MAXSEG, int64_t)
			once = true;
		}
	}
#if defined(HAVE_SCTP_ASSOC_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_MAXSEG, struct sctp_assoc_value)
#else
	UNEXPECTED
#endif
#else
	UNEXPECTED
#endif
#if defined(SCTP_STATUS) && 	\
    defined(HAVE_SCTP_STATUS)
	STRESS_SCTP_SOCKOPT(SCTP_STATUS, struct sctp_status)
#else
	UNEXPECTED
#endif
#if defined(SCTP_GET_PEER_ADDR_INFO) &&	\
    defined(HAVE_SCTP_PADDRINFO)
	STRESS_SCTP_SOCKOPT(SCTP_GET_PEER_ADDR_INFO, struct sctp_paddrinfo)
#else
	UNEXPECTED
#endif
#if defined(SCTP_GET_ASSOC_STATS) &&	\
    defined(HAVE_SCTP_ASSOC_STATS)
	STRESS_SCTP_SOCKOPT(SCTP_GET_ASSOC_STATS, struct sctp_assoc_stats)
#else
	UNEXPECTED
#endif
#if defined(SCTP_MAX_BURST)
	{
		/*
		 *  try using the wrong type, kernel will
		 *  complain, do this once
		 */
		static bool once = false;

		if (once == false) {
			STRESS_SCTP_SOCKOPT(SCTP_MAX_BURST, uint64_t)
			once = true;
		}
	}
#if defined(HAVE_SCTP_ASSOC_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_MAX_BURST, struct sctp_assoc_value)
#else
	UNEXPECTED
#endif
#endif
#if defined(SCTP_AUTOCLOSE)
	STRESS_SCTP_SOCKOPT(SCTP_AUTOCLOSE, unsigned int)
#else
	UNEXPECTED
#endif
#if defined(SCTP_GET_PEER_ADDRS) &&	\
    defined(HAVE_SCTP_GETADDRS)
	STRESS_SCTP_SOCKOPT(SCTP_GET_PEER_ADDRS, struct sctp_getaddrs)
#else
	UNEXPECTED
#endif
#if defined(SCTP_GET_LOCAL_ADDRS) &&	\
    defined(HAVE_SCTP_GETADDRS)
	STRESS_SCTP_SOCKOPT(SCTP_GET_LOCAL_ADDRS, struct sctp_getaddrs)
#else
	UNEXPECTED
#endif
#if defined(SCTP_ADAPTATION_LAYER) &&	\
    defined(HAVE_SCTP_SETADAPTION)
	STRESS_SCTP_SOCKOPT(SCTP_ADAPTATION_LAYER, struct sctp_setadaptation)
#else
	UNEXPECTED
#endif
#if defined(SCTP_CONTEXT) &&	\
    defined(HAVE_SCTP_ASSOC_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_CONTEXT, struct sctp_assoc_value)
#else
	UNEXPECTED
#endif
#if defined(SCTP_FRAGMENT_INTERLEAVE)
	STRESS_SCTP_SOCKOPT(SCTP_FRAGMENT_INTERLEAVE, int)
#else
	UNEXPECTED
#endif
#if defined(SCTP_PARTIAL_DELIVERY_POINT)
	STRESS_SCTP_SOCKOPT(SCTP_PARTIAL_DELIVERY_POINT, uint32_t)
#else
	UNEXPECTED
#endif
#if defined(SCTP_AUTO_ASCONF)
	STRESS_SCTP_SOCKOPT(SCTP_AUTO_ASCONF, int)
#else
	UNEXPECTED
#endif
#if defined(SCTP_DEFAULT_PRINFO) &&	\
    defined(HAVE_SCTP_DEFAULT_PRINFO)
	STRESS_SCTP_SOCKOPT(SCTP_DEFAULT_PRINFO, struct sctp_default_prinfo)
#else
	UNEXPECTED
#endif
#if defined(SCTP_STREAM_SCHEDULER) &&	\
    defined(HAVE_SCTP_ASSOC_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_STREAM_SCHEDULER, struct sctp_assoc_value)
#else
	UNEXPECTED
#endif
#if defined(SCTP_STREAM_SCHEDULER_VALUE) && \
    defined(HAVE_SCTP_STREAM_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_STREAM_SCHEDULER_VALUE, struct sctp_stream_value)
#else
	UNEXPECTED
#endif
#if defined(SCTP_INTERLEAVING_SUPPORTED) &&	\
    defined(HAVE_SCTP_ASSOC_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_INTERLEAVING_SUPPORTED, struct sctp_assoc_value)
#else
	UNEXPECTED
#endif
#if defined(SCTP_REUSE_PORT) &&	\
    defined(HAVE_SCTP_ASSOC_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_REUSE_PORT, struct sctp_assoc_value)
#else
	UNEXPECTED
#endif
#if defined(SCTP_EVENT) &&	\
    defined(HAVE_SCTP_EVENT)
	{
		typedef int32_t	shim_sctp_assoc_t;

		struct shim_sctp_event {
			shim_sctp_assoc_t se_assoc_id;
			uint16_t	se_type;
			uint8_t		se_on;
		};

		STRESS_SCTP_SOCKOPT(SCTP_EVENT, struct shim_sctp_event)
	}
#endif
#if defined(SCTP_ASCONF_SUPPORTED) &&	\
    defined(HAVE_SCTP_ASSOC_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_ASCONF_SUPPORTED, struct sctp_assoc_value)
#else
	UNEXPECTED
#endif
#if defined(SCTP_AUTH_SUPPORTED) &&	\
    defined(HAVE_SCTP_ASSOC_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_AUTH_SUPPORTED, struct sctp_assoc_value)
#else
	UNEXPECTED
#endif
#if defined(SCTP_ECN_SUPPORTED) &&	\
    defined(HAVE_SCTP_ASSOC_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_ECN_SUPPORTED, struct sctp_assoc_value)
#else
	UNEXPECTED
#endif
#if defined(SCTP_EXPOSE_POTENTIALLY_FAILED_STATE) && 	\
    defined(HAVE_SCTP_ASSOC_VALUE)
	STRESS_SCTP_SOCKOPT(SCTP_EXPOSE_POTENTIALLY_FAILED_STATE, struct sctp_assoc_value)
#endif
#if defined(SCTP_REMOTE_UDP_ENCAPS_PORT) && 	\
    defined(HAVE_SCTP_UDPENCAPS)
	STRESS_SCTP_SOCKOPT(SCTP_REMOTE_UDP_ENCAPS_PORT, struct sctp_udpencaps)
#else
	UNEXPECTED
#endif
#if defined(SCTP_PLPMTUD_PROBE_INTERVAL) &&	\
    defined(HAVE_SCTP_PROBEINTERVAL)
	STRESS_SCTP_SOCKOPT(SCTP_PLPMTUD_PROBE_INTERVAL, struct sctp_probeinterval)
#else
	UNEXPECTED
#endif
}

/*
 *  stress_sctp_client()
 *	client reader
 */
static int OPTIMIZE3 stress_sctp_client(
	stress_args_t *args,
	const pid_t mypid,
	const int sctp_port,
	const int sctp_domain,
	const int sctp_sched_type,
	const char *sctp_if)
{
	struct sockaddr *addr;
	int rc = EXIT_SUCCESS;

	(void)sctp_sched_type;

	stress_parent_died_alarm();
	(void)stress_sched_settings_apply(true);

	do {
		char ALIGN64 buf[MAX_SCTP_MAX_SIZE];
		int fd;
		int retries = 0;
		socklen_t addr_len = 0;
#if defined(HAVE_SCTP_EVENT_SUBSCRIBE)
		struct sctp_event_subscribe events;
#endif
retry:
		if (UNLIKELY(!stress_continue_flag()))
			return EXIT_FAILURE;
		fd = socket(sctp_domain, SOCK_STREAM, IPPROTO_SCTP);
		if (UNLIKELY(fd < 0)) {
			if (errno == EPROTONOSUPPORT) {
				if (stress_instance_zero(args))
					pr_inf_skip("%s: SCTP protocol not supported, skipping stressor\n",
						args->name);
				return EXIT_NO_RESOURCE;
			}
			pr_fail("%s: socket failed, errno=%d (%s)\n",
				args->name, errno, strerror(errno));
			return EXIT_FAILURE;
		}

		if (UNLIKELY(stress_net_sockaddr_if_set(args->name, args->instance, mypid,
							sctp_domain, sctp_port, sctp_if,
							&addr, &addr_len, NET_ADDR_LOOPBACK) < 0)) {
			(void)close(fd);
			return EXIT_FAILURE;
		}
		if (UNLIKELY(connect(fd, addr, addr_len) < 0)) {
			const int save_errno = errno;

			(void)close(fd);
			(void)shim_usleep(10000);
			retries++;
			if (UNLIKELY(retries > 100)) {
				/* Give up.. */
				pr_fail("%s: connect failed after 100 retries, errno=%d (%s)\n",
					args->name, save_errno, strerror(save_errno));
				return EXIT_FAILURE;
			}
			goto retry;
		}
#if defined(HAVE_SCTP_EVENT_SUBSCRIBE)
		(void)shim_memset(&events, 0, sizeof(events));
		events.sctp_data_io_event = 1;
		if (UNLIKELY(setsockopt(fd, SOL_SCTP, SCTP_EVENTS, &events, sizeof(events)) < 0)) {
			(void)close(fd);
			pr_fail("%s: setsockopt failed, errno=%d (%s)\n",
				args->name, errno, strerror(errno));
			return EXIT_FAILURE;
		}
#endif
#if defined(HAVE_SCTP_SCHED_TYPE) &&	\
    defined(HAVE_SCTP_ASSOC_VALUE)
		if (sctp_sched_type > -1) {
			struct sctp_assoc_value val;

			(void)shim_memset(&val, 0, sizeof(val));
			val.assoc_value = (uint32_t)sctp_sched_type;
			(void)setsockopt(fd, SOL_SCTP, SCTP_STREAM_SCHEDULER, &val, sizeof(val));
		}
#endif

		do {
			int flags = 0;
			struct sctp_sndrcvinfo sndrcvinfo;
			ssize_t n;

			n = sctp_recvmsg(fd, buf, sizeof(buf),
				NULL, NULL, &sndrcvinfo, &flags);
			if (UNLIKELY(n <= 0))
				break;
			if (n >= (ssize_t)sizeof(pid_t)) {
				pid_t pid;

				shim_memcpy(&pid, buf, sizeof(pid));
				if (UNLIKELY(pid != mypid)) {
					pr_fail("%s: client received unexpected data "
						"contents, got 0x%" PRIxMAX ", "
						"expected 0x%" PRIxMAX "\n",
						args->name, (uintmax_t)pid,
						(uintmax_t)mypid);
					rc = EXIT_FAILURE;
					break;
				}
			}
		} while (stress_continue_flag());
		(void)shutdown(fd, SHUT_RDWR);
		(void)close(fd);
	} while (stress_continue(args));

#if defined(AF_UNIX) &&		\
    defined(HAVE_SYS_UN_H) &&	\
    defined(HAVE_SOCKADDR_UN)
	if (sctp_domain == AF_UNIX) {
		const struct sockaddr_un *addr_un = (struct sockaddr_un *)addr;

		(void)shim_unlink(addr_un->sun_path);
	}
#else
	UNEXPECTED
#endif
	return rc;
}

/*
 *  stress_sctp_server()
 *	server writer
 */
static int OPTIMIZE3 stress_sctp_server(
	stress_args_t *args,
	const pid_t mypid,
	const int sctp_port,
	const int sctp_domain,
	const int sctp_sched_type,
	const char *sctp_if,
	const size_t sctp_max_size)
{
	char ALIGN64 buf[MAX_SCTP_MAX_SIZE];
	struct sockaddr *addr = NULL;
	socklen_t addr_len = 0;
	const size_t sctp_min_size = (sctp_max_size & 0xf) + 16;
	int fd;
	int so_reuseaddr = 1;
	int rc = EXIT_SUCCESS;
	int idx = 0;

	(void)sctp_sched_type;

	if (stress_signal_stop_stressing(args->name, SIGALRM)) {
		rc = EXIT_FAILURE;
		goto die;
	}
	fd = socket(sctp_domain, SOCK_STREAM, IPPROTO_SCTP);
	if (fd < 0) {
		if (errno == EPROTONOSUPPORT) {
			if (stress_instance_zero(args))
				pr_inf_skip("%s: SCTP protocol not supported, skipping stressor\n",
					args->name);
			rc = EXIT_NO_RESOURCE;
			goto die;
		}
		rc = stress_exit_status(errno);
		pr_fail("%s: socket failed, errno=%d (%s)\n",
			args->name, errno, strerror(errno));
		goto die;
	}
	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
		&so_reuseaddr, sizeof(so_reuseaddr)) < 0) {
		pr_fail("%s: setsockopt failed, errno=%d (%s)\n",
			args->name, errno, strerror(errno));
		rc = EXIT_FAILURE;
		goto die_close;
	}
	if (stress_net_sockaddr_if_set(args->name, args->instance, mypid,
				       sctp_domain, sctp_port, sctp_if,
				       &addr, &addr_len, NET_ADDR_ANY) < 0) {
		rc = EXIT_FAILURE;
		goto die_close;
	}
	if (bind(fd, addr, addr_len) < 0) {
		rc = stress_exit_status(errno);
		pr_fail("%s: bind failed, errno=%d (%s)\n",
			args->name, errno, strerror(errno));
		goto die_close;
	}
	if (listen(fd, 10) < 0) {
		pr_fail("%s: listen failed, errno=%d (%s)\n",
			args->name, errno, strerror(errno));
		rc = EXIT_FAILURE;
		goto die_close;
	}
#if defined(SCTP_NODELAY)
	{
		int one = 1;

		if (g_opt_flags & OPT_FLAGS_SOCKET_NODELAY) {
			if (setsockopt(fd, SOL_TCP, SCTP_NODELAY, &one, sizeof(one)) < 0) {
				pr_inf("%s: setsockopt SCTP_NODELAY "
					"failed and disabled, errno=%d (%s)\n",
					args->name, errno, strerror(errno));
				g_opt_flags &= ~OPT_FLAGS_SOCKET_NODELAY;
			}
		}
	}
#else
	UNEXPECTED
#endif

#if defined(HAVE_SCTP_SCHED_TYPE) &&	\
    defined(HAVE_SCTP_ASSOC_VALUE)
	if (sctp_sched_type > -1) {
		struct sctp_assoc_value val;

		(void)shim_memset(&val, 0, sizeof(val));
		val.assoc_value = (uint32_t)sctp_sched_type;
		(void)setsockopt(fd, SOL_SCTP, SCTP_STREAM_SCHEDULER, &val, sizeof(val));
	}
#endif
	do {
		int sfd;

		if (UNLIKELY(!stress_continue(args)))
			break;

		sfd = accept(fd, (struct sockaddr *)NULL, NULL);
		if (LIKELY(sfd >= 0)) {
			size_t i;
			const int c = stress_ascii32[idx++ & 0x1f];

			(void)shim_memset(buf + sizeof(mypid), c, sizeof(buf) - sizeof(mypid));
			(void)shim_memcpy(buf, &mypid, sizeof(mypid));
			for (i = sctp_min_size; i <= sctp_max_size; i += 16) {
				ssize_t ret = sctp_sendmsg(sfd, buf, i,
						NULL, 0, 0, 0,
						LOCALTIME_STREAM, 0, 0);
				if (UNLIKELY(ret < 0))
					break;
				stress_bogo_inc(args);
			}
			stress_sctp_sockopts(sfd);
			(void)close(sfd);
		}
	} while (stress_continue(args));

die_close:
	(void)close(fd);
die:
#if defined(AF_UNIX) &&		\
    defined(HAVE_SYS_UN_H) &&	\
    defined(HAVE_SOCKADDR_UN)
	if (addr && (sctp_domain == AF_UNIX)) {
		const struct sockaddr_un *addr_un = (struct sockaddr_un *)addr;

		(void)shim_unlink(addr_un->sun_path);
	}
#else
	UNEXPECTED
#endif
	return rc;
}

static void stress_sctp_sigpipe(int signum)
{
	(void)signum;

	sigpipe_count++;
}

/*
 *  stress_sctp
 *	stress SCTP by heavy SCTP network I/O
 */
static int stress_sctp(stress_args_t *args)
{
	char *sctp_if = NULL;
	size_t sctp_sched = 1; /* default to fcfs */
	size_t sctp_max_size = DEFAULT_SCTP_MAX_SIZE;
	pid_t pid;
	const pid_t mypid = getpid();
	int sctp_port = DEFAULT_SCTP_PORT;
	int sctp_domain = AF_INET;
	int sctp_sched_type = -1; /* undefined */
	int ret;
	int reserved_port;
	int parent_cpu;

	if (stress_signal_sigchld_handler(args) < 0)
		return EXIT_NO_RESOURCE;

	(void)stress_setting_get("sctp-domain", &sctp_domain);
	(void)stress_setting_get("sctp-if", &sctp_if);
	if (!stress_setting_get("sctp-max-size", &sctp_max_size)) {
		if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
			sctp_max_size = MAX_SCTP_MAX_SIZE;
		if (g_opt_flags & OPT_FLAGS_MINIMIZE)
			sctp_max_size = MIN_SCTP_MAX_SIZE;
	}
	(void)stress_setting_get("sctp-port", &sctp_port);
	if (stress_setting_get("sctp-sched", &sctp_sched)) {
#if defined(HAVE_SCTP_SCHED_TYPE) &&	\
    defined(HAVE_SCTP_ASSOC_VALUE)
		sctp_sched_type = stress_sctp_scheds[sctp_sched].sched_type;
#else
		pr_inf("sctp-sched option ignored, no SCTP scheduler types available\n");
#endif
	}

	if (sctp_if) {
		struct sockaddr if_addr;

		ret = stress_net_interface_exists(sctp_if, sctp_domain, &if_addr);
		if (ret < 0) {
			pr_inf("%s: interface '%s' is not enabled for domain '%s', defaulting to using loopback\n",
				args->name, sctp_if, stress_net_domain(sctp_domain));
			sctp_if = NULL;
		}
	}

	if (stress_signal_handler(args->name, SIGPIPE, stress_sctp_sigpipe, NULL) < 0)
		return EXIT_FAILURE;

	sctp_port += args->instance;
	sctp_port = stress_net_port_wraparound(sctp_port);
	reserved_port = stress_net_reserve_ports(args, sctp_port, sctp_port);
	if (reserved_port < 0) {
		pr_inf_skip("%s: cannot reserve port %d, skipping stressor\n",
			args->name, sctp_port);
		return EXIT_NO_RESOURCE;
	}
	sctp_port = reserved_port;

	pr_dbg("%s: process [%" PRIdMAX "] using socket port %d\n",
		args->name, (intmax_t)args->pid, sctp_port);

	ret = EXIT_FAILURE;

	stress_proc_state_set(args->name, STRESS_STATE_SYNC_WAIT);
	stress_sync_start_wait(args);
	stress_proc_state_set(args->name, STRESS_STATE_RUN);

	parent_cpu = stress_cpu_get();
	pid = stress_retry_fork(args, 0);
	if (pid < 0) {
		if (UNLIKELY(!stress_continue(args)))
			goto finish;
		pr_fail("%s: fork failed, errno=%d (%s)\n",
			args->name, errno, strerror(errno));
		return EXIT_FAILURE;
	} else if (pid == 0) {
		stress_proc_state_set(args->name, STRESS_STATE_RUN);
		stress_make_it_fail_set();
		 (void)stress_affinity_change_cpu(args, parent_cpu);
		ret = stress_sctp_client(args, mypid, sctp_port, sctp_domain,
					 sctp_sched_type, sctp_if);
		_exit(ret);
	} else {
		int status;

		ret = stress_sctp_server(args, mypid, sctp_port, sctp_domain,
					 sctp_sched_type, sctp_if, sctp_max_size);
		(void)stress_kill_pid_wait(pid, &status);
		if (WIFEXITED(status)) {
			if (WEXITSTATUS(status) != EXIT_SUCCESS) {
				ret = WEXITSTATUS(status);
			}
		}
	}

finish:
	if (sigpipe_count)
		pr_dbg("%s: caught %" PRIu64 " SIGPIPE signals\n", args->name, sigpipe_count);

	stress_proc_state_set(args->name, STRESS_STATE_DEINIT);
	stress_net_release_ports(sctp_port, sctp_port);

	return ret;
}

static const stress_exercises_t exercises[] = {
	STRESS_EX_SYSCALL("bind"),
	STRESS_EX_SYSCALL("close"),
	STRESS_EX_SYSCALL("connect"),
	STRESS_EX_SYSCALL("getsockopt"),
	STRESS_EX_SYSCALL("listen"),
	STRESS_EX_SYSCALL("recvmsg"),
	STRESS_EX_SYSCALL("sendmsg"),
	STRESS_EX_SYSCALL("setsockopt"),
	STRESS_EX_SYSCALL("shutdown"),
	STRESS_EX_SYSCALL("socket"),

	STRESS_EX_LIBRARY("sctp"),
	STRESS_EX_END,
};

const stressor_info_t stress_sctp_info = {
	.stressor = stress_sctp,
	.classifier = CLASS_NETWORK,
	.opts = opts,
	.verify = VERIFY_ALWAYS,
	.help = help,
	.exercises = exercises,
};
#else
const stressor_info_t stress_sctp_info = {
	.stressor = stress_unimplemented,
	.classifier = CLASS_NETWORK,
	.opts = opts,
	.verify = VERIFY_ALWAYS,
	.help = help,
	.unimplemented_reason = "built without netinet/sctp.h or libsctp support"
};
#endif
