2009-11-04 15:02:32 -08:00
|
|
|
|
/*
|
2015-02-20 08:44:48 -08:00
|
|
|
|
* Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014, 2015 Nicira, Inc.
|
2009-11-04 15:02:32 -08:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include "stream.h"
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <netinet/in.h>
|
2014-02-06 16:04:05 -08:00
|
|
|
|
#include <netdb.h>
|
2009-11-04 15:02:32 -08:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2010-05-26 10:37:39 -07:00
|
|
|
|
#include <sys/socket.h>
|
2009-11-04 15:02:32 -08:00
|
|
|
|
#include <unistd.h>
|
2016-03-03 10:20:46 -08:00
|
|
|
|
#include "openvswitch/dynamic-string.h"
|
2009-11-04 15:02:32 -08:00
|
|
|
|
#include "packets.h"
|
|
|
|
|
#include "socket-util.h"
|
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "stream-provider.h"
|
|
|
|
|
#include "stream-fd.h"
|
2014-12-15 14:10:38 +01:00
|
|
|
|
#include "openvswitch/vlog.h"
|
2010-07-16 11:02:49 -07:00
|
|
|
|
|
2010-10-19 14:47:01 -07:00
|
|
|
|
VLOG_DEFINE_THIS_MODULE(stream_tcp);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
|
|
|
|
|
/* Active TCP. */
|
|
|
|
|
|
2017-07-14 14:33:44 -07:00
|
|
|
|
/* Takes ownership of 'name'. */
|
2009-11-04 15:02:32 -08:00
|
|
|
|
static int
|
2017-07-14 14:33:44 -07:00
|
|
|
|
new_tcp_stream(char *name, int fd, int connect_status, struct stream **streamp)
|
2009-11-04 15:02:32 -08:00
|
|
|
|
{
|
2014-10-22 15:35:18 -07:00
|
|
|
|
if (connect_status == 0) {
|
|
|
|
|
setsockopt_tcp_nodelay(fd);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-22 15:35:18 -07:00
|
|
|
|
return new_fd_stream(name, fd, connect_status, AF_INET, streamp);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2012-03-10 15:58:10 -08:00
|
|
|
|
tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)
|
2009-11-04 15:02:32 -08:00
|
|
|
|
{
|
|
|
|
|
int fd, error;
|
|
|
|
|
|
2018-04-11 11:24:59 -07:00
|
|
|
|
error = inet_open_active(SOCK_STREAM, suffix, -1, NULL, &fd, dscp);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
if (fd >= 0) {
|
2017-07-14 14:33:44 -07:00
|
|
|
|
return new_tcp_stream(xstrdup(name), fd, error, streamp);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
} else {
|
2013-06-24 10:54:49 -07:00
|
|
|
|
VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
|
2009-11-04 15:02:32 -08:00
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-24 10:22:22 +09:00
|
|
|
|
const struct stream_class tcp_stream_class = {
|
2009-11-04 15:02:32 -08:00
|
|
|
|
"tcp", /* name */
|
2012-04-11 20:18:34 -07:00
|
|
|
|
true, /* needs_probes */
|
2009-11-04 15:02:32 -08:00
|
|
|
|
tcp_open, /* open */
|
|
|
|
|
NULL, /* close */
|
|
|
|
|
NULL, /* connect */
|
|
|
|
|
NULL, /* recv */
|
|
|
|
|
NULL, /* send */
|
2010-01-06 14:26:48 -08:00
|
|
|
|
NULL, /* run */
|
|
|
|
|
NULL, /* run_wait */
|
2009-11-04 15:02:32 -08:00
|
|
|
|
NULL, /* wait */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Passive TCP. */
|
|
|
|
|
|
2014-02-06 16:04:05 -08:00
|
|
|
|
static int ptcp_accept(int fd, const struct sockaddr_storage *,
|
|
|
|
|
size_t, struct stream **streamp);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
|
|
|
|
|
static int
|
2014-10-01 10:38:23 -07:00
|
|
|
|
new_pstream(char *suffix, const char *name, struct pstream **pstreamp,
|
|
|
|
|
int dscp, char *unlink_path, bool kernel_print_port)
|
2009-11-04 15:02:32 -08:00
|
|
|
|
{
|
2014-02-06 16:04:05 -08:00
|
|
|
|
struct sockaddr_storage ss;
|
2013-04-18 16:37:05 -07:00
|
|
|
|
int error;
|
2009-11-04 15:02:32 -08:00
|
|
|
|
int fd;
|
|
|
|
|
|
2014-05-19 11:58:14 -07:00
|
|
|
|
fd = inet_open_passive(SOCK_STREAM, suffix, -1, &ss, dscp,
|
|
|
|
|
kernel_print_port);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
if (fd < 0) {
|
|
|
|
|
return -fd;
|
|
|
|
|
}
|
2010-01-07 13:55:35 -08:00
|
|
|
|
|
2017-07-14 14:33:45 -07:00
|
|
|
|
struct ds bound_name = DS_EMPTY_INITIALIZER;
|
|
|
|
|
if (!name) {
|
|
|
|
|
ds_put_format(&bound_name, "ptcp:%"PRIu16":", ss_get_port(&ss));
|
|
|
|
|
ss_format_address(&ss, &bound_name);
|
|
|
|
|
} else {
|
|
|
|
|
ds_put_cstr(&bound_name, name);
|
2014-10-01 10:38:23 -07:00
|
|
|
|
}
|
2014-02-06 16:04:05 -08:00
|
|
|
|
|
2017-07-14 14:33:45 -07:00
|
|
|
|
error = new_fd_pstream(ds_steal_cstr(&bound_name), fd,
|
2017-07-14 14:33:44 -07:00
|
|
|
|
ptcp_accept, unlink_path, pstreamp);
|
2013-04-18 16:37:05 -07:00
|
|
|
|
if (!error) {
|
2017-07-14 14:33:45 -07:00
|
|
|
|
pstream_set_bound_port(*pstreamp, htons(ss_get_port(&ss)));
|
2013-04-18 16:37:05 -07:00
|
|
|
|
}
|
|
|
|
|
return error;
|
2009-11-04 15:02:32 -08:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-19 11:58:14 -07:00
|
|
|
|
static int
|
|
|
|
|
ptcp_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp,
|
|
|
|
|
uint8_t dscp)
|
|
|
|
|
{
|
2014-10-01 10:38:23 -07:00
|
|
|
|
return new_pstream(suffix, NULL, pstreamp, dscp, NULL, true);
|
2014-05-19 11:58:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2009-11-04 15:02:32 -08:00
|
|
|
|
static int
|
2014-02-06 16:04:05 -08:00
|
|
|
|
ptcp_accept(int fd, const struct sockaddr_storage *ss,
|
|
|
|
|
size_t ss_len OVS_UNUSED, struct stream **streamp)
|
2009-11-04 15:02:32 -08:00
|
|
|
|
{
|
2017-07-14 14:33:45 -07:00
|
|
|
|
struct ds name = DS_EMPTY_INITIALIZER;
|
|
|
|
|
ds_put_cstr(&name, "tcp:");
|
|
|
|
|
ss_format_address(ss, &name);
|
|
|
|
|
ds_put_format(&name, ":%"PRIu16, ss_get_port(ss));
|
2009-11-04 15:02:32 -08:00
|
|
|
|
|
2017-07-14 14:33:45 -07:00
|
|
|
|
return new_tcp_stream(ds_steal_cstr(&name), fd, 0, streamp);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-24 10:22:22 +09:00
|
|
|
|
const struct pstream_class ptcp_pstream_class = {
|
2009-11-04 15:02:32 -08:00
|
|
|
|
"ptcp",
|
2012-04-11 20:18:34 -07:00
|
|
|
|
true,
|
2009-11-04 15:02:32 -08:00
|
|
|
|
ptcp_open,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
2012-09-27 11:18:16 +09:00
|
|
|
|
NULL,
|
2009-11-04 15:02:32 -08:00
|
|
|
|
};
|