Skip to contentSkip to conversation

Question about add AF_ALG backend for virtio-crypto

Qemu-devel Longpeng (Mike) 4 participants 15 messages Jan 9, 2017 – Feb 9, 2017

lore
Share & tools
lore
#0 lore raw

Hi guys,

I'm one of Gonglei's virtio-crypto project members, and we plan to add a AF_ALG backend for virtio-crypto(there's only builtin-backend currently).

I found that Catalin, Paolo and Stefan had discussed about this in 2015
(http://www.spinics.net/lists/kvm/msg115457.html), but it seems that Catalin didn't do it, so I'm confuse about wether it is need to add a AF_ALG backend.

Do you have any suggestion? Thanks :)

Signature · 2 lines
Regards,
Longpeng(Mike)
#1 lore raw
Quoted 8 lines · Longpeng (Mike)

I'm one of Gonglei's virtio-crypto project members, and we plan to add a AF_ALG backend for virtio-crypto(there's only builtin-backend currently).

I found that Catalin, Paolo and Stefan had discussed about this in 2015
(http://www.spinics.net/lists/kvm/msg115457.html), but it seems that Catalin didn't do it, so I'm confuse about wether it is need to add a AF_ALG backend.

Do you have any suggestion? Thanks :)

I have no objections to an AF_ALG backend in QEMU.

Stefan

#2 lore raw
Quoted 11 lines · Stefan Hajnoczi
Quoted 8 lines · Longpeng (Mike)

I'm one of Gonglei's virtio-crypto project members, and we plan to add a AF_ALG backend for virtio-crypto(there's only builtin-backend currently).

I found that Catalin, Paolo and Stefan had discussed about this in 2015
(http://www.spinics.net/lists/kvm/msg115457.html), but it seems that Catalin didn't do it, so I'm confuse about wether it is need to add a AF_ALG backend.

Do you have any suggestion? Thanks :)

I have no objections to an AF_ALG backend in QEMU.

Rather than do another backend for virtio-crypto, IMHO, we should have an AF_ALG impl of the crypto/ APIs. That way any potential performance benefits will enhance our LUKS encryption code too.

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#3 lore raw

Hi,

Quoted 13 lines · Stefan Hajnoczi
Quoted 2 lines · Longpeng (Mike)
Quoted 1 line

I'm one of Gonglei's virtio-crypto project members, and we plan to add a

AF_ALG

Quoted 4 lines
Quoted 4 lines

backend for virtio-crypto(there's only builtin-backend currently).

I found that Catalin, Paolo and Stefan had discussed about this in 2015
(http://www.spinics.net/lists/kvm/msg115457.html), but it seems that

Catalin

Quoted 1 line
Quoted 1 line

didn't do it, so I'm confuse about wether it is need to add a AF_ALG

backend.

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Do you have any suggestion? Thanks :)

I have no objections to an AF_ALG backend in QEMU.

Rather than do another backend for virtio-crypto, IMHO, we should have an AF_ALG impl of the crypto/ APIs. That way any potential performance benefits will enhance our LUKS encryption code too.

According to the currently schemas of crypto/ APIs, we can't choose the specific backend dynamically. This is a limitation for virtio-crypto device I think.

If an AF_ALG backend is supported, we can directly use the hardware accelerators based on LKCF, which makes many sense.

Regards,
-Gonglei

#4 lore raw
Quoted 19 lines · Gonglei (Arei)

Hi,

Quoted 17 lines · Stefan Hajnoczi
Quoted 2 lines · Longpeng (Mike)
Quoted 1 line

I'm one of Gonglei's virtio-crypto project members, and we plan to add a

AF_ALG

Quoted 4 lines
Quoted 4 lines

backend for virtio-crypto(there's only builtin-backend currently).

I found that Catalin, Paolo and Stefan had discussed about this in 2015
(http://www.spinics.net/lists/kvm/msg115457.html), but it seems that

Catalin

Quoted 1 line
Quoted 1 line

didn't do it, so I'm confuse about wether it is need to add a AF_ALG

backend.

Quoted 4 lines
Quoted 2 lines

Do you have any suggestion? Thanks :)

I have no objections to an AF_ALG backend in QEMU.

Quoted 7 lines
Quoted 4 lines

Rather than do another backend for virtio-crypto, IMHO, we should have an AF_ALG impl of the crypto/ APIs. That way any potential performance benefits will enhance our LUKS encryption code too.

According to the currently schemas of crypto/ APIs, we can't choose the specific backend dynamically. This is a limitation for virtio-crypto device I think.

Do we really need to be able to choose the backend explicitly. If the AF_ALG backend is faster, why would you simply not use that automatically if it is available. I'm not seeing an obvious need for dynamically choosing between backends.

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#5 lore raw

Hi Daniel,

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines · Stefan Hajnoczi
Quoted 2 lines · Longpeng (Mike)
Quoted 1 line

I'm one of Gonglei's virtio-crypto project members, and we plan to add a

AF_ALG

Quoted 4 lines
Quoted 4 lines

backend for virtio-crypto(there's only builtin-backend currently).

I found that Catalin, Paolo and Stefan had discussed about this in 2015
(http://www.spinics.net/lists/kvm/msg115457.html), but it seems that

Catalin

Quoted 1 line
Quoted 1 line

didn't do it, so I'm confuse about wether it is need to add a AF_ALG

backend.

Quoted 4 lines
Quoted 2 lines

Do you have any suggestion? Thanks :)

I have no objections to an AF_ALG backend in QEMU.

Rather than do another backend for virtio-crypto, IMHO, we should have an AF_ALG impl of the crypto/ APIs. That way any potential performance benefits will enhance our LUKS encryption code too.

Quoted 7 lines
Quoted 3 lines

According to the currently schemas of crypto/ APIs, we can't choose the specific backend dynamically. This is a limitation for virtio-crypto device I think.

Do we really need to be able to choose the backend explicitly. If the AF_ALG backend is faster, why would you simply not use that automatically if it is available.

Can we realize the purpose based on the crypto/ APIs? IIUC the crypto subsystem chooses a backend during the building according to the specific priority, nettle > gcrypt > cipher-builtin.

If we add an AF_ALG implementation for crypto subsystem, shall we set it as the highest priority? If so, other backends won't be used since AF_ALG is always available. If not, how can we use AF_ALG backend for crypto/ API?

Please correct me if I'm missing something.

Quoted 3 lines

I'm not seeing an obvious need for dynamically choosing between backends.

Thanks,
-Gonglei

#6 lore raw
Quoted 37 lines · Gonglei (Arei)

Hi Daniel,

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines · Stefan Hajnoczi
Quoted 2 lines · Longpeng (Mike)
Quoted 1 line

I'm one of Gonglei's virtio-crypto project members, and we plan to add a

AF_ALG

Quoted 4 lines
Quoted 4 lines

backend for virtio-crypto(there's only builtin-backend currently).

I found that Catalin, Paolo and Stefan had discussed about this in 2015
(http://www.spinics.net/lists/kvm/msg115457.html), but it seems that

Catalin

Quoted 1 line
Quoted 1 line

didn't do it, so I'm confuse about wether it is need to add a AF_ALG

backend.

Quoted 4 lines
Quoted 2 lines

Do you have any suggestion? Thanks :)

I have no objections to an AF_ALG backend in QEMU.

Rather than do another backend for virtio-crypto, IMHO, we should have an AF_ALG impl of the crypto/ APIs. That way any potential performance benefits will enhance our LUKS encryption code too.

Quoted 7 lines
Quoted 3 lines

According to the currently schemas of crypto/ APIs, we can't choose the specific backend dynamically. This is a limitation for virtio-crypto device I think.

Do we really need to be able to choose the backend explicitly. If the AF_ALG backend is faster, why would you simply not use that automatically if it is available.

Can we realize the purpose based on the crypto/ APIs? IIUC the crypto subsystem chooses a backend during the building according to the specific priority, nettle > gcrypt > cipher-builtin.

Quoted 5 lines

If we add an AF_ALG implementation for crypto subsystem, shall we set it as the highest priority? If so, other backends won't be used since AF_ALG is always available. If not, how can we use AF_ALG backend for crypto/ API?

Please correct me if I'm missing something.

While AF_ALG has been available for a while, not all features have. For example AEAD support was only added in kernel 4.1

So if we had AF_ALG in QEMU, we would have to have a stacked impl, where we try AF_ALG and then fallback to the current code when QEMU runs on a kernel lacking the feature needed.

We could potentially also have a global arg to switch backends e.g.

-crypto-backend [afalg|builtin]

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#7 lore raw
Quoted 40 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Stefan Hajnoczi
Quoted 2 lines · Longpeng (Mike)
Quoted 1 line

I'm one of Gonglei's virtio-crypto project members, and we plan to

add a

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

AF_ALG

Quoted 3 lines
Quoted 3 lines

backend for virtio-crypto(there's only builtin-backend currently).

I found that Catalin, Paolo and Stefan had discussed about this in

2015

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(http://www.spinics.net/lists/kvm/msg115457.html), but it seems

that

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 4 lines

Catalin

Quoted 1 line
Quoted 1 line

didn't do it, so I'm confuse about wether it is need to add a AF_ALG

backend.

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Do you have any suggestion? Thanks :)

I have no objections to an AF_ALG backend in QEMU.

Rather than do another backend for virtio-crypto, IMHO, we should have an AF_ALG impl of the crypto/ APIs. That way any potential performance benefits will enhance our LUKS encryption code too.

According to the currently schemas of crypto/ APIs, we can't choose the specific backend dynamically. This is a limitation for virtio-crypto device I think.

Quoted 6 lines
Quoted 3 lines

Do we really need to be able to choose the backend explicitly. If the AF_ALG backend is faster, why would you simply not use that automatically if it is available.

Can we realize the purpose based on the crypto/ APIs? IIUC the crypto subsystem chooses a backend during the building according to the specific

priority,

Quoted 2 lines

nettle > gcrypt > cipher-builtin.

Quoted 9 lines
Quoted 5 lines

If we add an AF_ALG implementation for crypto subsystem, shall we set it as the highest priority? If so, other backends won't be used since AF_ALG is always available. If not, how can we use AF_ALG backend for crypto/ API?

Please correct me if I'm missing something.

While AF_ALG has been available for a while, not all features have. For example AEAD support was only added in kernel 4.1

OK.

Quoted 4 lines

So if we had AF_ALG in QEMU, we would have to have a stacked impl, where we try AF_ALG and then fallback to the current code when QEMU runs on a kernel lacking the feature needed.

It makes sense though the implementation will be more complicate since the code should identify the different error codes are returned by AF_ALG APIs.

Quoted 4 lines

We could potentially also have a global arg to switch backends e.g.

-crypto-backend [afalg|builtin]

So the backend here is not the cryptodev backend?

Thanks,
-Gonglei

#8 lore raw
Quoted 59 lines · Gonglei (Arei)
Quoted 40 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Stefan Hajnoczi
Quoted 2 lines · Longpeng (Mike)
Quoted 1 line

I'm one of Gonglei's virtio-crypto project members, and we plan to

add a

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

AF_ALG

Quoted 3 lines
Quoted 3 lines

backend for virtio-crypto(there's only builtin-backend currently).

I found that Catalin, Paolo and Stefan had discussed about this in

2015

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(http://www.spinics.net/lists/kvm/msg115457.html), but it seems

that

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 4 lines

Catalin

Quoted 1 line
Quoted 1 line

didn't do it, so I'm confuse about wether it is need to add a AF_ALG

backend.

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Do you have any suggestion? Thanks :)

I have no objections to an AF_ALG backend in QEMU.

Rather than do another backend for virtio-crypto, IMHO, we should have an AF_ALG impl of the crypto/ APIs. That way any potential performance benefits will enhance our LUKS encryption code too.

According to the currently schemas of crypto/ APIs, we can't choose the specific backend dynamically. This is a limitation for virtio-crypto device I think.

Quoted 6 lines
Quoted 3 lines

Do we really need to be able to choose the backend explicitly. If the AF_ALG backend is faster, why would you simply not use that automatically if it is available.

Can we realize the purpose based on the crypto/ APIs? IIUC the crypto subsystem chooses a backend during the building according to the specific

priority,

Quoted 2 lines

nettle > gcrypt > cipher-builtin.

Quoted 9 lines
Quoted 5 lines

If we add an AF_ALG implementation for crypto subsystem, shall we set it as the highest priority? If so, other backends won't be used since AF_ALG is always available. If not, how can we use AF_ALG backend for crypto/ API?

Please correct me if I'm missing something.

While AF_ALG has been available for a while, not all features have. For example AEAD support was only added in kernel 4.1

OK.

Quoted 4 lines

So if we had AF_ALG in QEMU, we would have to have a stacked impl, where we try AF_ALG and then fallback to the current code when QEMU runs on a kernel lacking the feature needed.

It makes sense though the implementation will be more complicate since the code should identify the different error codes are returned by AF_ALG APIs.

Quoted 5 lines
Quoted 4 lines

We could potentially also have a global arg to switch backends e.g.

-crypto-backend [afalg|builtin]

So the backend here is not the cryptodev backend?

No, it would apply to the crypto/ APIs, so it affects all users of those APIs not just cryptodev

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#9 lore raw

Hi Daniel,

I was writing AF_ALG-backed for QEMU crypto these days, I think there're more than two ways to implements it.

The first one look likes below:
[ cipher.c ]
qcrypto_cipher_new(...)

{
	if (...) { /* use AF_ALG */
		cipher = afalg_cipher_new(...)
		if (cipher) {
			return cipher;
		}
	}
	
	/* disabled AF_ALG or AF_ALG failed, then back to
	 * using 'builtin'(gcrypt/nettle/...)
	 */
	cipher = __qcrypto_cipher_new(...)
}

[ cipher-afalg.c ]
afalg_cipher_new(...) {....}
afalg_cipher_encrypt(...) {...}
......

[ cipher-gcrypt.c ]
__qcrypto_cipher_new(...) {...}
__qcrypto_cipher_encrypt(...) {...}
......

[ cipher-nettle.c ]
__qcrypto_cipher_new(...) {...}
__qcrypto_cipher_encrypt(...) {...}
......

In this way, I think I need to rename most functions in cipher-gcrypt.c/cipher-nettle.c with a prefixion(such as '__')

Alternative way is:
[ cipher-afalg.c ]
afalg_cipher_new(...) {....}
afalg_cipher_encrypt(...) {...}
......

[ cipher-gcrypt.c ]
qcrypto_cipher_new(...)

{
	if (...) { /* use AF_ALG */
		cipher = afalg_cipher_new(...)
		if (cipher) {
			return cipher;
		}
	}
	
	/* disabled AF_ALG or AF_ALG failed, then back to
	 * using 'builtin'
	 */
	.......( the existing code )
}

......

[ cipher-nettle.c ]
qcrypto_cipher_new(...)

{
	if (...) { /* use AF_ALG */
		cipher = afalg_cipher_new(...)
		if (cipher) {
			return cipher;
		}
	}
	
	/* disabled AF_ALG or AF_ALG failed, then back to
	 * using 'builtin'
	 */
	.......( the existing code )
}

......

In this way, we should add AF_ALG-backed code in most functions in cipher-gcrypt.c/cipher-nettle.c, I'm afraid this would introduce lots of duplicate code because the same AF_ALG-backed code must in both gcrypt-backed impls and nettle-backed impls as above.

I'm confusing about which way you'd prefer, or do you have any better suggestion?

Thanks!

Signature · 2 lines
Regards,
Longpeng(Mike)
Quoted 72 lines · Daniel P. Berrange
Quoted 59 lines · Gonglei (Arei)
Quoted 40 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Stefan Hajnoczi
Quoted 2 lines · Longpeng (Mike)
Quoted 1 line

I'm one of Gonglei's virtio-crypto project members, and we plan to

add a

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

AF_ALG

Quoted 3 lines
Quoted 3 lines

backend for virtio-crypto(there's only builtin-backend currently).

I found that Catalin, Paolo and Stefan had discussed about this in

2015

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(http://www.spinics.net/lists/kvm/msg115457.html), but it seems

that

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 4 lines

Catalin

Quoted 1 line
Quoted 1 line

didn't do it, so I'm confuse about wether it is need to add a AF_ALG

backend.

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Do you have any suggestion? Thanks :)

I have no objections to an AF_ALG backend in QEMU.

Rather than do another backend for virtio-crypto, IMHO, we should have an AF_ALG impl of the crypto/ APIs. That way any potential performance benefits will enhance our LUKS encryption code too.

According to the currently schemas of crypto/ APIs, we can't choose the specific backend dynamically. This is a limitation for virtio-crypto device I think.

Quoted 6 lines
Quoted 3 lines

Do we really need to be able to choose the backend explicitly. If the AF_ALG backend is faster, why would you simply not use that automatically if it is available.

Can we realize the purpose based on the crypto/ APIs? IIUC the crypto subsystem chooses a backend during the building according to the specific

priority,

Quoted 2 lines

nettle > gcrypt > cipher-builtin.

Quoted 9 lines
Quoted 5 lines

If we add an AF_ALG implementation for crypto subsystem, shall we set it as the highest priority? If so, other backends won't be used since AF_ALG is always available. If not, how can we use AF_ALG backend for crypto/ API?

Please correct me if I'm missing something.

While AF_ALG has been available for a while, not all features have. For example AEAD support was only added in kernel 4.1

OK.

Quoted 4 lines

So if we had AF_ALG in QEMU, we would have to have a stacked impl, where we try AF_ALG and then fallback to the current code when QEMU runs on a kernel lacking the feature needed.

It makes sense though the implementation will be more complicate since the code should identify the different error codes are returned by AF_ALG APIs.

Quoted 5 lines
Quoted 4 lines

We could potentially also have a global arg to switch backends e.g.

-crypto-backend [afalg|builtin]

So the backend here is not the cryptodev backend?

No, it would apply to the crypto/ APIs, so it affects all users of those APIs not just cryptodev

Regards,
Daniel

Signature · 2 lines
Regards,
Longpeng(Mike)
#10 lore raw
Quoted 81 lines · Longpeng (Mike)

Hi Daniel,

I was writing AF_ALG-backed for QEMU crypto these days, I think there're more than two ways to implements it.

The first one look likes below:
[ cipher.c ]
qcrypto_cipher_new(...)

{
	if (...) { /* use AF_ALG */
		cipher = afalg_cipher_new(...)
		if (cipher) {
			return cipher;
		}
	}
	
	/* disabled AF_ALG or AF_ALG failed, then back to
	 * using 'builtin'(gcrypt/nettle/...)
	 */
	cipher = __qcrypto_cipher_new(...)
}

[ cipher-afalg.c ]
afalg_cipher_new(...) {....}
afalg_cipher_encrypt(...) {...}
......

[ cipher-gcrypt.c ]
__qcrypto_cipher_new(...) {...}
__qcrypto_cipher_encrypt(...) {...}
......

[ cipher-nettle.c ]
__qcrypto_cipher_new(...) {...}
__qcrypto_cipher_encrypt(...) {...}
......

In this way, I think I need to rename most functions in cipher-gcrypt.c/cipher-nettle.c with a prefixion(such as '__')

Alternative way is:
[ cipher-afalg.c ]
afalg_cipher_new(...) {....}
afalg_cipher_encrypt(...) {...}
......

[ cipher-gcrypt.c ]
qcrypto_cipher_new(...)

{
	if (...) { /* use AF_ALG */
		cipher = afalg_cipher_new(...)
		if (cipher) {
			return cipher;
		}
	}
	
	/* disabled AF_ALG or AF_ALG failed, then back to
	 * using 'builtin'
	 */
	.......( the existing code )
}

......

[ cipher-nettle.c ]
qcrypto_cipher_new(...)

{
	if (...) { /* use AF_ALG */
		cipher = afalg_cipher_new(...)
		if (cipher) {
			return cipher;
		}
	}
	
	/* disabled AF_ALG or AF_ALG failed, then back to
	 * using 'builtin'
	 */
	.......( the existing code )
}

......

Quoted 7 lines

In this way, we should add AF_ALG-backed code in most functions in cipher-gcrypt.c/cipher-nettle.c, I'm afraid this would introduce lots of duplicate code because the same AF_ALG-backed code must in both gcrypt-backed impls and nettle-backed impls as above.

I'm confusing about which way you'd prefer, or do you have any better suggestion?

Yeah, both approaches have some reasonably significant downsides. Approach 1 is sort of like providing a virtual driver table, except it is hardcoded to switch between 2 impls only.

A variant on approach 1 is to actually setup a proper driver-table dispatch layer. eg define a struct that contains callbacks for each public api operation. The qcrypto_cipher_new() method will then either setup callbacks for AF_ALG, or for the library impl.

This is the design we took in crypto/{ivgen.c,ivgenpriv.h}

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#11 lore raw

Hi Daniel,

Quoted 50 lines · Daniel P. Berrange
Quoted 37 lines · Longpeng (Mike)

Hi Daniel,

I was writing AF_ALG-backed for QEMU crypto these days, I think there're more than two ways to implements it.

The first one look likes below:
[ cipher.c ]
qcrypto_cipher_new(...)

{
	if (...) { /* use AF_ALG */
		cipher = afalg_cipher_new(...)
		if (cipher) {
			return cipher;
		}
	}
	
	/* disabled AF_ALG or AF_ALG failed, then back to
	 * using 'builtin'(gcrypt/nettle/...)
	 */
	cipher = __qcrypto_cipher_new(...)
}

[ cipher-afalg.c ]
afalg_cipher_new(...) {....}
afalg_cipher_encrypt(...) {...}
......

[ cipher-gcrypt.c ]
__qcrypto_cipher_new(...) {...}
__qcrypto_cipher_encrypt(...) {...}
......

[ cipher-nettle.c ]
__qcrypto_cipher_new(...) {...}
__qcrypto_cipher_encrypt(...) {...}
......

Quoted 7 lines

In this way, I think I need to rename most functions in cipher-gcrypt.c/cipher-nettle.c with a prefixion(such as '__')

I'm confusing about which way you'd prefer, or do you have any better suggestion?

Yeah, both approaches have some reasonably significant downsides. Approach 1 is sort of like providing a virtual driver table, except it is hardcoded to switch between 2 impls only.

Quoted 7 lines

A variant on approach 1 is to actually setup a proper driver-table dispatch layer. eg define a struct that contains callbacks for each public api operation. The qcrypto_cipher_new() method will then either setup callbacks for AF_ALG, or for the library impl.

This is the design we took in crypto/{ivgen.c,ivgenpriv.h}

So...you prefer approach 1 with a driver-table dispatch layer, right? And this implies that we must either rename some public methods in cipher-gcrypt.c/cipher-nettle.c, or change them to 'static'.

I also have some other ideas:

1) *using bitmap to improve performance*

As you suggested before:
"if we had AF_ALG in QEMU, we would have to have a stacked impl, where we try AF_ALG and then fallback to the current code when QEMU runs on a kernel lacking the feature needed."

I think it would impact the performance if we "try AF_ALG and then fallback to library" each time, so we can use a bitmap to indicate whether the @alg is supported by AF_ALG.

2) *maybe we need a heuristic policy*

I added some speed test in test-crypto-cipher/hash and found that for big packets AF_ALG is much faster than library-impl while library-impl is better when the packets is small:

packet(bytes) AF_ALG(MB/sec, intel QAT) Library-impl(MB/sec)
512 53.68 127.82
1024 98.39 133.21
2048 167.56 134.62
4096 276.21 135.10
8192 410.80 135.82
16384 545.08 136.01
32768 654.49 136.30
65536 723.00 136.29

If a @alg is both supported by AF_ALG and library-impl, I think we should decide to use which one dynamically.

Quoted 3 lines

Regards,
Daniel

Signature · 2 lines
Regards,
Longpeng(Mike)
#12 lore raw
Quoted 59 lines · Longpeng (Mike)

Hi Daniel,

Quoted 55 lines · Daniel P. Berrange
Quoted 37 lines · Longpeng (Mike)

Hi Daniel,

I was writing AF_ALG-backed for QEMU crypto these days, I think there're more than two ways to implements it.

The first one look likes below:
[ cipher.c ]
qcrypto_cipher_new(...)

{
	if (...) { /* use AF_ALG */
		cipher = afalg_cipher_new(...)
		if (cipher) {
			return cipher;
		}
	}
	
	/* disabled AF_ALG or AF_ALG failed, then back to
	 * using 'builtin'(gcrypt/nettle/...)
	 */
	cipher = __qcrypto_cipher_new(...)
}

[ cipher-afalg.c ]
afalg_cipher_new(...) {....}
afalg_cipher_encrypt(...) {...}
......

[ cipher-gcrypt.c ]
__qcrypto_cipher_new(...) {...}
__qcrypto_cipher_encrypt(...) {...}
......

[ cipher-nettle.c ]
__qcrypto_cipher_new(...) {...}
__qcrypto_cipher_encrypt(...) {...}
......

Quoted 7 lines

In this way, I think I need to rename most functions in cipher-gcrypt.c/cipher-nettle.c with a prefixion(such as '__')

I'm confusing about which way you'd prefer, or do you have any better suggestion?

Yeah, both approaches have some reasonably significant downsides. Approach 1 is sort of like providing a virtual driver table, except it is hardcoded to switch between 2 impls only.

A variant on approach 1 is to actually setup a proper driver-table dispatch layer. eg define a struct that contains callbacks for each public api operation. The qcrypto_cipher_new() method will then either setup callbacks for AF_ALG, or for the library impl.

Quoted 7 lines
Quoted 2 lines

This is the design we took in crypto/{ivgen.c,ivgenpriv.h}

So...you prefer approach 1 with a driver-table dispatch layer, right? And this implies that we must either rename some public methods in cipher-gcrypt.c/cipher-nettle.c, or change them to 'static'.

I'd suggest both - renaming them to have 'gcrypt' or 'nettle' in their name, and also make them static.

Quoted 4 lines

I also have some other ideas:

1) *using bitmap to improve performance*

Quoted 8 lines

As you suggested before:
"if we had AF_ALG in QEMU, we would have to have a stacked impl, where we try AF_ALG and then fallback to the current code when QEMU runs on a kernel lacking the feature needed."

I think it would impact the performance if we "try AF_ALG and then fallback to library" each time, so we can use a bitmap to indicate whether the @alg is supported by AF_ALG.

Yep, remembering the decision makes total sense if that is a high overhead decision.

Quoted 16 lines

2) *maybe we need a heuristic policy*

I added some speed test in test-crypto-cipher/hash and found that for big packets AF_ALG is much faster than library-impl while library-impl is better when the packets is small:

packet(bytes) AF_ALG(MB/sec, intel QAT) Library-impl(MB/sec)
512 53.68 127.82
1024 98.39 133.21
2048 167.56 134.62
4096 276.21 135.10
8192 410.80 135.82
16384 545.08 136.01
32768 654.49 136.30
65536 723.00 136.29

Quoted 2 lines

If a @alg is both supported by AF_ALG and library-impl, I think we should decide to use which one dynamically.

What exactly are you measuring here?

Is this comparing encryption of a fixed total size of data, and varying the packet size. ie sending 1024 * 512 byte packets against 256 * 2048 byte packages.

Or is it sending a constant number of packets eg 1024 * 512 byte packets against 1024 * 2048 byte packets ?

The problem is that when constructing the cipher initially, we have no about the intended usage pattern, so can't decide which impl to use as is

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|
#13 lore raw

Hi Daniel,

Quoted 5 lines · Daniel P. Berrange
Quoted 4 lines · Longpeng (Mike)

Hi Daniel,

Quoted 7 lines
Quoted 3 lines

So...you prefer approach 1 with a driver-table dispatch layer, right? And this implies that we must either rename some public methods in cipher-gcrypt.c/cipher-nettle.c, or change them to 'static'.

I'd suggest both - renaming them to have 'gcrypt' or 'nettle' in their name, and also make them static.

OK.

Quoted 22 lines
Quoted 19 lines

I also have some other ideas:

2) *maybe we need a heuristic policy*

I added some speed test in test-crypto-cipher/hash and found that for big packets AF_ALG is much faster than library-impl while library-impl is better when the packets is small:

packet(bytes) AF_ALG(MB/sec, intel QAT) Library-impl(MB/sec)
512 53.68 127.82
1024 98.39 133.21
2048 167.56 134.62
4096 276.21 135.10
8192 410.80 135.82
16384 545.08 136.01
32768 654.49 136.30
65536 723.00 136.29

Quoted 2 lines

If a @alg is both supported by AF_ALG and library-impl, I think we should decide to use which one dynamically.

Quoted 9 lines

What exactly are you measuring here?

Is this comparing encryption of a fixed total size of data, and varying the packet size. ie sending 1024 * 512 byte packets against 256 * 2048 byte packages.

Or is it sending a constant number of packets eg 1024 * 512 byte packets against 1024 * 2048 byte packets ?

The testcase encrypts data for 5 seconds and then calculates how many MBs it can encrypt per second, as below:

    g_test_timer_start();
    do {
        g_assert(qcrypto_cipher_setiv(cipher,
                                      iv, niv,
                                      &err) == 0);

        g_assert(qcrypto_cipher_encrypt(cipher,
                                        plaintext,
                                        ciphertext,
                                        chunk_size,
                                        &err) == 0);
        total += chunk_size;
    } while (g_test_timer_elapsed() < 5.0);

    total /= 1024 * 1024; /* to MB */
    g_print("Testing cbc(aes128): ");
    g_print("Encrypting in chunks of %ld bytes: ", chunk_size);
    g_print("done. %.2f MB in %.2f secs: ", total, g_test_timer_last());
    g_print("%.2f MB/sec\t", total / g_test_timer_last());

chunk_size = 512/1024/2048/.../65536 bytes.

Some other projects (ie. cryptodev-linux, libkcapi) also use this way to test speed.

Quoted 4 lines

The problem is that when constructing the cipher initially, we have no about the intended usage pattern, so can't decide which impl to use as is

Yep, I wanted to allocate both af_alg and library-impl objects and then we can choose dynamically before, but now it seems too stupid. :)

Thanks.

Quoted 3 lines

Regards,
Daniel

Signature · 2 lines
Regards,
Longpeng(Mike)
#14 lore raw
Quoted 76 lines · Longpeng (Mike)

Hi Daniel,

Quoted 5 lines · Daniel P. Berrange
Quoted 4 lines · Longpeng (Mike)

Hi Daniel,

Quoted 7 lines
Quoted 3 lines

So...you prefer approach 1 with a driver-table dispatch layer, right? And this implies that we must either rename some public methods in cipher-gcrypt.c/cipher-nettle.c, or change them to 'static'.

I'd suggest both - renaming them to have 'gcrypt' or 'nettle' in their name, and also make them static.

OK.

Quoted 22 lines
Quoted 19 lines

I also have some other ideas:

2) *maybe we need a heuristic policy*

I added some speed test in test-crypto-cipher/hash and found that for big packets AF_ALG is much faster than library-impl while library-impl is better when the packets is small:

packet(bytes) AF_ALG(MB/sec, intel QAT) Library-impl(MB/sec)
512 53.68 127.82
1024 98.39 133.21
2048 167.56 134.62
4096 276.21 135.10
8192 410.80 135.82
16384 545.08 136.01
32768 654.49 136.30
65536 723.00 136.29

Quoted 2 lines

If a @alg is both supported by AF_ALG and library-impl, I think we should decide to use which one dynamically.

Quoted 9 lines

What exactly are you measuring here?

Is this comparing encryption of a fixed total size of data, and varying the packet size. ie sending 1024 * 512 byte packets against 256 * 2048 byte packages.

Or is it sending a constant number of packets eg 1024 * 512 byte packets against 1024 * 2048 byte packets ?

The testcase encrypts data for 5 seconds and then calculates how many MBs it can encrypt per second, as below:

    g_test_timer_start();
    do {
        g_assert(qcrypto_cipher_setiv(cipher,
                                      iv, niv,
                                      &err) == 0);

        g_assert(qcrypto_cipher_encrypt(cipher,
                                        plaintext,
                                        ciphertext,
                                        chunk_size,
                                        &err) == 0);
        total += chunk_size;
    } while (g_test_timer_elapsed() < 5.0);

    total /= 1024 * 1024; /* to MB */
    g_print("Testing cbc(aes128): ");
    g_print("Encrypting in chunks of %ld bytes: ", chunk_size);
    g_print("done. %.2f MB in %.2f secs: ", total, g_test_timer_last());
    g_print("%.2f MB/sec\t", total / g_test_timer_last());
Quoted 3 lines

chunk_size = 512/1024/2048/.../65536 bytes.

Some other projects (ie. cryptodev-linux, libkcapi) also use this way to test speed.

I'd be interested to know if there's any different in the results if you put the qcrypto_cipher_setiv() call outside of the loop.

Depending on the way the API is used, you might set an IV, and then write many chunks of data before setting the next IV.

Of course for LUKS, we set a new IV every 512 bytes of data, since IVs are tied to disk sectors, so we'd be hitting the small chunk size code path.

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|