Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lxc
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
lxc
Commits
6f6bd412
Unverified
Commit
6f6bd412
authored
Jan 10, 2020
by
Stéphane Graber
Committed by
GitHub
Jan 10, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3248 from brauner/2020-01-05/veth_devices
network: improve veth device creation
parents
90ddf3c0
8bf64b77
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
59 additions
and
24 deletions
+59
-24
lxc_user_nic.c
src/lxc/cmd/lxc_user_nic.c
+10
-21
network.c
src/lxc/network.c
+0
-0
network.h
src/lxc/network.h
+2
-1
api_reboot.c
src/tests/api_reboot.c
+47
-2
No files found.
src/lxc/cmd/lxc_user_nic.c
View file @
6f6bd412
...
@@ -487,11 +487,11 @@ static char *find_line(char *buf_start, char *buf_end, char *name,
...
@@ -487,11 +487,11 @@ static char *find_line(char *buf_start, char *buf_end, char *name,
return
NULL
;
return
NULL
;
}
}
static
int
instantiate_veth
(
char
*
veth1
,
char
*
veth2
)
static
int
instantiate_veth
(
char
*
veth1
,
char
*
veth2
,
pid_t
pid
,
unsigned
int
mtu
)
{
{
int
ret
;
int
ret
;
ret
=
lxc_veth_create
(
veth1
,
veth2
);
ret
=
lxc_veth_create
(
veth1
,
veth2
,
pid
,
mtu
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
errno
=
-
ret
;
errno
=
-
ret
;
CMD_SYSERROR
(
"Failed to create %s-%s
\n
"
,
veth1
,
veth2
);
CMD_SYSERROR
(
"Failed to create %s-%s
\n
"
,
veth1
,
veth2
);
...
@@ -524,8 +524,9 @@ static int get_mtu(char *name)
...
@@ -524,8 +524,9 @@ static int get_mtu(char *name)
static
int
create_nic
(
char
*
nic
,
char
*
br
,
int
pid
,
char
**
cnic
)
static
int
create_nic
(
char
*
nic
,
char
*
br
,
int
pid
,
char
**
cnic
)
{
{
unsigned
int
mtu
=
1500
;
int
ret
;
char
veth1buf
[
IFNAMSIZ
],
veth2buf
[
IFNAMSIZ
];
char
veth1buf
[
IFNAMSIZ
],
veth2buf
[
IFNAMSIZ
];
int
mtu
,
ret
;
ret
=
snprintf
(
veth1buf
,
IFNAMSIZ
,
"%s"
,
nic
);
ret
=
snprintf
(
veth1buf
,
IFNAMSIZ
,
"%s"
,
nic
);
if
(
ret
<
0
||
ret
>=
IFNAMSIZ
)
{
if
(
ret
<
0
||
ret
>=
IFNAMSIZ
)
{
...
@@ -539,16 +540,19 @@ static int create_nic(char *nic, char *br, int pid, char **cnic)
...
@@ -539,16 +540,19 @@ static int create_nic(char *nic, char *br, int pid, char **cnic)
return
-
1
;
return
-
1
;
}
}
if
(
strcmp
(
br
,
"none"
))
mtu
=
get_mtu
(
br
);
if
(
!
mtu
)
mtu
=
1500
;
/* create the nics */
/* create the nics */
ret
=
instantiate_veth
(
veth1buf
,
veth2buf
);
ret
=
instantiate_veth
(
veth1buf
,
veth2buf
,
pid
,
mtu
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
usernic_error
(
"%s"
,
"Error creating veth tunnel
\n
"
);
usernic_error
(
"%s"
,
"Error creating veth tunnel
\n
"
);
return
-
1
;
return
-
1
;
}
}
if
(
strcmp
(
br
,
"none"
))
{
if
(
strcmp
(
br
,
"none"
))
{
/* copy the bridge's mtu to both ends */
mtu
=
get_mtu
(
br
);
if
(
mtu
>
0
)
{
if
(
mtu
>
0
)
{
ret
=
lxc_netdev_set_mtu
(
veth1buf
,
mtu
);
ret
=
lxc_netdev_set_mtu
(
veth1buf
,
mtu
);
if
(
ret
<
0
)
{
if
(
ret
<
0
)
{
...
@@ -556,13 +560,6 @@ static int create_nic(char *nic, char *br, int pid, char **cnic)
...
@@ -556,13 +560,6 @@ static int create_nic(char *nic, char *br, int pid, char **cnic)
mtu
,
veth1buf
);
mtu
,
veth1buf
);
goto
out_del
;
goto
out_del
;
}
}
ret
=
lxc_netdev_set_mtu
(
veth2buf
,
mtu
);
if
(
ret
<
0
)
{
usernic_error
(
"Failed to set mtu to %d on %s
\n
"
,
mtu
,
veth2buf
);
goto
out_del
;
}
}
}
/* attach veth1 to bridge */
/* attach veth1 to bridge */
...
@@ -573,14 +570,6 @@ static int create_nic(char *nic, char *br, int pid, char **cnic)
...
@@ -573,14 +570,6 @@ static int create_nic(char *nic, char *br, int pid, char **cnic)
}
}
}
}
/* pass veth2 to target netns */
ret
=
lxc_netdev_move_by_name
(
veth2buf
,
pid
,
NULL
);
if
(
ret
<
0
)
{
usernic_error
(
"Error moving %s to network namespace of %d
\n
"
,
veth2buf
,
pid
);
goto
out_del
;
}
*
cnic
=
strdup
(
veth2buf
);
*
cnic
=
strdup
(
veth2buf
);
if
(
!*
cnic
)
{
if
(
!*
cnic
)
{
usernic_error
(
"Failed to copy string
\"
%s
\"\n
"
,
veth2buf
);
usernic_error
(
"Failed to copy string
\"
%s
\"\n
"
,
veth2buf
);
...
...
src/lxc/network.c
View file @
6f6bd412
This diff is collapsed.
Click to expand it.
src/lxc/network.h
View file @
6f6bd412
...
@@ -203,7 +203,8 @@ extern int lxc_netdev_down(const char *name);
...
@@ -203,7 +203,8 @@ extern int lxc_netdev_down(const char *name);
extern
int
lxc_netdev_set_mtu
(
const
char
*
name
,
int
mtu
);
extern
int
lxc_netdev_set_mtu
(
const
char
*
name
,
int
mtu
);
/* Create a virtual network devices. */
/* Create a virtual network devices. */
extern
int
lxc_veth_create
(
const
char
*
name1
,
const
char
*
name2
);
extern
int
lxc_veth_create
(
const
char
*
name1
,
const
char
*
name2
,
pid_t
pid
,
unsigned
int
mtu
);
extern
int
lxc_macvlan_create
(
const
char
*
master
,
const
char
*
name
,
int
mode
);
extern
int
lxc_macvlan_create
(
const
char
*
master
,
const
char
*
name
,
int
mode
);
extern
int
lxc_vlan_create
(
const
char
*
master
,
const
char
*
name
,
extern
int
lxc_vlan_create
(
const
char
*
master
,
const
char
*
name
,
unsigned
short
vid
);
unsigned
short
vid
);
...
...
src/tests/api_reboot.c
View file @
6f6bd412
...
@@ -29,17 +29,46 @@
...
@@ -29,17 +29,46 @@
#include "lxc/lxccontainer.h"
#include "lxc/lxccontainer.h"
#include "lxctest.h"
#include "lxctest.h"
#include "utils.h"
#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
#endif
#define TSTNAME "lxc-api-reboot"
int
main
(
int
argc
,
char
*
argv
[])
int
main
(
int
argc
,
char
*
argv
[])
{
{
int
i
;
int
i
;
struct
lxc_container
*
c
;
struct
lxc_container
*
c
;
int
ret
=
EXIT_FAILURE
;
int
ret
=
EXIT_FAILURE
;
struct
lxc_log
log
;
char
template
[
sizeof
(
P_tmpdir
"/reboot_XXXXXX"
)];
(
void
)
strlcpy
(
template
,
P_tmpdir
"/reboot_XXXXXX"
,
sizeof
(
template
));
i
=
lxc_make_tmpfile
(
template
,
false
);
if
(
i
<
0
)
{
lxc_error
(
"Failed to create temporary log file for container %s
\n
"
,
TSTNAME
);
exit
(
EXIT_FAILURE
);
}
else
{
lxc_debug
(
"Using
\"
%s
\"
as temporary log file for container %s
\n
"
,
template
,
TSTNAME
);
close
(
i
);
}
log
.
name
=
TSTNAME
;
log
.
file
=
template
;
log
.
level
=
"TRACE"
;
log
.
prefix
=
"reboot"
;
log
.
quiet
=
false
;
log
.
lxcpath
=
NULL
;
if
(
lxc_log_init
(
&
log
))
exit
(
ret
);
/* Test that the reboot() API function properly waits for containers to
/* Test that the reboot() API function properly waits for containers to
* restart.
* restart.
*/
*/
c
=
lxc_container_new
(
"reboot"
,
NULL
);
c
=
lxc_container_new
(
TSTNAME
,
NULL
);
if
(
!
c
)
{
if
(
!
c
)
{
lxc_error
(
"%s"
,
"Failed to create container
\"
reboot
\"
"
);
lxc_error
(
"%s"
,
"Failed to create container
\"
reboot
\"
"
);
exit
(
ret
);
exit
(
ret
);
...
@@ -120,8 +149,24 @@ on_error_stop:
...
@@ -120,8 +149,24 @@ on_error_stop:
on_error_put:
on_error_put:
lxc_container_put
(
c
);
lxc_container_put
(
c
);
if
(
ret
==
EXIT_SUCCESS
)
if
(
ret
==
EXIT_SUCCESS
)
{
lxc_debug
(
"%s
\n
"
,
"All reboot tests passed"
);
lxc_debug
(
"%s
\n
"
,
"All reboot tests passed"
);
}
else
{
int
fd
;
fd
=
open
(
template
,
O_RDONLY
);
if
(
fd
>=
0
)
{
char
buf
[
4096
];
ssize_t
buflen
;
while
((
buflen
=
read
(
fd
,
buf
,
1024
))
>
0
)
{
buflen
=
write
(
STDERR_FILENO
,
buf
,
buflen
);
if
(
buflen
<=
0
)
break
;
}
close
(
fd
);
}
}
(
void
)
unlink
(
template
);
exit
(
ret
);
exit
(
ret
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment