Skip to content

Commit c000dcf

Browse files
Dmitry Shulgadahlerlend
authored andcommitted
WL#12720 Add support for network namespaces on setting up listening sockets
The following changes were done within this worklog: * Extended format of a value for the bind-addressi, mysqlx-bind-address and admin-address options to allow specifying a network namespace that be used for listening incoming connections for corresponding interface addresses. * Added handling of network namespace on socket creation and IP address to host name resolving. * Extended a grammar of the statement CHANGE MASTER TO in order to support the clause NETWORK_NAMESPACE=string_value. This clause is used for specifying of a network namespace that a replication master uses for listening connection requests from a replication slave. * Provided storing of a network namespace in the table slave_master_info. * The error code ER_WILDCARD_NOT_ALLOWED_FOR_MULTIADDRESS_BIND was added to be output in case wildcard value specified for multi address --bind-address option value. It was done to be uniform with ER_NETWORK_NAMESPACE_NOT_ALLOWED_FOR_WILDCARD_ADDRESS that used for similar use case for network namespaces. * The error code ER_NETWORK_NAMESPACES_NOT_SUPPORTED was added to report error condition when a network namespace is specified on a platform that doesn't support network namespace feature. * The error code ER_UNKNOWN_NETWORK_NAMESPACE was added to report error condition when a specified network namespace is not configured on a host * The error code ER_SETNS_FAILED was added to report error condition that attempt to switch on a some network namespace failed. Reviewed-by: Thayumanavar Sachithanantham <[email protected]>
1 parent 17096ec commit c000dcf

File tree

85 files changed

+2130
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2130
-160
lines changed

client/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ADD_SUBDIRECTORY(base)
2929
## Subdirectory for mysqlpump code.
3030
ADD_SUBDIRECTORY(dump)
3131

32-
MYSQL_ADD_EXECUTABLE(mysql completion_hash.cc mysql.cc readline.cc ../sql-common/sql_string.cc pattern_matcher.cc)
32+
MYSQL_ADD_EXECUTABLE(mysql completion_hash.cc mysql.cc readline.cc ../sql-common/sql_string.cc pattern_matcher.cc ${CMAKE_SOURCE_DIR}/sql/net_ns.cc)
3333
TARGET_LINK_LIBRARIES(mysql mysqlclient)
3434
IF(UNIX)
3535
TARGET_LINK_LIBRARIES(mysql ${EDITLINE_LIBRARY})

client/mysql.cc

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License, version 2.0,
@@ -89,6 +89,8 @@
8989

9090
#include "sql_common.h"
9191

92+
#include "sql/net_ns.h"
93+
9294
using std::max;
9395
using std::min;
9496

