tokio-websockets 0.12.0

High performance, strict, tokio-util based WebSockets implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! This module contains six implementations of WebSocket frame masking and
//! unmasking, all of them using the same algorithm and methods:
//!   - One AVX512-based implementation that masks 64 bytes per cycle (requires
//!     nightly rust)
//!   - One AVX2-based implementation that masks 32 bytes per cycle
//!   - One SSE2-based implementation that masks 16 bytes per cycle
//!   - One NEON-based implementation that masks 16 bytes per cycle (requires
//!     nightly rust on 32-bit ARM)
//!   - One AltiVec-based implementation that masks 16 bytes per cycle (requires
//!     nightly rust)
//!   - One IBM z13 vector facility based implementation that masks 16 bytes per
//!     cycle (requires nightly rust)
//!   - One LASX based implementation that masks 32 bytes per cycle (requires
//!     nightly rust)
//!   - A fallback implementation that masks 8 bytes per cycle
//!
//! The SIMD implementations will be used if CPU support for them is detected at
//! runtime or enabled at compile time via compiler flags.

/// (Un-)masks input bytes with the framing key using AVX512.
///
/// This will use a fallback implementation for less than 64 bytes. For
/// sufficiently large inputs, it masks in chunks of 64 bytes per
/// instruction, applying the fallback method on all remaining data.
#[cfg(all(feature = "nightly", any(target_arch = "x86", target_arch = "x86_64")))]
#[allow(clippy::incompatible_msrv)] // nightly feature gated, stable since 1.89.0
#[target_feature(enable = "avx512f")]
unsafe fn frame_avx512(key: &mut [u8; 4], input: &mut [u8]) {
    #[cfg(target_arch = "x86")]
    use std::arch::x86::{__m512i, _mm512_set1_epi32, _mm512_xor_si512};
    #[cfg(target_arch = "x86_64")]
    use std::arch::x86_64::{__m512i, _mm512_set1_epi32, _mm512_xor_si512};

    unsafe {
        let (prefix, aligned_data, suffix) = input.align_to_mut::<__m512i>();

        // Run fallback implementation on unaligned prefix data
        if !prefix.is_empty() {
            fallback_frame(key, prefix);
        }

        if !aligned_data.is_empty() {
            let mask = _mm512_set1_epi32(i32::from_ne_bytes(*key));

            for block in aligned_data {
                *block = _mm512_xor_si512(*block, mask);
            }
        }

        // Run fallback implementation on unaligned suffix data
        if !suffix.is_empty() {
            fallback_frame(key, suffix);
        }
    }
}

/// (Un-)masks input bytes with the framing key using AVX2.
///
/// This will use a fallback implementation for less than 32 bytes. For
/// sufficiently large inputs, it masks in chunks of 32 bytes per
/// instruction, applying the fallback method on all remaining data.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
unsafe fn frame_avx2(key: &mut [u8; 4], input: &mut [u8]) {
    #[cfg(target_arch = "x86")]
    use std::arch::x86::{__m256i, _mm256_set1_epi32, _mm256_xor_si256};
    #[cfg(target_arch = "x86_64")]
    use std::arch::x86_64::{__m256i, _mm256_set1_epi32, _mm256_xor_si256};

    unsafe {
        let (prefix, aligned_data, suffix) = input.align_to_mut::<__m256i>();

        // Run fallback implementation on unaligned prefix data
        if !prefix.is_empty() {
            fallback_frame(key, prefix);
        }

        if !aligned_data.is_empty() {
            let mask = _mm256_set1_epi32(i32::from_ne_bytes(*key));

            for block in aligned_data {
                *block = _mm256_xor_si256(*block, mask);
            }
        }

        // Run fallback implementation on unaligned suffix data
        if !suffix.is_empty() {
            fallback_frame(key, suffix);
        }
    }
}

/// (Un-)masks input bytes with the framing key using SSE2.
///
/// This will use a fallback implementation for less than 16 bytes. For
/// sufficiently large inputs, it masks in chunks of 16 bytes per
/// instruction, applying the fallback method on all remaining data.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "sse2")]
unsafe fn frame_sse2(key: &mut [u8; 4], input: &mut [u8]) {
    #[cfg(target_arch = "x86")]
    use std::arch::x86::{__m128i, _mm_set1_epi32, _mm_xor_si128};
    #[cfg(target_arch = "x86_64")]
    use std::arch::x86_64::{__m128i, _mm_set1_epi32, _mm_xor_si128};

    unsafe {
        let (prefix, aligned_data, suffix) = input.align_to_mut::<__m128i>();

        // Run fallback implementation on unaligned prefix data
        if !prefix.is_empty() {
            fallback_frame(key, prefix);
        }

        if !aligned_data.is_empty() {
            let mask = _mm_set1_epi32(i32::from_ne_bytes(*key));

            for block in aligned_data {
                *block = _mm_xor_si128(*block, mask);
            }
        }

        // Run fallback implementation on unaligned suffix data
        if !suffix.is_empty() {
            fallback_frame(key, suffix);
        }
    }
}

