Commit baf5d743d1b8783fdbd5c1260ada2926e5bbaaee

Authored by Jamal Hadi Salim
Committed by David S. Miller
1 parent 1b6651f1bf

[XFRM] Optimize policy dumping

This change optimizes the dumping of Security policies.

1) Before this change ..
speedopolis:~# time ./ip xf pol

real    0m22.274s
user    0m0.000s
sys     0m22.269s

2) Turn off sub-policies

speedopolis:~# ./ip xf pol

real    0m13.496s
user    0m0.000s
sys     0m13.493s

i suppose the above is to be expected

3) With this change ..
speedopolis:~# time ./ip x policy

real    0m7.901s
user    0m0.008s
sys     0m7.896s

Showing 1 changed file with 25 additions and 30 deletions Side-by-side Diff

net/xfrm/xfrm_policy.c
... ... @@ -860,34 +860,13 @@
860 860 int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
861 861 void *data)
862 862 {
863   - struct xfrm_policy *pol;
  863 + struct xfrm_policy *pol, *last = NULL;
864 864 struct hlist_node *entry;
865   - int dir, count, error;
  865 + int dir, last_dir = 0, count, error;
866 866  
867 867 read_lock_bh(&xfrm_policy_lock);
868 868 count = 0;
869   - for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
870   - struct hlist_head *table = xfrm_policy_bydst[dir].table;
871   - int i;
872 869  
873   - hlist_for_each_entry(pol, entry,
874   - &xfrm_policy_inexact[dir], bydst) {
875   - if (pol->type == type)
876   - count++;
877   - }
878   - for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
879   - hlist_for_each_entry(pol, entry, table + i, bydst) {
880   - if (pol->type == type)
881   - count++;
882   - }
883   - }
884   - }
885   -
886   - if (count == 0) {
887   - error = -ENOENT;
888   - goto out;
889   - }
890   -
891 870 for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
892 871 struct hlist_head *table = xfrm_policy_bydst[dir].table;
893 872 int i;
894 873  
895 874  
... ... @@ -896,21 +875,37 @@
896 875 &xfrm_policy_inexact[dir], bydst) {
897 876 if (pol->type != type)
898 877 continue;
899   - error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
900   - if (error)
901   - goto out;
  878 + if (last) {
  879 + error = func(last, last_dir % XFRM_POLICY_MAX,
  880 + count, data);
  881 + if (error)
  882 + goto out;
  883 + }
  884 + last = pol;
  885 + last_dir = dir;
  886 + count++;
902 887 }
903 888 for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
904 889 hlist_for_each_entry(pol, entry, table + i, bydst) {
905 890 if (pol->type != type)
906 891 continue;
907   - error = func(pol, dir % XFRM_POLICY_MAX, --count, data);
908   - if (error)
909   - goto out;
  892 + if (last) {
  893 + error = func(last, last_dir % XFRM_POLICY_MAX,
  894 + count, data);
  895 + if (error)
  896 + goto out;
  897 + }
  898 + last = pol;
  899 + last_dir = dir;
  900 + count++;
910 901 }
911 902 }
912 903 }
913   - error = 0;
  904 + if (count == 0) {
  905 + error = -ENOENT;
  906 + goto out;
  907 + }
  908 + error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
914 909 out:
915 910 read_unlock_bh(&xfrm_policy_lock);
916 911 return error;