@@ -187,6 +189,9 @@ static STATUS status;
187189
static ulong select_limit, max_join_size, opt_connect_timeout = 0;
188190
static char mysql_charsets_dir[FN_REFLEN + 1];
189191
static char *opt_plugin_dir = 0, *opt_default_auth = 0;
192+
#ifdef HAVE_SETNS
193+
static char *opt_network_namespace = 0;
194+
#endif
190195
static const char *xmlmeta[] = {
191196
"&", "&amp;", "<", "&lt;", ">", "&gt;", "\"", "&quot;",
192197
/* Turn \0 into a space. Why not &#0;? That's not valid XML or HTML. */
@@ -1528,15 +1533,28 @@ static void kill_query(const char *reason) {
15281533
kill_mysql = mysql_init(kill_mysql);
15291534
init_connection_options(kill_mysql);
15301535

1536+
#ifdef HAVE_SETNS
1537+
if (opt_network_namespace && set_network_namespace(opt_network_namespace)) {
1538+
goto err;
1539+
}
1540+
#endif
1541+
15311542
if (!mysql_real_connect(kill_mysql, current_host, current_user, opt_password,
15321543
"", opt_mysql_port, opt_mysql_unix_port, 0)) {
1544+
#ifdef HAVE_SETNS
1545+
if (opt_network_namespace) (void)restore_original_network_namespace();
1546+
#endif
15331547
tee_fprintf(stdout,
15341548
"%s -- Sorry, cannot connect to the server to kill "
15351549
"query, giving up ...\n",
15361550
reason);
15371551
goto err;
15381552
}
15391553

1554+
#ifdef HAVE_SETNS
1555+
if (opt_network_namespace && restore_original_network_namespace()) goto err;
1556+
#endif
1557+
15401558
interrupted_query = true;
15411559

15421560
/* mysqld < 5 does not understand KILL QUERY, skip to KILL CONNECTION */
@@ -1552,7 +1570,11 @@ static void kill_query(const char *reason) {
15521570
tee_fprintf(stdout, "%s -- query aborted\n", reason);
15531571

15541572
err:
1573+
#ifdef HAVE_SETNS
1574+
if (opt_network_namespace) (void)release_network_namespace_resources();
1575+
#endif
15551576
mysql_close(kill_mysql);
1577+
15561578
return;
15571579
}
15581580

@@ -1821,6 +1843,12 @@ static struct my_option my_long_options[] = {
18211843
"test purpose, so it is just built when DEBUG is on.",
18221844
&opt_build_completion_hash, &opt_build_completion_hash, 0, GET_BOOL,
18231845
NO_ARG, 0, 0, 0, 0, 0, 0},
1846+
#endif
1847+
#ifdef HAVE_SETNS
1848+
{"network-namespace", 0,
1849+
"Network namespace to use for connection via tcp with a server.",
1850+
&opt_network_namespace, &opt_network_namespace, 0, GET_STR, REQUIRED_ARG,
1851+
0, 0, 0, 0, 0, 0},
18241852
#endif
18251853
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}};
18261854