/// (Un-)masks input bytes with the framing key using NEON.
///
/// This will use a fallback implementation for less than 16 bytes. For
/// sufficiently large inputs, it masks in chunks of 16 bytes per
/// instruction, applying the fallback method on all remaining data.
#[cfg(any(all(feature = "nightly", target_arch = "arm"), target_arch = "aarch64"))]
#[target_feature(enable = "neon")]
unsafe fn frame_neon(key: &mut [u8; 4], input: &mut [u8]) {
    #[cfg(target_arch = "aarch64")]
    use std::arch::aarch64::{uint8x16_t, veorq_u8, vld1q_dup_s32, vreinterpretq_u8_s32};
    #[cfg(target_arch = "arm")]
    use std::arch::arm::{uint8x16_t, veorq_u8, vld1q_dup_s32, vreinterpretq_u8_s32};

    unsafe {
        let (prefix, aligned_data, suffix) = input.align_to_mut::<uint8x16_t>();

        // Run fallback implementation on unaligned prefix data
        if !prefix.is_empty() {
            fallback_frame(key, prefix);
        }

        if !aligned_data.is_empty() {
            let mask = vreinterpretq_u8_s32(vld1q_dup_s32(key.as_ptr() as *const i32));

            for block in aligned_data {
                *block = veorq_u8(*block, mask);
            }
        }

        // Run fallback implementation on unaligned suffix data
        if !suffix.is_empty() {
            fallback_frame(key, suffix);
        }
    }
}

/// (Un-)masks input bytes with the framing key using AltiVec.
///
/// This will use a fallback implementation for less than 16 bytes. For
/// sufficiently large inputs, it masks in chunks of 16 bytes per
/// instruction, applying the fallback method on all remaining data.
#[cfg(all(
    feature = "nightly",
    any(target_arch = "powerpc", target_arch = "powerpc64")
))]
#[target_feature(enable = "altivec")]
unsafe fn frame_altivec(key: &mut [u8; 4], input: &mut [u8]) {
    #[cfg(target_arch = "powerpc")]
    use std::arch::powerpc::{vec_splats, vec_xor, vector_unsigned_char};
    #[cfg(target_arch = "powerpc64")]
    use std::arch::powerpc64::{vec_splats, vec_xor, vector_unsigned_char};
    use std::mem::transmute;

    unsafe {
        let (prefix, aligned_data, suffix) = input.align_to_mut::<vector_unsigned_char>();

        // Run fallback implementation on unaligned prefix data
        if !prefix.is_empty() {
            fallback_frame(key, prefix);
        }

        if !aligned_data.is_empty() {
            // SAFETY: 4x i32 to 16x u8 is safe
            let mask: vector_unsigned_char = transmute(vec_splats(i32::from_ne_bytes(*key)));

            for block in aligned_data {
                *block = vec_xor(*block, mask);
            }
        }

        // Run fallback implementation on unaligned suffix data
        if !suffix.is_empty() {
            fallback_frame(key, suffix);
        }
    }
}

/// (Un-)masks input bytes with the framing key using s390x vectors.
///
/// This will use a fallback implementation for less than 16 bytes. For
/// sufficiently large inputs, it masks in chunks of 16 bytes per
/// instruction, applying the fallback method on all remaining data.
#[cfg(all(feature = "nightly", target_arch = "s390x"))]
#[target_feature(enable = "vector")]
unsafe fn frame_s390x_vector(key: &mut [u8; 4], input: &mut [u8]) {
    use std::{
        arch::s390x::{vec_splats, vec_xor, vector_signed_int, vector_unsigned_char},
        mem::transmute,
    };

    unsafe {
        let (prefix, aligned_data, suffix) = input.align_to_mut::<vector_unsigned_char>();

        // Run fallback implementation on unaligned prefix data
        if !prefix.is_empty() {
            fallback_frame(key, prefix);
        }

        if !aligned_data.is_empty() {
            // SAFETY: 4x i32 to 16x u8 is safe
            let mask: vector_unsigned_char = transmute(vec_splats::<i32, vector_signed_int>(
                i32::from_ne_bytes(*key),
            ));

            for block in aligned_data {
                *block = vec_xor(*block, mask);
            }
        }

        // Run fallback implementation on unaligned suffix data
        if !suffix.is_empty() {
            fallback_frame(key, suffix);
        }
    }
}

/// (Un-)masks input bytes with the framing key using LASX.
///
/// This will use a fallback implementation for less than 32 bytes. For
/// sufficiently large inputs, it masks in chunks of 32 bytes per
/// instruction, applying the fallback method on all remaining data.
#[cfg(all(feature = "nightly", target_arch = "loongarch64"))]
#[target_feature(enable = "lasx")]
unsafe fn frame_lasx_vector(key: &mut [u8; 4], input: &mut [u8]) {
    use std::{
        arch::loongarch64::{lasx_xvld, lasx_xvxor_v, v32u8},
        mem::transmute,
    };

    unsafe {
        let (prefix, aligned_data, suffix) = input.align_to_mut::<v32u8>();

        // Run fallback implementation on unaligned prefix data
        if !prefix.is_empty() {
            fallback_frame(key, prefix);
        }

        if !aligned_data.is_empty() {
            let key_vector = [i32::from_ne_bytes(*key); 8];
            // SAFETY: 32x i8 to 32x u8 is safe
            let mask: v32u8 = transmute(lasx_xvld::<0>(key_vector.as_ptr().cast()));

            for block in aligned_data {
                *block = lasx_xvxor_v(*block, mask);
            }
        }

        // Run fallback implementation on unaligned suffix data
        if !suffix.is_empty() {
            fallback_frame(key, suffix);
        }
    }
}

