@@ -173,29 +173,26 @@ static void flow_new_hash_rnd(struct flow_cache *fc,
173173
174174static u32 flow_hash_code (struct flow_cache * fc ,
175175 struct flow_cache_percpu * fcp ,
176- const struct flowi * key )
176+ const struct flowi * key ,
177+ size_t keysize )
177178{
178179 const u32 * k = (const u32 * ) key ;
180+ const u32 length = keysize * sizeof (flow_compare_t ) / sizeof (u32 );
179181
180- return jhash2 (k , ( sizeof ( * key ) / sizeof ( u32 )) , fcp -> hash_rnd )
182+ return jhash2 (k , length , fcp -> hash_rnd )
181183 & (flow_cache_hash_size (fc ) - 1 );
182184}
183185
184- typedef unsigned long flow_compare_t ;
185-
186186/* I hear what you're saying, use memcmp. But memcmp cannot make
187- * important assumptions that we can here, such as alignment and
188- * constant size.
187+ * important assumptions that we can here, such as alignment.
189188 */
190- static int flow_key_compare (const struct flowi * key1 , const struct flowi * key2 )
189+ static int flow_key_compare (const struct flowi * key1 , const struct flowi * key2 ,
190+ size_t keysize )
191191{
192192 const flow_compare_t * k1 , * k1_lim , * k2 ;
193- const int n_elem = sizeof (struct flowi ) / sizeof (flow_compare_t );
194-
195- BUILD_BUG_ON (sizeof (struct flowi ) % sizeof (flow_compare_t ));
196193
197194 k1 = (const flow_compare_t * ) key1 ;
198- k1_lim = k1 + n_elem ;
195+ k1_lim = k1 + keysize ;
199196
200197 k2 = (const flow_compare_t * ) key2 ;
201198
@@ -216,13 +213,19 @@ flow_cache_lookup(struct net *net, const struct flowi *key, u16 family, u8 dir,
216213 struct flow_cache_entry * fle , * tfle ;
217214 struct hlist_node * entry ;
218215 struct flow_cache_object * flo ;
216+ size_t keysize ;
219217 unsigned int hash ;
220218
221219 local_bh_disable ();
222220 fcp = this_cpu_ptr (fc -> percpu );
223221
224222 fle = NULL ;
225223 flo = NULL ;
224+
225+ keysize = flow_key_size (family );
226+ if (!keysize )
227+ goto nocache ;
228+
226229 /* Packet really early in init? Making flow_cache_init a
227230 * pre-smp initcall would solve this. --RR */
228231 if (!fcp -> hash_table )
@@ -231,12 +234,12 @@ flow_cache_lookup(struct net *net, const struct flowi *key, u16 family, u8 dir,
231234 if (fcp -> hash_rnd_recalc )
232235 flow_new_hash_rnd (fc , fcp );
233236
234- hash = flow_hash_code (fc , fcp , key );
237+ hash = flow_hash_code (fc , fcp , key , keysize );
235238 hlist_for_each_entry (tfle , entry , & fcp -> hash_table [hash ], u .hlist ) {
236239 if (tfle -> net == net &&
237240 tfle -> family == family &&
238241 tfle -> dir == dir &&
239- flow_key_compare (key , & tfle -> key ) == 0 ) {
242+ flow_key_compare (key , & tfle -> key , keysize ) == 0 ) {
240243 fle = tfle ;
241244 break ;
242245 }
@@ -251,7 +254,7 @@ flow_cache_lookup(struct net *net, const struct flowi *key, u16 family, u8 dir,
251254 fle -> net = net ;
252255 fle -> family = family ;
253256 fle -> dir = dir ;
254- memcpy (& fle -> key , key , sizeof (* key ));
257+ memcpy (& fle -> key , key , keysize * sizeof (flow_compare_t ));
255258 fle -> object = NULL ;
256259 hlist_add_head (& fle -> u .hlist , & fcp -> hash_table [hash ]);
257260 fcp -> hash_count ++ ;
0 commit comments