18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef __BPF_RAND__ 38c2ecf20Sopenharmony_ci#define __BPF_RAND__ 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <stdint.h> 68c2ecf20Sopenharmony_ci#include <stdlib.h> 78c2ecf20Sopenharmony_ci#include <time.h> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_cistatic inline uint64_t bpf_rand_mask(uint64_t mask) 108c2ecf20Sopenharmony_ci{ 118c2ecf20Sopenharmony_ci return (((uint64_t)(uint32_t)rand()) | 128c2ecf20Sopenharmony_ci ((uint64_t)(uint32_t)rand() << 32)) & mask; 138c2ecf20Sopenharmony_ci} 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define bpf_rand_ux(x, m) \ 168c2ecf20Sopenharmony_cistatic inline uint64_t bpf_rand_u##x(int shift) \ 178c2ecf20Sopenharmony_ci{ \ 188c2ecf20Sopenharmony_ci return bpf_rand_mask((m)) << shift; \ 198c2ecf20Sopenharmony_ci} 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cibpf_rand_ux( 8, 0xffULL) 228c2ecf20Sopenharmony_cibpf_rand_ux(16, 0xffffULL) 238c2ecf20Sopenharmony_cibpf_rand_ux(24, 0xffffffULL) 248c2ecf20Sopenharmony_cibpf_rand_ux(32, 0xffffffffULL) 258c2ecf20Sopenharmony_cibpf_rand_ux(40, 0xffffffffffULL) 268c2ecf20Sopenharmony_cibpf_rand_ux(48, 0xffffffffffffULL) 278c2ecf20Sopenharmony_cibpf_rand_ux(56, 0xffffffffffffffULL) 288c2ecf20Sopenharmony_cibpf_rand_ux(64, 0xffffffffffffffffULL) 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic inline void bpf_semi_rand_init(void) 318c2ecf20Sopenharmony_ci{ 328c2ecf20Sopenharmony_ci srand(time(NULL)); 338c2ecf20Sopenharmony_ci} 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic inline uint64_t bpf_semi_rand_get(void) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci switch (rand() % 39) { 388c2ecf20Sopenharmony_ci case 0: return 0x000000ff00000000ULL | bpf_rand_u8(0); 398c2ecf20Sopenharmony_ci case 1: return 0xffffffff00000000ULL | bpf_rand_u16(0); 408c2ecf20Sopenharmony_ci case 2: return 0x00000000ffff0000ULL | bpf_rand_u16(0); 418c2ecf20Sopenharmony_ci case 3: return 0x8000000000000000ULL | bpf_rand_u32(0); 428c2ecf20Sopenharmony_ci case 4: return 0x00000000f0000000ULL | bpf_rand_u32(0); 438c2ecf20Sopenharmony_ci case 5: return 0x0000000100000000ULL | bpf_rand_u24(0); 448c2ecf20Sopenharmony_ci case 6: return 0x800ff00000000000ULL | bpf_rand_u32(0); 458c2ecf20Sopenharmony_ci case 7: return 0x7fffffff00000000ULL | bpf_rand_u32(0); 468c2ecf20Sopenharmony_ci case 8: return 0xffffffffffffff00ULL ^ bpf_rand_u32(24); 478c2ecf20Sopenharmony_ci case 9: return 0xffffffffffffff00ULL | bpf_rand_u8(0); 488c2ecf20Sopenharmony_ci case 10: return 0x0000000010000000ULL | bpf_rand_u32(0); 498c2ecf20Sopenharmony_ci case 11: return 0xf000000000000000ULL | bpf_rand_u8(0); 508c2ecf20Sopenharmony_ci case 12: return 0x0000f00000000000ULL | bpf_rand_u8(8); 518c2ecf20Sopenharmony_ci case 13: return 0x000000000f000000ULL | bpf_rand_u8(16); 528c2ecf20Sopenharmony_ci case 14: return 0x0000000000000f00ULL | bpf_rand_u8(32); 538c2ecf20Sopenharmony_ci case 15: return 0x00fff00000000f00ULL | bpf_rand_u8(48); 548c2ecf20Sopenharmony_ci case 16: return 0x00007fffffffffffULL ^ bpf_rand_u32(1); 558c2ecf20Sopenharmony_ci case 17: return 0xffff800000000000ULL | bpf_rand_u8(4); 568c2ecf20Sopenharmony_ci case 18: return 0xffff800000000000ULL | bpf_rand_u8(20); 578c2ecf20Sopenharmony_ci case 19: return (0xffffffc000000000ULL + 0x80000ULL) | bpf_rand_u32(0); 588c2ecf20Sopenharmony_ci case 20: return (0xffffffc000000000ULL - 0x04000000ULL) | bpf_rand_u32(0); 598c2ecf20Sopenharmony_ci case 21: return 0x0000000000000000ULL | bpf_rand_u8(55) | bpf_rand_u32(20); 608c2ecf20Sopenharmony_ci case 22: return 0xffffffffffffffffULL ^ bpf_rand_u8(3) ^ bpf_rand_u32(40); 618c2ecf20Sopenharmony_ci case 23: return 0x0000000000000000ULL | bpf_rand_u8(bpf_rand_u8(0) % 64); 628c2ecf20Sopenharmony_ci case 24: return 0x0000000000000000ULL | bpf_rand_u16(bpf_rand_u8(0) % 64); 638c2ecf20Sopenharmony_ci case 25: return 0xffffffffffffffffULL ^ bpf_rand_u8(bpf_rand_u8(0) % 64); 648c2ecf20Sopenharmony_ci case 26: return 0xffffffffffffffffULL ^ bpf_rand_u40(bpf_rand_u8(0) % 64); 658c2ecf20Sopenharmony_ci case 27: return 0x0000800000000000ULL; 668c2ecf20Sopenharmony_ci case 28: return 0x8000000000000000ULL; 678c2ecf20Sopenharmony_ci case 29: return 0x0000000000000000ULL; 688c2ecf20Sopenharmony_ci case 30: return 0xffffffffffffffffULL; 698c2ecf20Sopenharmony_ci case 31: return bpf_rand_u16(bpf_rand_u8(0) % 64); 708c2ecf20Sopenharmony_ci case 32: return bpf_rand_u24(bpf_rand_u8(0) % 64); 718c2ecf20Sopenharmony_ci case 33: return bpf_rand_u32(bpf_rand_u8(0) % 64); 728c2ecf20Sopenharmony_ci case 34: return bpf_rand_u40(bpf_rand_u8(0) % 64); 738c2ecf20Sopenharmony_ci case 35: return bpf_rand_u48(bpf_rand_u8(0) % 64); 748c2ecf20Sopenharmony_ci case 36: return bpf_rand_u56(bpf_rand_u8(0) % 64); 758c2ecf20Sopenharmony_ci case 37: return bpf_rand_u64(bpf_rand_u8(0) % 64); 768c2ecf20Sopenharmony_ci default: return bpf_rand_u64(0); 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci#endif /* __BPF_RAND__ */ 81