Skip to content

Commit 7f4b396

Browse files
jmberg-intelkuba-moo
authored andcommitted
net: netlink: add nla_get_*_default() accessors
There are quite a number of places that use patterns such as if (attr) val = nla_get_u16(attr); else val = DEFAULT; Add nla_get_u16_default() and friends like that to not have to type this out all the time. Acked-by: Toke Høiland-Jørgensen <[email protected]> Acked-by: Jakub Kicinski <[email protected]> Signed-off-by: Johannes Berg <[email protected]> Link: https://patch.msgid.link/20241108114145.acd2aadb03ac.I3df6aac71d38a5baa1c0a03d0c7e82d4395c030e@changeid Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 774ca6d commit 7f4b396

File tree

1 file changed

+262
-0
lines changed

1 file changed

+262
-0
lines changed

include/net/netlink.h

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@
142142
* nla_get_flag(nla) return 1 if flag is true
143143
* nla_get_msecs(nla) get payload for a msecs attribute
144144
*
145+
* The same functions also exist with _default().
146+
*
145147
* Attribute Misc:
146148
* nla_memcpy(dest, nla, count) copy attribute into memory
147149
* nla_memcmp(nla, data, size) compare attribute with memory area
@@ -1695,6 +1697,20 @@ static inline u32 nla_get_u32(const struct nlattr *nla)
16951697
return *(u32 *) nla_data(nla);
16961698
}
16971699

