Skip to content

Conversation

@am11
Copy link
Member

@am11 am11 commented Jan 10, 2020

Managed code is shared between Linux and FreeBSD, native parts are implemented alongside Linux, using route(4).

To run the unit tests, used corefx 3.0 build of corefx (Debug), coreclr and core-setup (Release). Then cherry-picked the bits back to runtime master. (haven't tried building entire dotnet/runtime repo on FreeBSD yet)

To test the native implementation, following technique was used:

cd dotnet-runtime
# did a quick corefx native build on FreeBSD x64 (and later Linux x64)
./src/libraries/Native/build-native.sh

# compiled some glue / main code with pal_networkchange.c and friends
# (adjust next line; location of runtime repo)
RepoRoot=/datadrive/projects/dotnet-runtime
UnixBasePath=$RepoRoot/src/libraries/Native/Unix
CC=clang # or gcc

$CC -xc $UnixBasePath/System.Native/pal_networkchange.c $UnixBasePath/System.Native/pal_errno.c \
  -I$(dirname $(find $RepoRoot/artifacts/obj/native/ -name pal_config.h)) \
  -I$UnixBasePath/System.Native -I$UnixBasePath/Common \
  -o start_listener -std=gnu99 -D_GNU_SOURCE -<<EOF
#include <stdio.h>
#include <sys/types.h>
#include <stdbool.h>
#include "pal_errno.h"
#include "pal_networkchange.h"

void cb(int32_t sock, NetworkChangeKind notificationKind)
{
    printf("sock: %d, kind: %d\n", sock, notificationKind);
}

int main()
{
    int32_t newSocket;
    int err = SystemNative_CreateNetworkChangeListenerSocket(&newSocket);
    if(err != 0)
    {
        printf("error: %d\n", err);
        return err;
    }

    printf("starting to read events from sock fd: %d, press Ctrl+C to exit\n", newSocket);
    while(true) SystemNative_ReadEvents(newSocket, cb);

    return 0;
}
EOF


# run the listener app
./start_listener

then in a separate terminal:

# FreeBSD

# add ips to an iface, in order to fire NetworkAddressChanged (non-disruptive manner)
sudo ifconfig em0 192.168.123.4/24 alias
# to revert, run the above with -alias

# to restart network, in order to fire NetworkAvailabilityChanged
# (bit disruptive, but will not lose the ssh session)
sudo /etc/rc.d/netif restart

######

# Linux

# add ips to an iface, in order to fire NetworkAddressChanged (non-disruptive manner)
sudo ip addr add 192.168.123.4/24 dev eth0
# to revert, replace add with del

# to restart network, in order to fire NetworkAvailabilityChanged
# (this can be disruptive if we run both commands spearately,
# i lost session to a remote vm, then learned to join them on single
# line in order to keep the ssh session intact)
sudo ifconfig eth0 down; sudo ifconfig eth0 up

Fixes dotnet/corefx#33530

@am11
Copy link
Member Author

am11 commented Jan 10, 2020

cc @wfurt

@am11
Copy link
Member Author

am11 commented Jan 18, 2020

Resolved merge conflicts. @stephentoub, @wfurt, could you please give it another pass? Thanks.

Copy link
Member

@wfurt wfurt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. thanks @am11.

{
onNetworkChange(sock, AvailabilityChanged);
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: why are the braces needed here but not for the other cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in #2071.

@stephentoub stephentoub merged commit a412911 into dotnet:master Jan 22, 2020
@am11 am11 deleted the feature/gh-33530_network-change-on-freebsd branch January 23, 2020 12:16
@karelz karelz added this to the 5.0.0 milestone Aug 18, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants