Skip to content

Commit aa1c366

Browse files
dpwarddavem330
authored andcommitted
net: Handle different key sizes between address families in flow cache
With the conversion of struct flowi to a union of AF-specific structs, some operations on the flow cache need to account for the exact size of the key. Signed-off-by: David Ward <david.ward@ll.mit.edu> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 728871b commit aa1c366

2 files changed

Lines changed: 36 additions & 14 deletions

File tree

‎include/net/flow.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef _NET_FLOW_H
88
#define _NET_FLOW_H
99

10+
#include <linux/socket.h>
1011
#include <linux/in6.h>
1112
#include <linux/atomic.h>
1213

@@ -161,6 +162,24 @@ static inline struct flowi *flowidn_to_flowi(struct flowidn *fldn)
161162
return container_of(fldn, struct flowi, u.dn);
162163
}
163164

165+
typedef unsigned long flow_compare_t;
166+
167+
static inline size_t flow_key_size(u16 family)
168+
{
169+
switch (family) {
170+
case AF_INET:
171+
BUILD_BUG_ON(sizeof(struct flowi4) % sizeof(flow_compare_t));
172+
return sizeof(struct flowi4) / sizeof(flow_compare_t);
173+
case AF_INET6:
174+
BUILD_BUG_ON(sizeof(struct flowi6) % sizeof(flow_compare_t));
175+
return sizeof(struct flowi6) / sizeof(flow_compare_t);
176+
case AF_DECnet:
177+
BUILD_BUG_ON(sizeof(struct flowidn) % sizeof(flow_compare_t));
178+
return sizeof(struct flowidn) / sizeof(flow_compare_t);
179+
}
180+
return 0;
181+
}
182+
164183
#define FLOW_DIR_IN 0
165184
#define FLOW_DIR_OUT 1
166185
#define FLOW_DIR_FWD 2

‎net/core/flow.c‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,29 +173,26 @@ static void flow_new_hash_rnd(struct flow_cache *fc,
173173

174174
static 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

Comments
 (0)