1700+
/**
1701+
* nla_get_u32_default - return payload of u32 attribute or default
1702+
* @nla: u32 netlink attribute, may be %NULL
1703+
* @defvalue: default value to use if @nla is %NULL
1704+
*
1705+
* Return: the value of the attribute, or the default value if not present
1706+
*/
1707+
static inline u32 nla_get_u32_default(const struct nlattr *nla, u32 defvalue)
1708+
{
1709+
if (!nla)
1710+
return defvalue;
1711+
return nla_get_u32(nla);
1712+
}
1713+
16981714
/**
16991715
* nla_get_be32 - return payload of __be32 attribute
17001716
* @nla: __be32 netlink attribute
@@ -1704,6 +1720,21 @@ static inline __be32 nla_get_be32(const struct nlattr *nla)
17041720
return *(__be32 *) nla_data(nla);
17051721
}
17061722

1723+
/**
1724+
* nla_get_be32_default - return payload of be32 attribute or default
1725+
* @nla: __be32 netlink attribute, may be %NULL
1726+
* @defvalue: default value to use if @nla is %NULL
1727+
*
1728+
* Return: the value of the attribute, or the default value if not present
1729+
*/
1730+
static inline __be32 nla_get_be32_default(const struct nlattr *nla,
1731+
__be32 defvalue)
1732+
{
1733+
if (!nla)
1734+
return defvalue;
1735+
return nla_get_be32(nla);
1736+
}
1737+
17071738
/**
17081739
* nla_get_le32 - return payload of __le32 attribute
17091740
* @nla: __le32 netlink attribute
@@ -1713,6 +1744,21 @@ static inline __le32 nla_get_le32(const struct nlattr *nla)
17131744
return *(__le32 *) nla_data(nla);
17141745
}
17151746

1747+
/**
1748+
* nla_get_le32_default - return payload of le32 attribute or default
1749+
* @nla: __le32 netlink attribute, may be %NULL
1750+
* @defvalue: default value to use if @nla is %NULL
1751+
*
1752+
* Return: the value of the attribute, or the default value if not present
1753+
*/
1754+
static inline __le32 nla_get_le32_default(const struct nlattr *nla,
1755+
__le32 defvalue)
1756+
{
1757+
if (!nla)
1758+
return defvalue;
1759+
return nla_get_le32(nla);
1760+
}
1761+
17161762
/**
17171763
* nla_get_u16 - return payload of u16 attribute
17181764
* @nla: u16 netlink attribute
@@ -1722,6 +1768,20 @@ static inline u16 nla_get_u16(const struct nlattr *nla)
17221768
return *(u16 *) nla_data(nla);
17231769
}
17241770

1771+
/**
1772+
* nla_get_u16_default - return payload of u16 attribute or default
1773+
* @nla: u16 netlink attribute, may be %NULL
1774+
* @defvalue: default value to use if @nla is %NULL
1775+
*
1776+
* Return: the value of the attribute, or the default value if not present
1777+
*/
1778+
static inline u16 nla_get_u16_default(const struct nlattr *nla, u16 defvalue)
1779+
{
1780+
if (!nla)
1781+
return defvalue;
1782+
return nla_get_u16(nla);
1783+
}
1784+
17251785
/**
17261786
* nla_get_be16 - return payload of __be16 attribute
17271787
* @nla: __be16 netlink attribute
@@ -1731,6 +1791,21 @@ static inline __be16 nla_get_be16(const struct nlattr *nla)
17311791
return *(__be16 *) nla_data(nla);
17321792
}
17331793

1794+
/**
1795+
* nla_get_be16_default - return payload of be16 attribute or default
1796+
* @nla: __be16 netlink attribute, may be %NULL
1797+
* @defvalue: default value to use if @nla is %NULL
1798+
*
1799+
* Return: the value of the attribute, or the default value if not present
1800+
*/
1801+
static inline __be16 nla_get_be16_default(const struct nlattr *nla,
1802+
__be16 defvalue)
1803+
{
1804+
if (!nla)
1805+
return defvalue;
1806+
return nla_get_be16(nla);
1807+
}
1808+
17341809
/**
17351810
* nla_get_le16 - return payload of __le16 attribute
17361811
* @nla: __le16 netlink attribute
@@ -1740,6 +1815,21 @@ static inline __le16 nla_get_le16(const struct nlattr *nla)
17401815
return *(__le16 *) nla_data(nla);
17411816
}
17421817

1818+
/**
1819+
* nla_get_le16_default - return payload of le16 attribute or default
1820+
* @nla: __le16 netlink attribute, may be %NULL
1821+
* @defvalue: default value to use if @nla is %NULL
1822+
*
1823+
* Return: the value of the attribute, or the default value if not present
1824+
*/
1825+
static inline __le16 nla_get_le16_default(const struct nlattr *nla,
1826+
__le16 defvalue)
1827+
{
1828+
if (!nla)
1829+
return defvalue;
1830+
return nla_get_le16(nla);
1831+
}
1832+
17431833
/**
17441834
* nla_get_u8 - return payload of u8 attribute
17451835
* @nla: u8 netlink attribute
@@ -1749,6 +1839,20 @@ static inline u8 nla_get_u8(const struct nlattr *nla)
17491839
return *(u8 *) nla_data(nla);
17501840
}
17511841

1842+
/**
1843+
* nla_get_u8_default - return payload of u8 attribute or default
1844+
* @nla: u8 netlink attribute, may be %NULL
1845+
* @defvalue: default value to use if @nla is %NULL
1846+
*
1847+
* Return: the value of the attribute, or the default value if not present
1848+
*/
1849+
static inline u8 nla_get_u8_default(const struct nlattr *nla, u8 defvalue)
1850+
{
1851+
if (!nla)
1852+
return defvalue;
1853+
return nla_get_u8(nla);
1854+
}
1855+
17521856
/**
17531857
* nla_get_u64 - return payload of u64 attribute
17541858
* @nla: u64 netlink attribute
@@ -1762,6 +1866,20 @@ static inline u64 nla_get_u64(const struct nlattr *nla)
17621866
return tmp;
17631867
}
17641868

1869+
/**
1870+
* nla_get_u64_default - return payload of u64 attribute or default
1871+
* @nla: u64 netlink attribute, may be %NULL
1872+
* @defvalue: default value to use if @nla is %NULL
1873+
*
1874+
* Return: the value of the attribute, or the default value if not present
1875+
*/
1876+
static inline u64 nla_get_u64_default(const struct nlattr *nla, u64 defvalue)
1877+
{
1878+
if (!nla)
1879+
return defvalue;
1880+
return nla_get_u64(nla);
1881+
}
1882+
17651883
/**
17661884
* nla_get_uint - return payload of uint attribute
17671885
* @nla: uint netlink attribute
@@ -1773,6 +1891,20 @@ static inline u64 nla_get_uint(const struct nlattr *nla)
17731891
return nla_get_u64(nla);
17741892
}
17751893

1894+
/**
1895+
* nla_get_uint_default - return payload of uint attribute or default
1896+
* @nla: uint netlink attribute, may be %NULL
1897+
* @defvalue: default value to use if @nla is %NULL
1898+
*
1899+
* Return: the value of the attribute, or the default value if not present
1900+
*/
1901+
static inline u64 nla_get_uint_default(const struct nlattr *nla, u64 defvalue)
1902+
{
1903+
if (!nla)
1904+
return defvalue;
1905+
return nla_get_uint(nla);
1906+
}
1907+
17761908
/**
17771909
* nla_get_be64 - return payload of __be64 attribute
17781910
* @nla: __be64 netlink attribute
@@ -1786,6 +1918,21 @@ static inline __be64 nla_get_be64(const struct nlattr *nla)
17861918
return tmp;
17871919
}
17881920

1921+
/**
1922+
* nla_get_be64_default - return payload of be64 attribute or default
1923+
* @nla: __be64 netlink attribute, may be %NULL
1924+
* @defvalue: default value to use if @nla is %NULL
1925+
*
1926+
* Return: the value of the attribute, or the default value if not present
1927+
*/
1928+
static inline __be64 nla_get_be64_default(const struct nlattr *nla,
1929+
__be64 defvalue)
1930+
{
1931+
if (!nla)
1932+
return defvalue;
1933+
return nla_get_be64(nla);
1934+
}
1935+
17891936
/**
17901937
* nla_get_le64 - return payload of __le64 attribute
17911938
* @nla: __le64 netlink attribute
@@ -1795,6 +1942,21 @@ static inline __le64 nla_get_le64(const struct nlattr *nla)
17951942
return *(__le64 *) nla_data(nla);
17961943
}
17971944

1945+
/**
1946+
* nla_get_le64_default - return payload of le64 attribute or default
1947+
* @nla: __le64 netlink attribute, may be %NULL
1948+
* @defvalue: default value to use if @nla is %NULL
1949+
*
1950+
* Return: the value of the attribute, or the default value if not present
1951+
*/
1952+
static inline __le64 nla_get_le64_default(const struct nlattr *nla,
1953+
__le64 defvalue)
1954+
{
1955+
if (!nla)
1956+
return defvalue;
1957+
return nla_get_le64(nla);
1958+
}
1959+
17981960
/**
17991961
* nla_get_s32 - return payload of s32 attribute
18001962
* @nla: s32 netlink attribute
@@ -1804,6 +1966,20 @@ static inline s32 nla_get_s32(const struct nlattr *nla)
18041966
return *(s32 *) nla_data(nla);
18051967
}
18061968

1969+
/**
1970+
* nla_get_s32_default - return payload of s32 attribute or default
1971+
* @nla: s32 netlink attribute, may be %NULL
1972+
* @defvalue: default value to use if @nla is %NULL
1973+
*
1974+
* Return: the value of the attribute, or the default value if not present
1975+
*/
1976+
static inline s32 nla_get_s32_default(const struct nlattr *nla, s32 defvalue)
1977+
{
1978+
if (!nla)
1979+
return defvalue;
1980+
return nla_get_s32(nla);
1981+
}
1982+
18071983
/**
18081984
* nla_get_s16 - return payload of s16 attribute
18091985
* @nla: s16 netlink attribute
@@ -1813,6 +1989,20 @@ static inline s16 nla_get_s16(const struct nlattr *nla)
18131989
return *(s16 *) nla_data(nla);
18141990
}
18151991

1992+
/**
1993+
* nla_get_s16_default - return payload of s16 attribute or default
1994+
* @nla: s16 netlink attribute, may be %NULL
1995+
* @defvalue: default value to use if @nla is %NULL
1996+
*
1997+
* Return: the value of the attribute, or the default value if not present
1998+
*/
1999+
static inline s16 nla_get_s16_default(const struct nlattr *nla, s16 defvalue)
2000+
{
2001+
if (!nla)
2002+
return defvalue;
2003+
return nla_get_s16(nla);
2004+
}
2005+
18162006
/**
18172007
* nla_get_s8 - return payload of s8 attribute
18182008
* @nla: s8 netlink attribute
@@ -1822,6 +2012,20 @@ static inline s8 nla_get_s8(const struct nlattr *nla)
18222012
return *(s8 *) nla_data(nla);
18232013
}
18242014

2015+
/**
2016+
* nla_get_s8_default - return payload of s8 attribute or default
2017+
* @nla: s8 netlink attribute, may be %NULL
2018+
* @defvalue: default value to use if @nla is %NULL
2019+
*
2020+
* Return: the value of the attribute, or the default value if not present
2021+
*/
2022+
static inline s8 nla_get_s8_default(const struct nlattr *nla, s8 defvalue)
2023+
{
2024+
if (!nla)
2025+
return defvalue;
2026+
return nla_get_s8(nla);
2027+
}
2028+
18252029
/**
18262030
* nla_get_s64 - return payload of s64 attribute
18272031
* @nla: s64 netlink attribute
@@ -1835,6 +2039,20 @@ static inline s64 nla_get_s64(const struct nlattr *nla)
18352039
return tmp;
18362040
}
18372041

2042+
/**
2043+
* nla_get_s64_default - return payload of s64 attribute or default
2044+
* @nla: s64 netlink attribute, may be %NULL
2045+
* @defvalue: default value to use if @nla is %NULL
2046+
*
2047+
* Return: the value of the attribute, or the default value if not present
2048+
*/
2049+
static inline s64 nla_get_s64_default(const struct nlattr *nla, s64 defvalue)
2050+
{
2051+
if (!nla)
2052+
return defvalue;
2053+
return nla_get_s64(nla);
2054+
}
2055+
18382056
/**
18392057
* nla_get_sint - return payload of uint attribute
18402058
* @nla: uint netlink attribute
@@ -1846,6 +2064,20 @@ static inline s64 nla_get_sint(const struct nlattr *nla)
18462064
return nla_get_s64(nla);
18472065
}
18482066

2067+
/**
2068+
* nla_get_sint_default - return payload of sint attribute or default
2069+
* @nla: sint netlink attribute, may be %NULL
2070+
* @defvalue: default value to use if @nla is %NULL
2071+
*
2072+
* Return: the value of the attribute, or the default value if not present
2073+
*/
2074+
static inline s64 nla_get_sint_default(const struct nlattr *nla, s64 defvalue)
2075+
{
2076+
if (!nla)
2077+
return defvalue;
2078+
return nla_get_sint(nla);
2079+
}
2080+
18492081
/**
18502082
* nla_get_flag - return payload of flag attribute
18512083
* @nla: flag netlink attribute
@@ -1868,6 +2100,21 @@ static inline unsigned long nla_get_msecs(const struct nlattr *nla)
18682100
return msecs_to_jiffies((unsigned long) msecs);
18692101
}
18702102

2103+
/**
2104+
* nla_get_msecs_default - return payload of msecs attribute or default
2105+
* @nla: msecs netlink attribute, may be %NULL
2106+
* @defvalue: default value to use if @nla is %NULL
2107+
*
2108+
* Return: the value of the attribute, or the default value if not present
2109+
*/
2110+
static inline unsigned long nla_get_msecs_default(const struct nlattr *nla,
2111+
unsigned long defvalue)
2112+
{
2113+
if (!nla)
2114+
return defvalue;
2115+
return nla_get_msecs(nla);
2116+
}
2117+
18712118
/**
18722119
* nla_get_in_addr - return payload of IPv4 address attribute
18732120
* @nla: IPv4 address netlink attribute
@@ -1877,6 +2124,21 @@ static inline __be32 nla_get_in_addr(const struct nlattr *nla)
18772124
return *(__be32 *) nla_data(nla);
18782125
}
18792126

2127+
/**
2128+
* nla_get_in_addr_default - return payload of be32 attribute or default
2129+
* @nla: IPv4 address netlink attribute, may be %NULL
2130+
* @defvalue: default value to use if @nla is %NULL
2131+
*
2132+
* Return: the value of the attribute, or the default value if not present
2133+
*/
2134+
static inline __be32 nla_get_in_addr_default(const struct nlattr *nla,
2135+
__be32 defvalue)
2136+
{
2137+
if (!nla)
2138+
return defvalue;
2139+
return nla_get_in_addr(nla);
2140+
}
2141+
18802142
/**
18812143
* nla_get_in6_addr - return payload of IPv6 address attribute
18822144
* @nla: IPv6 address netlink attribute

0 commit comments

Comments
 (0)