Skip to content

Commit

Permalink
[nozzle] cleanup and document nozzle_set_mtu and nozzle_reset_mtu
Browse files Browse the repository at this point in the history
couldn't split those 2 because they are stricly related

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Dec 15, 2017
1 parent eac8cac commit c03189e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 19 deletions.
66 changes: 49 additions & 17 deletions libnozzle/libnozzle.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,13 +651,21 @@ int nozzle_get_mtu(const nozzle_t nozzle)
return err;
}

int nozzle_set_mtu(nozzle_t nozzle, const int mtu)
int nozzle_set_mtu(nozzle_t nozzle, const int mtu, char **error_string)
{
int err = 0, savederrno = 0;
struct _ip *tmp_ip;
char *error_string = NULL;
int err;

pthread_mutex_lock(&lib_mutex);
if ((!nozzle) || (!mtu) || (!error_string)) {
errno = EINVAL;
return -1;
}

savederrno = pthread_mutex_lock(&lib_mutex);
if (savederrno) {
errno = savederrno;
return -1;
}

if (!_check(nozzle)) {
errno = EINVAL;
Expand All @@ -666,21 +674,28 @@ int nozzle_set_mtu(nozzle_t nozzle, const int mtu)
}

err = nozzle->current_mtu = _get_mtu(nozzle);
if (err < 0)
if (err < 0) {
savederrno = errno;
goto out_clean;
}

nozzle->ifr.ifr_mtu = mtu;

err = ioctl(lib_cfg.sockfd, SIOCSIFMTU, &nozzle->ifr);
if (err) {
savederrno = errno;
goto out_clean;
}

if ((!err) && (nozzle->current_mtu < 1280) && (mtu >= 1280)) {
if ((nozzle->current_mtu < 1280) && (mtu >= 1280)) {
tmp_ip = nozzle->ip;
while(tmp_ip) {
if (tmp_ip->domain == AF_INET6) {
err = _set_ip(nozzle, "add", tmp_ip->ip_addr, tmp_ip->prefix, &error_string, 0);
if (error_string) {
free(error_string);
error_string = NULL;
err = _set_ip(nozzle, "add", tmp_ip->ip_addr, tmp_ip->prefix, error_string, 0);
if (err) {
savederrno = errno;
err = -1;
goto out_clean;
}
}
tmp_ip = tmp_ip->next;
Expand All @@ -689,13 +704,13 @@ int nozzle_set_mtu(nozzle_t nozzle, const int mtu)

out_clean:
pthread_mutex_unlock(&lib_mutex);

errno = savederrno;
return err;
}

int nozzle_reset_mtu(nozzle_t nozzle)
int nozzle_reset_mtu(nozzle_t nozzle, char **error_string)
{
return nozzle_set_mtu(nozzle, nozzle->default_mtu);
return nozzle_set_mtu(nozzle, nozzle->default_mtu, error_string);
}

int nozzle_get_mac(const nozzle_t nozzle, char **ether_addr)
Expand Down Expand Up @@ -1537,6 +1552,7 @@ static int check_knet_mtu(void)
int err=0;
nozzle_t nozzle;
char *error_down = NULL, *error_postdown = NULL;
char *error_string = NULL;

int current_mtu = 0;
int expected_mtu = 1500;
Expand Down Expand Up @@ -1565,8 +1581,12 @@ static int check_knet_mtu(void)

printf("Setting MTU to 9000\n");
expected_mtu = 9000;
if (nozzle_set_mtu(nozzle, expected_mtu) < 0) {
if (nozzle_set_mtu(nozzle, expected_mtu, &error_string) < 0) {
printf("Unable to set MTU to %d\n", expected_mtu);
if (error_string) {
printf("error: %s\n", error_string);
free(error_string);
}
err = -1;
goto out_clean;
}
Expand All @@ -1593,8 +1613,12 @@ static int check_knet_mtu(void)
}

printf("Passing empty struct to set_mtu\n");
if (nozzle_set_mtu(NULL, 1500) == 0) {
if (nozzle_set_mtu(NULL, 1500, &error_string) == 0) {
printf("Something is wrong in nozzle_set_mtu sanity checks\n");
if (error_string) {
printf("error: %s\n", error_string);
free(error_string);
}
err = -1;
goto out_clean;
}
Expand Down Expand Up @@ -1671,8 +1695,12 @@ static int check_knet_mtu_ipv6(void)
}

printf("Setting MTU to 1200\n");
if (nozzle_set_mtu(nozzle, 1200) < 0) {
if (nozzle_set_mtu(nozzle, 1200, &error_string) < 0) {
printf("Unable to set MTU to 1200\n");
if (error_string) {
printf("error: %s\n", error_string);
free(error_string);
}
err = -1;
goto out_clean;
}
Expand Down Expand Up @@ -1728,8 +1756,12 @@ static int check_knet_mtu_ipv6(void)
}

printf("Restoring MTU to default\n");
if (nozzle_reset_mtu(nozzle) < 0) {
if (nozzle_reset_mtu(nozzle, &error_string) < 0) {
printf("Unable to reset mtu\n");
if (error_string) {
printf("error: %s\n", error_string);
free(error_string);
}
err = -1;
goto out_clean;
}
Expand Down
42 changes: 40 additions & 2 deletions libnozzle/libnozzle.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,46 @@ int nozzle_get_ips(const nozzle_t nozzle, char **ip_addr_list, int *entries);
*/

int nozzle_get_mtu(const nozzle_t nozzle);
int nozzle_set_mtu(nozzle_t nozzle, const int mtu);
int nozzle_reset_mtu(nozzle_t nozzle);

/**
* nozzle_set_mtu
* @brief set mtu on a given nozzle interface
*
* nozzle - pointer to the nozzle struct
*
* mtu - new MTU value
*
* error_string - pointer to string to record errors from ipaddr2 (Linux) or ifconfig (BSD)
* when re-instanting IPv6 address if MTU is becoming again > 1280.
* The string is malloc'ed, the caller needs to free this buffer.
*
* @return
* 0 on success
* -1 on error and errno is set.
* error_string is set to NULL on success
* error_string will contain a string recording the execution error.
*/

int nozzle_set_mtu(nozzle_t nozzle, const int mtu, char **error_string);

/**
* nozzle_reset_mtu
* @brief reset mtu on a given nozzle interface to the system default
*
* nozzle - pointer to the nozzle struct
*
* error_string - pointer to string to record errors from ipaddr2 (Linux) or ifconfig (BSD)
* when re-instanting IPv6 address if MTU is becoming again > 1280.
* The string is malloc'ed, the caller needs to free this buffer.
*
* @return
* 0 on success
* -1 on error and errno is set.
* error_string is set to NULL on success
* error_string will contain a string recording the execution error.
*/

int nozzle_reset_mtu(nozzle_t nozzle, char **error_string);

int nozzle_get_mac(const nozzle_t nozzle, char **ether_addr);
int nozzle_set_mac(nozzle_t nozzle, const char *ether_addr);
Expand Down

0 comments on commit c03189e

Please sign in to comment.