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); > >> Thanks & Regards, >> Dominic >> >> . > > .