On Sat, Sep 7, 2024 at 4:33 PM Vincent Li <vincent.mc.li@xxxxxxxxx> wrote: > > Hi Tao, > > Sorry to hijack this old email thread :) > > On Mon, Dec 25, 2023 at 5:39 PM Hou Tao <houtao@xxxxxxxxxxxxxxx> wrote: > > > > > > > > On 12/25/2023 10:24 PM, Hou Tao wrote: > > > Hi, > > > > > > On 12/22/2023 8:05 PM, Dominic wrote: > > >> Can BPF_MAP_TYPE_LPM_TRIE be used for string matching? I tried it but > > >> the matching doesn't work as expected. > > > Yes. LPM_TRIE will work for string matching. Did you setup the key size > > > of the map and the prefixlen field of bpf_lpm_trie_key correctly ? > > > Because the unit of key_size is in-bytes and it should be the maximal > > > length of these strings, but the unit of prefixlen is in-bits and it is > > > the length of string expressed in bits. And could you share the steps on > > > how you used it ? > > > > Forgot to mention the trick when using LPM_TRIE for string matching. > > Because LPM_TRIE uses longest prefix matching to find the target > > element, so using the string abcd as key to lookup LPM_TRIE will match > > the string abc saved in LPM_TRIE and it is not we wanted. To fix that, > > we need to add the terminated null byte of the string to the key, so the > > string abc\0 will not be the prefix of the string abcd\0. The code > > snippet looks as follows. > > > > map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, name, > > sizeof(bpf_lpm_trie_key) + max_string_length + 1, value_size, > > max_entries, &opts); > > > > key.prefixlen = (strlen(str) + 1) * 8; > > memcpy(key.data, str, strlen(str) + 1); > > bpf_map_update_elem(map_fd, &key, &value, BPF_NOEXIST); > > > > I have a use case where I want to run an XDP program to parse the DNS > packet to get the query domain name, then lookup the domain name in > bpf map to decide if the query is allowed or not. For example if the > LPM_TRIE map is pre-populated with "example.com", then queries like > "foo.example.com", "bar.example.com" should be matched, right? > > I haven't gone that far yet because I haven't got a string match in > hash map working, but that LPM_TRIE map would be my end goal. > > Here is the hash map string not matching problem > https://github.com/vincentmli/xdp-tools/issues/2, am I missing > something about string terminator '\0'? > Sorry for the noise, my hash string key inserted from user space missed characters like "wwwbpfirenet" should have been "3www6bpfire3net" > > > > > > >> Thanks & Regards, > > >> Dominic > > >> > > >> . > > > > > > . > > > >