/// Rotates the mask in-place by a certain amount of bytes.
#[allow(clippy::cast_possible_truncation)] // offset % 4 is within u32 bounds
fn rotate_mask(key: &mut [u8; 4], offset: usize) {
    *key = u32::from_be_bytes(*key)
        .rotate_left((offset % key.len()) as u32 * u8::BITS)
        .to_be_bytes();
}

/// (Un-)masks input bytes with the framing key, one byte at once.
///
/// See [`fallback_frame`] for more details.
fn one_byte_at_once(key: &mut [u8; 4], input: &mut [u8]) {
    for (index, byte) in input.iter_mut().enumerate() {
        *byte ^= key[index % key.len()];
    }

    rotate_mask(key, input.len());
}

/// (Un-)masks input bytes with the framing key.
///
/// This is used as the internal implementation in non-SIMD builds and as a
/// fallback in SIMD builds.
fn fallback_frame(key: &mut [u8; 4], input: &mut [u8]) {
    let (prefix, aligned_data, suffix) = unsafe { input.align_to_mut::<u64>() };

    // Run fallback implementation on unaligned prefix data
    if !prefix.is_empty() {
        one_byte_at_once(key, prefix);
    }

    if !aligned_data.is_empty() {
        let masking_key = u64::from(u32::from_ne_bytes(*key));
        let mask = (masking_key << u32::BITS) | masking_key;

        for block in aligned_data {
            *block ^= mask;
        }
    }

    // Run fallback implementation on unaligned suffix data
    if !suffix.is_empty() {
        one_byte_at_once(key, suffix);
    }
}

/// (Un-)masks input bytes with the framing key.
#[inline]
pub fn frame(key: &mut [u8; 4], input: &mut [u8]) {
    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
    {
        use std::arch::is_x86_feature_detected;

        #[cfg(feature = "nightly")]
        if is_x86_feature_detected!("avx512f") {
            return unsafe { frame_avx512(key, input) };
        }

        if is_x86_feature_detected!("avx2") {
            return unsafe { frame_avx2(key, input) };
        } else if is_x86_feature_detected!("sse2") {
            return unsafe { frame_sse2(key, input) };
        }
    }

    #[cfg(all(feature = "nightly", target_arch = "arm"))]
    {
        use std::arch::is_arm_feature_detected;

        if is_arm_feature_detected!("neon") {
            return unsafe { frame_neon(key, input) };
        }
    }

    #[cfg(target_arch = "aarch64")]
    {
        use std::arch::is_aarch64_feature_detected;

        if is_aarch64_feature_detected!("neon") {
            return unsafe { frame_neon(key, input) };
        }
    }

    #[cfg(all(feature = "nightly", target_arch = "powerpc"))]
    {
        use std::arch::is_powerpc_feature_detected;

        if is_powerpc_feature_detected!("altivec") {
            return unsafe { frame_altivec(key, input) };
        }
    }

    #[cfg(all(feature = "nightly", target_arch = "powerpc64"))]
    {
        use std::arch::is_powerpc64_feature_detected;

        if is_powerpc64_feature_detected!("altivec") {
            return unsafe { frame_altivec(key, input) };
        }
    }

    #[cfg(all(feature = "nightly", target_arch = "s390x"))]
    {
        use std::arch::is_s390x_feature_detected;

        if is_s390x_feature_detected!("vector") {
            return unsafe { frame_s390x_vector(key, input) };
        }
    }

    #[cfg(all(feature = "nightly", target_arch = "loongarch64"))]
    {
        use std::arch::is_loongarch_feature_detected;

        if is_loongarch_feature_detected!("lasx") {
            return unsafe { frame_lasx_vector(key, input) };
        }
    }

    fallback_frame(key, input);
}

#[cfg(all(test, feature = "client", feature = "fastrand"))]
#[test]
fn test_mask() {
    use crate::rand::get_mask;

    let mut random_data: Vec<u8> = std::iter::repeat_with(|| fastrand::u8(..))
        .take(1024)
        .collect();
    // Mess around with the data to ensure we have unaligned input
    let data = &mut random_data[2..998];
    let mut data_clone = data.to_vec();
    let mut mask = [0; 4];
    get_mask(&mut mask);
    let mut mask2 = mask;
    frame(&mut mask, data);
    one_byte_at_once(&mut mask2, &mut data_clone);

    assert_eq!(&data, &data_clone);
}