+struct netio_options {
+ struct thread_data *td;
+ unsigned int port;
+ unsigned int proto;
+ unsigned int listen;
+ unsigned int pingpong;
+ unsigned int nodelay;
+ unsigned int ttl;
+ unsigned int window_size;
+ unsigned int mss;
+ char *intfc;
+};
+
+struct udp_close_msg {
+ uint32_t magic;
+ uint32_t cmd;
+};
+
+struct udp_seq {
+ uint64_t magic;
+ uint64_t seq;
+ uint64_t bs;
+};
+
+enum {
+ FIO_LINK_CLOSE = 0x89,
+ FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
+ FIO_LINK_OPEN = 0x98,
+ FIO_UDP_SEQ_MAGIC = 0x657375716e556563ULL,
+
+ FIO_TYPE_TCP = 1,
+ FIO_TYPE_UDP = 2,
+ FIO_TYPE_UNIX = 3,
+ FIO_TYPE_TCP_V6 = 4,
+ FIO_TYPE_UDP_V6 = 5,
+};
+
+static int str_hostname_cb(void *data, const char *input);
+static struct fio_option options[] = {
+ {
+ .name = "hostname",
+ .lname = "net engine hostname",
+ .type = FIO_OPT_STR_STORE,
+ .cb = str_hostname_cb,
+ .help = "Hostname for net IO engine",
+ .category = FIO_OPT_C_ENGINE,
+ .group = FIO_OPT_G_NETIO,
+ },
+ {
+ .name = "port",
+ .lname = "net engine port",
+ .type = FIO_OPT_INT,
+ .off1 = offsetof(struct netio_options, port),
+ .minval = 1,
+ .maxval = 65535,
+ .help = "Port to use for TCP or UDP net connections",
+ .category = FIO_OPT_C_ENGINE,
+ .group = FIO_OPT_G_NETIO,
+ },
+ {
+ .name = "protocol",
+ .lname = "net engine protocol",
+ .alias = "proto",
+ .type = FIO_OPT_STR,
+ .off1 = offsetof(struct netio_options, proto),
+ .help = "Network protocol to use",
+ .def = "tcp",
+ .posval = {
+ { .ival = "tcp",
+ .oval = FIO_TYPE_TCP,
+ .help = "Transmission Control Protocol",
+ },
+#ifdef CONFIG_IPV6
+ { .ival = "tcpv6",
+ .oval = FIO_TYPE_TCP_V6,
+ .help = "Transmission Control Protocol V6",
+ },
+#endif
+ { .ival = "udp",
+ .oval = FIO_TYPE_UDP,
+ .help = "User Datagram Protocol",
+ },
+#ifdef CONFIG_IPV6
+ { .ival = "udpv6",
+ .oval = FIO_TYPE_UDP_V6,
+ .help = "User Datagram Protocol V6",
+ },
+#endif
+ { .ival = "unix",
+ .oval = FIO_TYPE_UNIX,
+ .help = "UNIX domain socket",
+ },
+ },
+ .category = FIO_OPT_C_ENGINE,
+ .group = FIO_OPT_G_NETIO,
+ },
+#ifdef CONFIG_TCP_NODELAY
+ {
+ .name = "nodelay",
+ .type = FIO_OPT_BOOL,
+ .off1 = offsetof(struct netio_options, nodelay),
+ .help = "Use TCP_NODELAY on TCP connections",
+ .category = FIO_OPT_C_ENGINE,
+ .group = FIO_OPT_G_NETIO,
+ },
+#endif
+ {
+ .name = "listen",
+ .lname = "net engine listen",
+ .type = FIO_OPT_STR_SET,
+ .off1 = offsetof(struct netio_options, listen),
+ .help = "Listen for incoming TCP connections",
+ .category = FIO_OPT_C_ENGINE,
+ .group = FIO_OPT_G_NETIO,
+ },
+ {
+ .name = "pingpong",
+ .type = FIO_OPT_STR_SET,
+ .off1 = offsetof(struct netio_options, pingpong),
+ .help = "Ping-pong IO requests",
+ .category = FIO_OPT_C_ENGINE,
+ .group = FIO_OPT_G_NETIO,
+ },
+ {
+ .name = "interface",
+ .lname = "net engine interface",
+ .type = FIO_OPT_STR_STORE,
+ .off1 = offsetof(struct netio_options, intfc),
+ .help = "Network interface to use",
+ .category = FIO_OPT_C_ENGINE,
+ .group = FIO_OPT_G_NETIO,
+ },
+ {
+ .name = "ttl",
+ .lname = "net engine multicast ttl",
+ .type = FIO_OPT_INT,
+ .off1 = offsetof(struct netio_options, ttl),
+ .def = "1",
+ .minval = 0,
+ .help = "Time-to-live value for outgoing UDP multicast packets",
+ .category = FIO_OPT_C_ENGINE,
+ .group = FIO_OPT_G_NETIO,
+ },
+#ifdef CONFIG_NET_WINDOWSIZE
+ {
+ .name = "window_size",
+ .lname = "Window Size",
+ .type = FIO_OPT_INT,
+ .off1 = offsetof(struct netio_options, window_size),
+ .minval = 0,
+ .help = "Set socket buffer window size",
+ .category = FIO_OPT_C_ENGINE,
+ .group = FIO_OPT_G_NETIO,
+ },
+#endif
+#ifdef CONFIG_NET_MSS
+ {
+ .name = "mss",
+ .lname = "Maximum segment size",
+ .type = FIO_OPT_INT,
+ .off1 = offsetof(struct netio_options, mss),
+ .minval = 0,
+ .help = "Set TCP maximum segment size",
+ .category = FIO_OPT_C_ENGINE,
+ .group = FIO_OPT_G_NETIO,
+ },
+#endif
+ {
+ .name = NULL,
+ },
+};
+
+static inline int is_udp(struct netio_options *o)
+{
+ return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
+}
+
+static inline int is_tcp(struct netio_options *o)
+{
+ return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
+}
+
+static inline int is_ipv6(struct netio_options *o)
+{
+ return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
+}
+
+static int set_window_size(struct thread_data *td, int fd)
+{
+#ifdef CONFIG_NET_WINDOWSIZE
+ struct netio_options *o = td->eo;
+ unsigned int wss;
+ int snd, rcv, ret;
+
+ if (!o->window_size)
+ return 0;
+
+ rcv = o->listen || o->pingpong;
+ snd = !o->listen || o->pingpong;
+ wss = o->window_size;
+ ret = 0;
+
+ if (rcv) {
+ ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss,
+ sizeof(wss));
+ if (ret < 0)
+ td_verror(td, errno, "rcvbuf window size");
+ }
+ if (snd && !ret) {
+ ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss,
+ sizeof(wss));
+ if (ret < 0)
+ td_verror(td, errno, "sndbuf window size");
+ }
+
+ return ret;
+#else
+ td_verror(td, -EINVAL, "setsockopt window size");
+ return -1;
+#endif
+}
+
+static int set_mss(struct thread_data *td, int fd)
+{
+#ifdef CONFIG_NET_MSS
+ struct netio_options *o = td->eo;
+ unsigned int mss;
+ int ret;
+
+ if (!o->mss || !is_tcp(o))
+ return 0;
+
+ mss = o->mss;
+ ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss,
+ sizeof(mss));
+ if (ret < 0)
+ td_verror(td, errno, "setsockopt TCP_MAXSEG");
+
+ return ret;
+#else
+ td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG");
+ return -1;
+#endif
+}
+
+
+/*
+ * Return -1 for error and 'nr events' for a positive number
+ * of events
+ */
+static int poll_wait(struct thread_data *td, int fd, short events)
+{
+ struct pollfd pfd;
+ int ret;
+
+ while (!td->terminate) {
+ pfd.fd = fd;
+ pfd.events = events;
+ ret = poll(&pfd, 1, -1);
+ if (ret < 0) {
+ if (errno == EINTR)
+ break;
+
+ td_verror(td, errno, "poll");
+ return -1;
+ } else if (!ret)
+ continue;
+
+ break;
+ }
+
+ if (pfd.revents & events)
+ return 1;
+
+ return -1;
+}
+
+static int fio_netio_is_multicast(const char *mcaddr)
+{
+ in_addr_t addr = inet_network(mcaddr);
+ if (addr == -1)
+ return 0;
+
+ if (inet_network("224.0.0.0") <= addr &&
+ inet_network("239.255.255.255") >= addr)
+ return 1;
+
+ return 0;
+}
+
+