@@ -4364,6 +4392,9 @@ static int sql_real_connect(char *host, char *database, char *user,
43644392
char *password, uint silent) {
43654393
if (connected) {
43664394
connected = 0;
4395+
#ifdef HAVE_SETNS
4396+
if (opt_network_namespace) (void)release_network_namespace_resources();
4397+
#endif
43674398
mysql_close(&mysql);
43684399
}
43694400

@@ -4392,9 +4423,25 @@ static int sql_real_connect(char *host, char *database, char *user,
43924423
}
43934424
#endif
43944425

4426+
#ifdef HAVE_SETNS
4427+
if (opt_network_namespace && set_network_namespace(opt_network_namespace)) {
4428+
if (!silent) {
4429+
char msgbuf[PATH_MAX];
4430+
snprintf(msgbuf, sizeof(msgbuf), "Network namespace error: %s",
4431+
strerror(errno));
4432+
put_info(msgbuf, INFO_ERROR);
4433+
}
4434+
4435+
return ignore_errors ? -1 : 1; // Abort
4436+
}
4437+
#endif
4438+
43954439
if (!mysql_real_connect(&mysql, host, user, password, database,
43964440
opt_mysql_port, opt_mysql_unix_port,
43974441
connect_flag | CLIENT_MULTI_STATEMENTS)) {
4442+
#ifdef HAVE_SETNS
4443+
if (opt_network_namespace) (void)restore_original_network_namespace();
4444+
#endif
43984445
if (mysql_errno(&mysql) == ER_MUST_CHANGE_PASSWORD_LOGIN) {
43994446
tee_fprintf(stdout,
44004447
"Please use --connect-expired-password option or "
@@ -4410,6 +4457,19 @@ static int sql_real_connect(char *host, char *database, char *user,
44104457
return -1; // Retryable
44114458
}
44124459

4460+
#ifdef HAVE_SETNS
4461+
if (opt_network_namespace && restore_original_network_namespace()) {
4462+
if (!silent) {
4463+
char msgbuf[PATH_MAX];
4464+
snprintf(msgbuf, sizeof(msgbuf), "Network namespace error: %s",
4465+
strerror(errno));
4466+
put_info(msgbuf, INFO_ERROR);
4467+
}
4468+
4469+
return ignore_errors ? -1 : 1; // Abort
4470+
}
4471+
#endif
4472+
44134473
#ifdef _WIN32
44144474
/* Convert --execute buffer from UTF8MB4 to connection character set */
44154475
if (!execute_buffer_conversion_done++ && status.line_buff &&

config.h.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
#cmakedefine HAVE_ISINF 1
162162

163163
#cmakedefine HAVE_KQUEUE 1
164+
#cmakedefine HAVE_SETNS 1
164165
#cmakedefine HAVE_KQUEUE_TIMERS 1
165166
#cmakedefine HAVE_POSIX_TIMERS 1
166167

configure.cmake

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0,
@@ -375,6 +375,19 @@ ENDIF()
375375
CHECK_FUNCTION_EXISTS (timer_create HAVE_TIMER_CREATE)
376376
CHECK_FUNCTION_EXISTS (timer_settime HAVE_TIMER_SETTIME)
377377
CHECK_FUNCTION_EXISTS (kqueue HAVE_KQUEUE)
378+
379+
# Check whether the setns() API function supported by a target platform
380+
CHECK_C_SOURCE_RUNS("
381+
#ifndef _GNU_SOURCE
382+
#define _GNU_SOURCE
383+
#endif
384+
#include <sched.h>
385+
int main()
386+
{
387+
(void)setns(0, 0);
388+
return 0;
389+
}" HAVE_SETNS)
390+
378391
CHECK_SYMBOL_EXISTS(EVFILT_TIMER "sys/types.h;sys/event.h;sys/time.h" HAVE_EVFILT_TIMER)
379392
IF(HAVE_KQUEUE AND HAVE_EVFILT_TIMER)
380393
SET(HAVE_KQUEUE_TIMERS 1 CACHE INTERNAL "Have kqueue timer-related filter")

include/violite.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#endif
3838
#include <sys/types.h>
3939

40+
#include <string>
41+
4042
#include "my_inttypes.h"
4143
#include "my_psi_config.h" // IWYU pragma: keep
4244
#include "mysql/components/services/my_io_bits.h"
@@ -368,6 +370,12 @@ struct Vio {
368370
std::atomic_flag kevent_wakeup_flag = ATOMIC_FLAG_INIT;
369371
#endif
370372

373+
#ifdef HAVE_SETNS
374+
/**
375+
Socket network namespace.
376+
*/
377+
char network_namespace[256];
378+
#endif
371379
/*
372380
VIO vtable interface to be implemented by VIO's like SSL, Socket,
373381
Named Pipe, etc.

mysql-test/collections/disabled.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,5 @@ rpl.rpl_multi_source_corrupt_repository : Bug#28765425 Disabled until the bug
7878

7979
# sysschema suite tests
8080
sysschema.v_wait_classes_global_by_avg_latency : BUG#21550054 Test fails too often.
81+
82+
# x plugin suite tests

mysql-test/include/check-testcase.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
33
#
44

55
# ==== Purpose ====
@@ -86,6 +86,7 @@ if ($tmp) {
8686
--echo Master_TLS_Version
8787
--echo Master_public_key_path
8888
--echo Get_master_public_key 0
89+
--echo Network_Namespace
8990
}
9091

9192
if (!$tmp) {

mysql-test/r/disabled_replication.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SHOW SLAVE STATUS;
2-
Slave_IO_State Master_Host Master_User Master_Port Connect_Retry Master_Log_File Read_Master_Log_Pos Relay_Log_File Relay_Log_Pos Relay_Master_Log_File Slave_IO_Running Slave_SQL_Running Replicate_Do_DB Replicate_Ignore_DB Replicate_Do_Table Replicate_Ignore_Table Replicate_Wild_Do_Table Replicate_Wild_Ignore_Table Last_Errno Last_Error Skip_Counter Exec_Master_Log_Pos Relay_Log_Space Until_Condition Until_Log_File Until_Log_Pos Master_SSL_Allowed Master_SSL_CA_File Master_SSL_CA_Path Master_SSL_Cert Master_SSL_Cipher Master_SSL_Key Seconds_Behind_Master Master_SSL_Verify_Server_Cert Last_IO_Errno Last_IO_Error Last_SQL_Errno Last_SQL_Error Replicate_Ignore_Server_Ids Master_Server_Id Master_UUID Master_Info_File SQL_Delay SQL_Remaining_Delay Slave_SQL_Running_State Master_Retry_Count Master_Bind Last_IO_Error_Timestamp Last_SQL_Error_Timestamp Master_SSL_Crl Master_SSL_Crlpath Retrieved_Gtid_Set Executed_Gtid_Set Auto_Position Replicate_Rewrite_DB Channel_Name Master_TLS_Version Master_public_key_path Get_master_public_key
2+
Slave_IO_State Master_Host Master_User Master_Port Connect_Retry Master_Log_File Read_Master_Log_Pos Relay_Log_File Relay_Log_Pos Relay_Master_Log_File Slave_IO_Running Slave_SQL_Running Replicate_Do_DB Replicate_Ignore_DB Replicate_Do_Table Replicate_Ignore_Table Replicate_Wild_Do_Table Replicate_Wild_Ignore_Table Last_Errno Last_Error Skip_Counter Exec_Master_Log_Pos Relay_Log_Space Until_Condition Until_Log_File Until_Log_Pos Master_SSL_Allowed Master_SSL_CA_File Master_SSL_CA_Path Master_SSL_Cert Master_SSL_Cipher Master_SSL_Key Seconds_Behind_Master Master_SSL_Verify_Server_Cert Last_IO_Errno Last_IO_Error Last_SQL_Errno Last_SQL_Error Replicate_Ignore_Server_Ids Master_Server_Id Master_UUID Master_Info_File SQL_Delay SQL_Remaining_Delay Slave_SQL_Running_State Master_Retry_Count Master_Bind Last_IO_Error_Timestamp Last_SQL_Error_Timestamp Master_SSL_Crl Master_SSL_Crlpath Retrieved_Gtid_Set Executed_Gtid_Set Auto_Position Replicate_Rewrite_DB Channel_Name Master_TLS_Version Master_public_key_path Get_master_public_key Network_Namespace
33
RESET SLAVE;
44
ERROR HY000: Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in the MySQL error log.
55
SHOW RELAYLOG EVENTS;

mysql-test/r/information_schema_keywords.result

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ NCHAR 0
375375
NDB 0
376376
NDBCLUSTER 0
377377
NESTED 0
378+
NETWORK_NAMESPACE 0
378379
NEVER 0
379380
NEW 0
380381
NEXT 0

mysql-test/r/mysqld--help-notwin.result

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ The following options may be given as the first argument:
1717
--admin-address=name
1818
IP address to bind to for service connection. Address can
1919
be an IPv4 address, IPv6 address, or host name. Wildcard
20-
values *, ::, 0.0.0.0 are not allowed.
20+
values *, ::, 0.0.0.0 are not allowed. Address value can
21+
have following optional network namespace separated by
22+
the delimiter / from the address value. E.g., the
23+
following value 192.168.1.1/red specifies IP addresses to
24+
listen for incoming TCP connections that have to be
25+
placed into the namespace 'red'. Using of network
26+
namespace requires its support from underlying Operating
27+
System. Attempt to specify a network namespace for a
28+
platform that doesn't support it results in error during
29+
socket creation.
2130
--admin-port=# Port number to use for service connection, built-in
2231
default (33062)
2332
--allow-suspicious-udfs
@@ -55,6 +64,19 @@ The following options may be given as the first argument:
5564
name or one of the wildcard values *, ::, 0.0.0.0. In
5665
case more than one address is specified in a
5766
comma-separated list, wildcard values are not allowed.
67+
Every address can have optional network namespace
68+
separated by the delimiter / from the address value.
69+
E.g., the following value
70+
192.168.1.1/red,172.16.1.1/green,193.168.1.1 specifies
71+
three IP addresses to listen for incoming TCP connections
72+
two of that have to be placed in corresponding
73+
namespaces: the address 192.168.1.1 must be placed into
74+
the namespace red and the address 172.16.1.1 must be
75+
placed into the namespace green. Using of network
76+
namespace requires its support from underlying Operating
77+
System. Attempt to specify a network namespace for a
78+
platform that doesn't support it results in error during
79+
socket creation.
5880
--binlog-cache-size=#
5981
The size of the transactional cache for updates to
6082
transactional engines for the binary log. If you often

0 commit comments

Comments
 (0)