OPNsense Forum

Archive => 19.1 Legacy Series => Topic started by: 611 on March 16, 2019, 11:23:22 pm

Title: Forcing AES-NI for IPsec and OpenVPN when VPS fails to forward CPU flags
Post by: 611 on March 16, 2019, 11:23:22 pm
Just installed 19.1.4 on Proxmox-based VPS. Looks like the host fails to forward AESNI flag to VM:
Code: [Select]
root@OPNsense:~ # dmesg | grep -i Features2
  Features2=0x80202001<SSE3,CX16,x2APIC,HV>

Still AES-NI works fine in OpenSSL tests if forced (and gives 5x speed improvement):
Code: [Select]
root@OPNsense:~ # openssl speed -elapsed -evp aes-256-gcm
Doing aes-256-gcm for 3s on 16 size blocks: 6438831 aes-256-gcm's in 3.01s
Doing aes-256-gcm for 3s on 64 size blocks: 1945000 aes-256-gcm's in 3.01s
Doing aes-256-gcm for 3s on 256 size blocks: 428613 aes-256-gcm's in 3.01s
Doing aes-256-gcm for 3s on 1024 size blocks: 110367 aes-256-gcm's in 3.00s
Doing aes-256-gcm for 3s on 8192 size blocks: 14538 aes-256-gcm's in 3.00s
OpenSSL 1.0.2o-freebsd  27 Mar 2018
built on: date not available
options:bn(64,64) rc4(8x,char) des(idx,cisc,16,int) aes(partial) idea(int) blowfish(idx)
compiler: clang
The 'numbers' are in 1000s of bytes per second processed.
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
aes-256-gcm      34251.24k    41385.56k    36479.98k    37671.94k    39698.43k

root@OPNsense:~ # env OPENSSL_ia32cap="+0x200000200000000" openssl speed -evp aes-256-gcm
Doing aes-256-gcm for 3s on 16 size blocks: 16130859 aes-256-gcm's in 2.98s
Doing aes-256-gcm for 3s on 64 size blocks: 8240677 aes-256-gcm's in 2.97s
Doing aes-256-gcm for 3s on 256 size blocks: 2304077 aes-256-gcm's in 2.97s
Doing aes-256-gcm for 3s on 1024 size blocks: 690699 aes-256-gcm's in 2.95s
Doing aes-256-gcm for 3s on 8192 size blocks: 77484 aes-256-gcm's in 2.97s
OpenSSL 1.0.2o-freebsd  27 Mar 2018
built on: date not available
options:bn(64,64) rc4(8x,int) des(idx,cisc,16,int) aes(partial) idea(int) blowfish(idx)
compiler: clang
The 'numbers' are in 1000s of bytes per second processed.
type             16 bytes     64 bytes    256 bytes   1024 bytes   8192 bytes
aes-256-gcm      86481.67k   177651.65k   198684.20k   239500.79k   213810.17k

Is there a way to force AES-NI use for both IPsec and OpenVPN?
Title: Re: Forcing AES-NI for IPsec and OpenVPN when VPS fails to forward CPU flags
Post by: weust on March 17, 2019, 06:46:47 am
You say Promox fails to pass AES-NI to the VM, but then mention it is used by OpenSSL.
That sounds like an impossibility to me.

I lack experience here, but how do you know OpenSSL uses a CPU flag that isn't passed to the VM?
Title: Re: Forcing AES-NI for IPsec and OpenVPN when VPS fails to forward CPU flags
Post by: 611 on March 17, 2019, 04:02:15 pm
The host fails to pass CPU feature flag, not the feature itself.

OpenSSL can be forced to believe that AES-NI is present by adding needed flags to capabilities bitmap:
Code: [Select]
env OPENSSL_ia32cap="+0x200000200000000"If AES-NI is actually not supported, this will cause OpenSSL to fail with "invalid opcode" when it would try to use an AES-NI instruction (that's not happening in my case - I see 5x performance boost instead).

I've searched the net and found two solutions that are good for various Linux distros (kernel module that forces the original AESNI flag on, so all the components are sure AES-NI is supported on the system; and this OPENSSL_ia32cap thing, with adding it to global environment), but no solutions for FreeBSD-based systems.

I'm also curious if both OpenVPN and IPsec engine are using OpenSSL on OPNsense, or they would require separate solutions?
Title: Re: Forcing AES-NI for IPsec and OpenVPN when VPS fails to forward CPU flags
Post by: weust on March 18, 2019, 10:46:22 am
Didn't know that. Good to know.

You select which flavour you want on the firmware config page.
Either LibreSSL or OpenSSL.
When you select one, an upgrade is needed that will switch the needed packaged with those compiled with support for the flavour your selected.

So yes, both will use the same.
Title: Re: Forcing AES-NI for IPsec and OpenVPN when VPS fails to forward CPU flags
Post by: 611 on March 18, 2019, 11:11:47 am
Are you sure IPsec on OPNsense uses OpenSSL/LibreSSL?

From what I've found in strongswan docs and forums, strongswan has its own, built-in AES-NI module, but it's only used for IKE - actual traffic encryption/decryption is done in kernel using its AESNI module if loaded.

That means unless it's possible to force original AESNI flag on (like it's possible on Linux - https://www.lowendtalk.com/discussion/132214/force-kernel-aes-ni-usage-on-a-vps-without-the-aes-cpu-flag), strongswan would require its own workaround, and another one for kernel.

Regarding OpenVPN, looks like I need to add the OPENSSL_ia32cap setting to environment where OpenVPN runs.
How can I add it? I've tried to add it to default section of login.conf (and rebuild the database), but that doesn't seem to help.
Title: Re: Forcing AES-NI for IPsec and OpenVPN when VPS fails to forward CPU flags
Post by: weust on March 18, 2019, 11:13:30 am
I was only mentioning packages, not how what they do inside.

I think one of the developers can better answer you.