Slow wifi reconnect after suspend on focal, HP ProBook 650 with BCM43228

Solution 1:

OK, I found a solution for my problem, it is based on one of the suggestions in How to prevent wifi sleep after suspend

I created a file /lib/systemd/system-sleep/wlwifi-reset with this content:

#!/bin/sh

# NAME: /lib/systemd/system-sleep/wlwifi-reset
# DESC: Resets Broadcom WiFi after suspend to speed up reconnect
# DATE: Apr 28, 2020

MYNAME=$0
restart_wifi() {
    /usr/bin/logger $MYNAME 'restart wifi to speed up reconnect'
    /sbin/modprobe -v -r wl
    /sbin/modprobe -v wl
    /usr/bin/logger $MYNAME 'restart wifi done'
}

/usr/bin/logger $MYNAME 'case=[' ${1}' ]'
case "${1}/${2}" in
    hibernate|suspend|pre*)
          ;;
    resume|thaw|post*)
          restart_wifi;;
esac

Make it executable:

sudo chmod a+x /lib/systemd/system-sleep/wlwifi-reset

Now the wl kernel module is reloaded when the laptop wakes up and reconnect time is a couple of seconds!

Not sure if this is the best solution and why it works out of the box on 18.04... If anyone has a better solution: feel free to share.

Solution 2:

I had a similar problem and none of the solutions I found fixed it completely. For me, it was taking up to 5 minutes after resuming from suspend before the WiFi would connect. I had initially tried editing the configuration in /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf, but I found that it didn't do anything. After some more digging, I found the solution on Let's FOSS - WiFi not working after Suspend in Ubuntu, where it triggers the network manager service to restart on resume, but it still took like an entire minute after the network manager service restarted.

But I noticed that restarting the Network Manager using sudo service network-manager restart, caused it to connect within 30 seconds to a minute.

So, after playing around with the settings and doing more research, I came to the following solution.

SOLUTION

  1. Open up terminal (CTRL+ALT+T) and enter sudo nano /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf, and edit it so its contents look like this:

    [connection]
    wifi.powersave = 1
    
  2. Save the file by pressing CTRL+X, then press ENTER to save to the current file path.

  3. Enter the following command to make a default-wifi-powersave-off.conf file with the same contents as default-wifi-powersave-on.conf

    sudo cp /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf \
    /etc/NetworkManager/conf.d/default-wifi-powersave-off.conf
    
  4. Create and edit a new script, /lib/systemd/system-sleep/wififix, with the following command

    sudo nano /lib/systemd/system-sleep/wififix
    
  5. Add the following to wififix:

    #!/bin/sh
    # command line input we're expecting:
    # # wififix <pre|post> <suspend|hybrid-sleep>
    
    set -e
    
    if [ $2 = "suspend" ] || [ $2 = "hybrid-sleep" ]; then
        case $1 in
            pre)
                true
                ;;
            post)
                # After 0.1 seconds, restart the network manager service
                sleep 0.1 && service network-manager restart
                ;;
        esac
    fi
    
  6. Save wififix by pressing CRTL+X, then ENTER. Then make the file executable with the following command

    sudo chmod +x /lib/systemd/system-sleep/wififix
    
  7. Test the wififix script by entering the command

    sudo /lib/systemd/system-sleep/wififix post suspend
    

    It should hang in the terminal for a split second, then you should see the WiFi status indicator disappear then quickly reappear and connect.

EXPLANATION

Just a disclaimer, I am a PHP developer who casually uses Ubuntu, not a Linux/Ubuntu expert.

The wififix script in /lib/systemd/system-sleep/ is the main fix here. It forces the network manager service to restart within 0.1 seconds after resuming from suspend. This always got me connected faster than when I let the system reconnect/re-enable on its own.

The reason for creating the /etc/NetworkManager/conf.d/default-wifi-powersave-*.conf files, was to make sure that no behavior is employed when entering and exiting suspend (see here). I found that this way the WiFi gets up and running faster after the network manager restarts because it does not try to change any setting on your wireless card or anywhere else (which could cause issues depending on other .conf settings files in /etc/NetworkManager/). Without those scripts, it was still taking like a minute or so to connect to WiFi, but with them it now only takes a matter of seconds and I'm usually connected before I finish entering my password!