libsrtp icon indicating copy to clipboard operation
libsrtp copied to clipboard

bitvector_left_shift profiling

Open mparis opened this issue 10 years ago • 15 comments

Hello, I have done some CPU profiling of my app (that uses libsrtp) since some months, and I have noticed that bitvector_left_shift funtcion [1] worsened a lot few months ago. I am using libsrtp-v1.5.2 and I have done the profiling in these machines:

  1. Linux mparis-XPS-L421X 3.16.0-57-generic #77~14.04.1-Ubuntu SMP Thu Dec 17 23:20:00 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
  2. Linux 528af6a221f7 3.13.0-66-generic #108-Ubuntu SMP Wed Oct 7 15:20:27 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Here the differences in the profiling between dates: 5-oct-2015, 26-nov-2015: 220 cycles/call 18-dec-2015, 29-jan-2016: 8875 cycles/call

This is a huge difference (40000%) that decreases the QoS.

From this data, my question is why the cost of this function has changed if I am using the same version:

  • Does it depends on the data of the RTP packets to encrypt?
  • Does it depend on the linux kernel version (and it has changed)?
  • Does it depend on third party libraries (and they have changed)?

Thanks in advance!!

Refs [1] https://github.com/cisco/libsrtp/blob/v1.5.2/crypto/math/datatypes.c#L485

mparis avatar Jan 29 '16 13:01 mparis

That function is located in datatypes.c, which was last updated 10 months ago in the 1.5.x throttle branch. Therefore, the code hasn't changed. Perhaps the compiler changes between your Oct 5 and Dec 18 builds. Did you check the gcc version used on your systems? This would be the first area I would investigate.

jfigus avatar Feb 02 '16 18:02 jfigus

Good point, but I don't find any change in the gcc version neither in the compile flags :(. In order to find the cause of the CPU consumption increment:

  • What does bitvector_left_shift do?
  • Does the cost of bitvector_left_shift depend on the entry RTP packet data?

PS: I am using libsrtp through GStreamer (srtpdec and srtpenc)

mparis avatar Feb 04 '16 08:02 mparis

This function does a left bit shift on a vector. libsrtp uses this for the anti-replay database. The vector is keeping track of the most recent X packets received, where the vector is X bits long. I wouldn't think individual packet data would cause this function to perform slower. Maybe a lot of out-of-order packets would cause the bit shift to be invoked more???

jfigus avatar Feb 04 '16 14:02 jfigus

Hello again, thanks to your pointer, I have found that my problem is that I have a huge replay_window_size (32767). I will try to reduce the size and verify that does not cause any problem. Anyway, do you have some idea of how implement this function in a more efficient way?

FYI, I have done some profiling with callgrind pushing 1000 RTP packets in order and the results are the following: windows size | ir per call 64 81 128 107 256 151 512 220 1024 356 2048 628 4096 1171 8192 2258 16384 4433 32767 8781

The results do not depend on the RTP packets size.

mparis avatar Feb 05 '16 17:02 mparis

32767 is a huge replay window. Using a 20 ms packetization period, that allows for 655 seconds of replay. Is there any value in this?

jfigus avatar Feb 08 '16 17:02 jfigus

Packets that are so old they are beyond the replay window are rejected (effectively "replayed"). For real-time applications like VoIP, the replay window only needs to be large enough to accommodate the application's jitter buffer, because those are the packets you might want to play. Anything that is too old to play would be discarded by the application, anyway. I would set the size of the replay database to accommodate the size of the jitter buffer (or perhaps a little larger). I think the default is 128, which with audio sent at 50 packet/second (20ms per packet), that would be 2.5 seconds of replay protection. That should be sufficient, as audio played 2.5 seconds later than the arrival time is far too late in any real-time application (especially since the arrival time includes encoding and transport delays).

If the application is sending packets at a substantially higher rate (say, 200pps), then you might need a larger replay protection window (say, 512). If the application is streaming video where you have a higher tolerance for late-arriving packets (i.e., you will still play or record them, even if 5 seconds late), then you might want a slightly larger replay window. Still, it would be far smaller than 32767.

paulej avatar Feb 08 '16 20:02 paulej

You are right. The point is that I am doing packet forwarding with this design: socket_recv -> srtp_unprotect -> ... -> rtxqueue -> srtp_protect -> socket_send

And I realized that with a window size of 512 in srtp_unprotect and srtp_protect, when I had losses (and retransmissions) in the receive side, the srtp_protect sometimes returned err_status_replay_fail. Because of this, I decided to increment the window size to 32767 thinking that it didn't increment the CPU cost so much. Now, that I know that I was wrong, I will think about the window size for srtp_unprotect and srtp_protect. Apparently it should be the next ones, but I have to test it deeply:

  • N for srtp_unprotect (eg: 512)
  • 2xN for srtp_protect (eg: 1024)

Thanks for your comments and suggestions ;).

mparis avatar Feb 09 '16 22:02 mparis

Analysing the problem, the best solution for my use case is disable the replay protection in the srtp_protect. Is it possible?

mparis avatar Feb 14 '16 13:02 mparis

That doesn't sound wise. You'd be introducing a vulnerability. Anti-replay protection is there for a reason. Without this, an attacker could perform a replay attack.

jfigus avatar Feb 15 '16 13:02 jfigus

Yes, but this protection makes sense in the receive side (srtp_unprotect) but not in the send side (srtp_protect), where the application has the control of which RTP packets sends, isn't it?

mparis avatar Feb 15 '16 14:02 mparis

There's a flag allow_repeat_tx on the srtp_policy_t structure. Currently it runs the replay database but ignores its return status for repeat (though not out of window); I suspect however you could probably safely change the code to avoid running the send-side replay protection database when this flag is set, if you’re in a scenario where it genuinely is safe to set allow_repeat_tx.

Additionally, I suspect the implementation of the replay protection database could be made more efficient if it were a circular buffer with a rolling window, rather than doing a left-shift after every packet that’s received. (This isn’t particularly interesting if crypto is in software, since the computational cost of the crypto far outweighs the replay buffer, but in a scenario like this where crypto is offloaded to hardware it might be relevant.)

JonathanLennox avatar Feb 15 '16 16:02 JonathanLennox

OK, I will upload a patch ;).

mparis avatar Feb 17 '16 11:02 mparis

Hi, @mparis are you still considering uploading a patch? I am not sure we would prioritize optimizing the replay window at this point so it would be good to know if this is still a issue for you.

pabuhler avatar Jan 31 '17 13:01 pabuhler

Hello @pabuhler, the roadmap of my company changed and this work was stopped. Sorry because I did not tell you anything :(.

mparis avatar Feb 01 '17 09:02 mparis

ok, closing for now

pabuhler avatar Feb 01 '17 10:02 pabuhler