48
48
#define SOCK_CREATE_FLAG_MASK \
49
49
(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
50
50
51
- struct bpf_stab {
52
- struct bpf_map map ;
53
- struct sock * * sock_map ;
51
+ struct bpf_sock_progs {
54
52
struct bpf_prog * bpf_tx_msg ;
55
53
struct bpf_prog * bpf_parse ;
56
54
struct bpf_prog * bpf_verdict ;
57
55
};
58
56
57
+ struct bpf_stab {
58
+ struct bpf_map map ;
59
+ struct sock * * sock_map ;
60
+ struct bpf_sock_progs progs ;
61
+ };
62
+
59
63
enum smap_psock_state {
60
64
SMAP_TX_RUNNING ,
61
65
};
@@ -461,7 +465,7 @@ static int free_curr_sg(struct sock *sk, struct sk_msg_buff *md)
461
465
static int bpf_map_msg_verdict (int _rc , struct sk_msg_buff * md )
462
466
{
463
467
return ((_rc == SK_PASS ) ?
464
- (md -> map ? __SK_REDIRECT : __SK_PASS ) :
468
+ (md -> sk_redir ? __SK_REDIRECT : __SK_PASS ) :
465
469
__SK_DROP );
466
470
}
467
471
@@ -1092,7 +1096,7 @@ static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)
1092
1096
* when we orphan the skb so that we don't have the possibility
1093
1097
* to reference a stale map.
1094
1098
*/
1095
- TCP_SKB_CB (skb )-> bpf .map = NULL ;
1099
+ TCP_SKB_CB (skb )-> bpf .sk_redir = NULL ;
1096
1100
skb -> sk = psock -> sock ;
1097
1101
bpf_compute_data_pointers (skb );
1098
1102
preempt_disable ();
@@ -1102,7 +1106,7 @@ static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)
1102
1106
1103
1107
/* Moving return codes from UAPI namespace into internal namespace */
1104
1108
return rc == SK_PASS ?
1105
- (TCP_SKB_CB (skb )-> bpf .map ? __SK_REDIRECT : __SK_PASS ) :
1109
+ (TCP_SKB_CB (skb )-> bpf .sk_redir ? __SK_REDIRECT : __SK_PASS ) :
1106
1110
__SK_DROP ;
1107
1111
}
1108
1112
@@ -1372,7 +1376,6 @@ static int smap_init_sock(struct smap_psock *psock,
1372
1376
}
1373
1377
1374
1378
static void smap_init_progs (struct smap_psock * psock ,
1375
- struct bpf_stab * stab ,
1376
1379
struct bpf_prog * verdict ,
1377
1380
struct bpf_prog * parse )
1378
1381
{
@@ -1450,14 +1453,13 @@ static void smap_gc_work(struct work_struct *w)
1450
1453
kfree (psock );
1451
1454
}
1452
1455
1453
- static struct smap_psock * smap_init_psock (struct sock * sock ,
1454
- struct bpf_stab * stab )
1456
+ static struct smap_psock * smap_init_psock (struct sock * sock , int node )
1455
1457
{
1456
1458
struct smap_psock * psock ;
1457
1459
1458
1460
psock = kzalloc_node (sizeof (struct smap_psock ),
1459
1461
GFP_ATOMIC | __GFP_NOWARN ,
1460
- stab -> map . numa_node );
1462
+ node );
1461
1463
if (!psock )
1462
1464
return ERR_PTR (- ENOMEM );
1463
1465
@@ -1662,60 +1664,46 @@ static int sock_map_delete_elem(struct bpf_map *map, void *key)
1662
1664
* - sock_map must use READ_ONCE and (cmp)xchg operations
1663
1665
* - BPF verdict/parse programs must use READ_ONCE and xchg operations
1664
1666
*/
1665
- static int sock_map_ctx_update_elem (struct bpf_sock_ops_kern * skops ,
1666
- struct bpf_map * map ,
1667
- void * key , u64 flags )
1667
+
1668
+ static int __sock_map_ctx_update_elem (struct bpf_map * map ,
1669
+ struct bpf_sock_progs * progs ,
1670
+ struct sock * sock ,
1671
+ struct sock * * map_link ,
1672
+ void * key )
1668
1673
{
1669
- struct bpf_stab * stab = container_of (map , struct bpf_stab , map );
1670
- struct smap_psock_map_entry * e = NULL ;
1671
1674
struct bpf_prog * verdict , * parse , * tx_msg ;
1672
- struct sock * osock , * sock ;
1675
+ struct smap_psock_map_entry * e = NULL ;
1673
1676
struct smap_psock * psock ;
1674
- u32 i = * (u32 * )key ;
1675
1677
bool new = false;
1676
1678
int err ;
1677
1679
1678
- if (unlikely (flags > BPF_EXIST ))
1679
- return - EINVAL ;
1680
-
1681
- if (unlikely (i >= stab -> map .max_entries ))
1682
- return - E2BIG ;
1683
-
1684
- sock = READ_ONCE (stab -> sock_map [i ]);
1685
- if (flags == BPF_EXIST && !sock )
1686
- return - ENOENT ;
1687
- else if (flags == BPF_NOEXIST && sock )
1688
- return - EEXIST ;
1689
-
1690
- sock = skops -> sk ;
1691
-
1692
1680
/* 1. If sock map has BPF programs those will be inherited by the
1693
1681
* sock being added. If the sock is already attached to BPF programs
1694
1682
* this results in an error.
1695
1683
*/
1696
- verdict = READ_ONCE (stab -> bpf_verdict );
1697
- parse = READ_ONCE (stab -> bpf_parse );
1698
- tx_msg = READ_ONCE (stab -> bpf_tx_msg );
1684
+ verdict = READ_ONCE (progs -> bpf_verdict );
1685
+ parse = READ_ONCE (progs -> bpf_parse );
1686
+ tx_msg = READ_ONCE (progs -> bpf_tx_msg );
1699
1687
1700
1688
if (parse && verdict ) {
1701
1689
/* bpf prog refcnt may be zero if a concurrent attach operation
1702
1690
* removes the program after the above READ_ONCE() but before
1703
1691
* we increment the refcnt. If this is the case abort with an
1704
1692
* error.
1705
1693
*/
1706
- verdict = bpf_prog_inc_not_zero (stab -> bpf_verdict );
1694
+ verdict = bpf_prog_inc_not_zero (progs -> bpf_verdict );
1707
1695
if (IS_ERR (verdict ))
1708
1696
return PTR_ERR (verdict );
1709
1697
1710
- parse = bpf_prog_inc_not_zero (stab -> bpf_parse );
1698
+ parse = bpf_prog_inc_not_zero (progs -> bpf_parse );
1711
1699
if (IS_ERR (parse )) {
1712
1700
bpf_prog_put (verdict );
1713
1701
return PTR_ERR (parse );
1714
1702
}
1715
1703
}
1716
1704
1717
1705
if (tx_msg ) {
1718
- tx_msg = bpf_prog_inc_not_zero (stab -> bpf_tx_msg );
1706
+ tx_msg = bpf_prog_inc_not_zero (progs -> bpf_tx_msg );
1719
1707
if (IS_ERR (tx_msg )) {
1720
1708
if (verdict )
1721
1709
bpf_prog_put (verdict );
@@ -1748,7 +1736,7 @@ static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops,
1748
1736
goto out_progs ;
1749
1737
}
1750
1738
} else {
1751
- psock = smap_init_psock (sock , stab );
1739
+ psock = smap_init_psock (sock , map -> numa_node );
1752
1740
if (IS_ERR (psock )) {
1753
1741
err = PTR_ERR (psock );
1754
1742
goto out_progs ;
@@ -1763,7 +1751,6 @@ static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops,
1763
1751
err = - ENOMEM ;
1764
1752
goto out_progs ;
1765
1753
}
1766
- e -> entry = & stab -> sock_map [i ];
1767
1754
1768
1755
/* 3. At this point we have a reference to a valid psock that is
1769
1756
* running. Attach any BPF programs needed.
@@ -1780,7 +1767,7 @@ static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops,
1780
1767
err = smap_init_sock (psock , sock );
1781
1768
if (err )
1782
1769
goto out_free ;
1783
- smap_init_progs (psock , stab , verdict , parse );
1770
+ smap_init_progs (psock , verdict , parse );
1784
1771
smap_start_sock (psock , sock );
1785
1772
}
1786
1773
@@ -1789,19 +1776,12 @@ static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops,
1789
1776
* it with. Because we can only have a single set of programs if
1790
1777
* old_sock has a strp we can stop it.
1791
1778
*/
1792
- list_add_tail (& e -> list , & psock -> maps );
1793
- write_unlock_bh (& sock -> sk_callback_lock );
1794
-
1795
- osock = xchg (& stab -> sock_map [i ], sock );
1796
- if (osock ) {
1797
- struct smap_psock * opsock = smap_psock_sk (osock );
1798
-
1799
- write_lock_bh (& osock -> sk_callback_lock );
1800
- smap_list_remove (opsock , & stab -> sock_map [i ]);
1801
- smap_release_sock (opsock , osock );
1802
- write_unlock_bh (& osock -> sk_callback_lock );
1779
+ if (map_link ) {
1780
+ e -> entry = map_link ;
1781
+ list_add_tail (& e -> list , & psock -> maps );
1803
1782
}
1804
- return 0 ;
1783
+ write_unlock_bh (& sock -> sk_callback_lock );
1784
+ return err ;
1805
1785
out_free :
1806
1786
smap_release_sock (psock , sock );
1807
1787
out_progs :
@@ -1816,23 +1796,69 @@ static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops,
1816
1796
return err ;
1817
1797
}
1818
1798
1819
- int sock_map_prog (struct bpf_map * map , struct bpf_prog * prog , u32 type )
1799
+ static int sock_map_ctx_update_elem (struct bpf_sock_ops_kern * skops ,
1800
+ struct bpf_map * map ,
1801
+ void * key , u64 flags )
1820
1802
{
1821
1803
struct bpf_stab * stab = container_of (map , struct bpf_stab , map );
1804
+ struct bpf_sock_progs * progs = & stab -> progs ;
1805
+ struct sock * osock , * sock ;
1806
+ u32 i = * (u32 * )key ;
1807
+ int err ;
1808
+
1809
+ if (unlikely (flags > BPF_EXIST ))
1810
+ return - EINVAL ;
1811
+
1812
+ if (unlikely (i >= stab -> map .max_entries ))
1813
+ return - E2BIG ;
1814
+
1815
+ sock = READ_ONCE (stab -> sock_map [i ]);
1816
+ if (flags == BPF_EXIST && !sock )
1817
+ return - ENOENT ;
1818
+ else if (flags == BPF_NOEXIST && sock )
1819
+ return - EEXIST ;
1820
+
1821
+ sock = skops -> sk ;
1822
+ err = __sock_map_ctx_update_elem (map , progs , sock , & stab -> sock_map [i ],
1823
+ key );
1824
+ if (err )
1825
+ goto out ;
1826
+
1827
+ osock = xchg (& stab -> sock_map [i ], sock );
1828
+ if (osock ) {
1829
+ struct smap_psock * opsock = smap_psock_sk (osock );
1830
+
1831
+ write_lock_bh (& osock -> sk_callback_lock );
1832
+ smap_list_remove (opsock , & stab -> sock_map [i ]);
1833
+ smap_release_sock (opsock , osock );
1834
+ write_unlock_bh (& osock -> sk_callback_lock );
1835
+ }
1836
+ out :
1837
+ return 0 ;
1838
+ }
1839
+
1840
+ int sock_map_prog (struct bpf_map * map , struct bpf_prog * prog , u32 type )
1841
+ {
1842
+ struct bpf_sock_progs * progs ;
1822
1843
struct bpf_prog * orig ;
1823
1844
1824
- if (unlikely (map -> map_type != BPF_MAP_TYPE_SOCKMAP ))
1845
+ if (map -> map_type == BPF_MAP_TYPE_SOCKMAP ) {
1846
+ struct bpf_stab * stab = container_of (map , struct bpf_stab , map );
1847
+
1848
+ progs = & stab -> progs ;
1849
+ } else {
1825
1850
return - EINVAL ;
1851
+ }
1826
1852
1827
1853
switch (type ) {
1828
1854
case BPF_SK_MSG_VERDICT :
1829
- orig = xchg (& stab -> bpf_tx_msg , prog );
1855
+ orig = xchg (& progs -> bpf_tx_msg , prog );
1830
1856
break ;
1831
1857
case BPF_SK_SKB_STREAM_PARSER :
1832
- orig = xchg (& stab -> bpf_parse , prog );
1858
+ orig = xchg (& progs -> bpf_parse , prog );
1833
1859
break ;
1834
1860
case BPF_SK_SKB_STREAM_VERDICT :
1835
- orig = xchg (& stab -> bpf_verdict , prog );
1861
+ orig = xchg (& progs -> bpf_verdict , prog );
1836
1862
break ;
1837
1863
default :
1838
1864
return - EOPNOTSUPP ;
@@ -1881,16 +1907,18 @@ static int sock_map_update_elem(struct bpf_map *map,
1881
1907
static void sock_map_release (struct bpf_map * map )
1882
1908
{
1883
1909
struct bpf_stab * stab = container_of (map , struct bpf_stab , map );
1910
+ struct bpf_sock_progs * progs ;
1884
1911
struct bpf_prog * orig ;
1885
1912
1886
- orig = xchg (& stab -> bpf_parse , NULL );
1913
+ progs = & stab -> progs ;
1914
+ orig = xchg (& progs -> bpf_parse , NULL );
1887
1915
if (orig )
1888
1916
bpf_prog_put (orig );
1889
- orig = xchg (& stab -> bpf_verdict , NULL );
1917
+ orig = xchg (& progs -> bpf_verdict , NULL );
1890
1918
if (orig )
1891
1919
bpf_prog_put (orig );
1892
1920
1893
- orig = xchg (& stab -> bpf_tx_msg , NULL );
1921
+ orig = xchg (& progs -> bpf_tx_msg , NULL );
1894
1922
if (orig )
1895
1923
bpf_prog_put (orig );
1896
1924
}
0 commit comments