drop_monitor.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Monitoring code for network dropped packet alerts
  4. *
  5. * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/netdevice.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/string.h>
  11. #include <linux/if_arp.h>
  12. #include <linux/inetdevice.h>
  13. #include <linux/inet.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/netpoll.h>
  16. #include <linux/sched.h>
  17. #include <linux/delay.h>
  18. #include <linux/types.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/netlink.h>
  21. #include <linux/net_dropmon.h>
  22. #include <linux/bitfield.h>
  23. #include <linux/percpu.h>
  24. #include <linux/timer.h>
  25. #include <linux/bitops.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <net/genetlink.h>
  29. #include <net/netevent.h>
  30. #include <net/flow_offload.h>
  31. #include <net/dropreason.h>
  32. #include <net/devlink.h>
  33. #include <trace/events/skb.h>
  34. #include <trace/events/napi.h>
  35. #include <trace/events/devlink.h>
  36. #include <linux/unaligned.h>
  37. #define TRACE_ON 1
  38. #define TRACE_OFF 0
  39. /*
  40. * Globals, our netlink socket pointer
  41. * and the work handle that will send up
  42. * netlink alerts
  43. */
  44. static int trace_state = TRACE_OFF;
  45. static bool monitor_hw;
  46. /* net_dm_mutex
  47. *
  48. * An overall lock guarding every operation coming from userspace.
  49. */
  50. static DEFINE_MUTEX(net_dm_mutex);
  51. struct net_dm_stats {
  52. u64_stats_t dropped;
  53. struct u64_stats_sync syncp;
  54. };
  55. #define NET_DM_MAX_HW_TRAP_NAME_LEN 40
  56. struct net_dm_hw_entry {
  57. char trap_name[NET_DM_MAX_HW_TRAP_NAME_LEN];
  58. u32 count;
  59. };
  60. struct net_dm_hw_entries {
  61. u32 num_entries;
  62. struct net_dm_hw_entry entries[];
  63. };
  64. struct per_cpu_dm_data {
  65. raw_spinlock_t lock; /* Protects 'skb', 'hw_entries' and
  66. * 'send_timer'
  67. */
  68. union {
  69. struct sk_buff *skb;
  70. struct net_dm_hw_entries *hw_entries;
  71. };
  72. struct sk_buff_head drop_queue;
  73. struct work_struct dm_alert_work;
  74. struct timer_list send_timer;
  75. struct net_dm_stats stats;
  76. };
  77. struct dm_hw_stat_delta {
  78. unsigned long last_rx;
  79. unsigned long last_drop_val;
  80. struct rcu_head rcu;
  81. };
  82. static struct genl_family net_drop_monitor_family;
  83. static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
  84. static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_hw_cpu_data);
  85. static int dm_hit_limit = 64;
  86. static int dm_delay = 1;
  87. static unsigned long dm_hw_check_delta = 2*HZ;
  88. static enum net_dm_alert_mode net_dm_alert_mode = NET_DM_ALERT_MODE_SUMMARY;
  89. static u32 net_dm_trunc_len;
  90. static u32 net_dm_queue_len = 1000;
  91. struct net_dm_alert_ops {
  92. void (*kfree_skb_probe)(void *ignore, struct sk_buff *skb,
  93. void *location,
  94. enum skb_drop_reason reason,
  95. struct sock *rx_sk);
  96. void (*napi_poll_probe)(void *ignore, struct napi_struct *napi,
  97. int work, int budget);
  98. void (*work_item_func)(struct work_struct *work);
  99. void (*hw_work_item_func)(struct work_struct *work);
  100. void (*hw_trap_probe)(void *ignore, const struct devlink *devlink,
  101. struct sk_buff *skb,
  102. const struct devlink_trap_metadata *metadata);
  103. };
  104. struct net_dm_skb_cb {
  105. union {
  106. struct devlink_trap_metadata *hw_metadata;
  107. void *pc;
  108. };
  109. enum skb_drop_reason reason;
  110. };
  111. #define NET_DM_SKB_CB(__skb) ((struct net_dm_skb_cb *)&((__skb)->cb[0]))
  112. static struct sk_buff *reset_per_cpu_data(struct per_cpu_dm_data *data)
  113. {
  114. size_t al;
  115. struct net_dm_alert_msg *msg;
  116. struct nlattr *nla;
  117. struct sk_buff *skb;
  118. unsigned long flags;
  119. void *msg_header;
  120. al = sizeof(struct net_dm_alert_msg);
  121. al += dm_hit_limit * sizeof(struct net_dm_drop_point);
  122. al += sizeof(struct nlattr);
  123. skb = genlmsg_new(al, GFP_KERNEL);
  124. if (!skb)
  125. goto err;
  126. msg_header = genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
  127. 0, NET_DM_CMD_ALERT);
  128. if (!msg_header) {
  129. nlmsg_free(skb);
  130. skb = NULL;
  131. goto err;
  132. }
  133. nla = nla_reserve(skb, NLA_UNSPEC,
  134. sizeof(struct net_dm_alert_msg));
  135. if (!nla) {
  136. nlmsg_free(skb);
  137. skb = NULL;
  138. goto err;
  139. }
  140. msg = nla_data(nla);
  141. memset(msg, 0, al);
  142. goto out;
  143. err:
  144. mod_timer(&data->send_timer, jiffies + HZ / 10);
  145. out:
  146. raw_spin_lock_irqsave(&data->lock, flags);
  147. swap(data->skb, skb);
  148. raw_spin_unlock_irqrestore(&data->lock, flags);
  149. if (skb) {
  150. struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
  151. struct genlmsghdr *gnlh = (struct genlmsghdr *)nlmsg_data(nlh);
  152. genlmsg_end(skb, genlmsg_data(gnlh));
  153. }
  154. return skb;
  155. }
  156. static const struct genl_multicast_group dropmon_mcgrps[] = {
  157. { .name = "events", .flags = GENL_MCAST_CAP_SYS_ADMIN, },
  158. };
  159. static void send_dm_alert(struct work_struct *work)
  160. {
  161. struct sk_buff *skb;
  162. struct per_cpu_dm_data *data;
  163. data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
  164. skb = reset_per_cpu_data(data);
  165. if (skb)
  166. genlmsg_multicast(&net_drop_monitor_family, skb, 0,
  167. 0, GFP_KERNEL);
  168. }
  169. /*
  170. * This is the timer function to delay the sending of an alert
  171. * in the event that more drops will arrive during the
  172. * hysteresis period.
  173. */
  174. static void sched_send_work(struct timer_list *t)
  175. {
  176. struct per_cpu_dm_data *data = timer_container_of(data, t, send_timer);
  177. schedule_work(&data->dm_alert_work);
  178. }
  179. static void trace_drop_common(struct sk_buff *skb, void *location)
  180. {
  181. struct net_dm_alert_msg *msg;
  182. struct net_dm_drop_point *point;
  183. struct nlmsghdr *nlh;
  184. struct nlattr *nla;
  185. int i;
  186. struct sk_buff *dskb;
  187. struct per_cpu_dm_data *data;
  188. unsigned long flags;
  189. local_irq_save(flags);
  190. data = this_cpu_ptr(&dm_cpu_data);
  191. raw_spin_lock(&data->lock);
  192. dskb = data->skb;
  193. if (!dskb)
  194. goto out;
  195. nlh = (struct nlmsghdr *)dskb->data;
  196. nla = genlmsg_data(nlmsg_data(nlh));
  197. msg = nla_data(nla);
  198. point = msg->points;
  199. for (i = 0; i < msg->entries; i++) {
  200. if (!memcmp(&location, &point->pc, sizeof(void *))) {
  201. point->count++;
  202. goto out;
  203. }
  204. point++;
  205. }
  206. if (msg->entries == dm_hit_limit)
  207. goto out;
  208. /*
  209. * We need to create a new entry
  210. */
  211. __nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point));
  212. nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
  213. memcpy(point->pc, &location, sizeof(void *));
  214. point->count = 1;
  215. msg->entries++;
  216. if (!timer_pending(&data->send_timer)) {
  217. data->send_timer.expires = jiffies + dm_delay * HZ;
  218. add_timer(&data->send_timer);
  219. }
  220. out:
  221. raw_spin_unlock_irqrestore(&data->lock, flags);
  222. }
  223. static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb,
  224. void *location,
  225. enum skb_drop_reason reason,
  226. struct sock *rx_sk)
  227. {
  228. trace_drop_common(skb, location);
  229. }
  230. static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi,
  231. int work, int budget)
  232. {
  233. struct net_device *dev = napi->dev;
  234. struct dm_hw_stat_delta *stat;
  235. /*
  236. * Don't check napi structures with no associated device
  237. */
  238. if (!dev)
  239. return;
  240. rcu_read_lock();
  241. stat = rcu_dereference(dev->dm_private);
  242. if (stat) {
  243. /*
  244. * only add a note to our monitor buffer if:
  245. * 1) its after the last_rx delta
  246. * 2) our rx_dropped count has gone up
  247. */
  248. if (time_after(jiffies, stat->last_rx + dm_hw_check_delta) &&
  249. (dev->stats.rx_dropped != stat->last_drop_val)) {
  250. trace_drop_common(NULL, NULL);
  251. stat->last_drop_val = dev->stats.rx_dropped;
  252. stat->last_rx = jiffies;
  253. }
  254. }
  255. rcu_read_unlock();
  256. }
  257. static struct net_dm_hw_entries *
  258. net_dm_hw_reset_per_cpu_data(struct per_cpu_dm_data *hw_data)
  259. {
  260. struct net_dm_hw_entries *hw_entries;
  261. unsigned long flags;
  262. hw_entries = kzalloc_flex(*hw_entries, entries, dm_hit_limit);
  263. if (!hw_entries) {
  264. /* If the memory allocation failed, we try to perform another
  265. * allocation in 1/10 second. Otherwise, the probe function
  266. * will constantly bail out.
  267. */
  268. mod_timer(&hw_data->send_timer, jiffies + HZ / 10);
  269. }
  270. raw_spin_lock_irqsave(&hw_data->lock, flags);
  271. swap(hw_data->hw_entries, hw_entries);
  272. raw_spin_unlock_irqrestore(&hw_data->lock, flags);
  273. return hw_entries;
  274. }
  275. static int net_dm_hw_entry_put(struct sk_buff *msg,
  276. const struct net_dm_hw_entry *hw_entry)
  277. {
  278. struct nlattr *attr;
  279. attr = nla_nest_start(msg, NET_DM_ATTR_HW_ENTRY);
  280. if (!attr)
  281. return -EMSGSIZE;
  282. if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_NAME, hw_entry->trap_name))
  283. goto nla_put_failure;
  284. if (nla_put_u32(msg, NET_DM_ATTR_HW_TRAP_COUNT, hw_entry->count))
  285. goto nla_put_failure;
  286. nla_nest_end(msg, attr);
  287. return 0;
  288. nla_put_failure:
  289. nla_nest_cancel(msg, attr);
  290. return -EMSGSIZE;
  291. }
  292. static int net_dm_hw_entries_put(struct sk_buff *msg,
  293. const struct net_dm_hw_entries *hw_entries)
  294. {
  295. struct nlattr *attr;
  296. int i;
  297. attr = nla_nest_start(msg, NET_DM_ATTR_HW_ENTRIES);
  298. if (!attr)
  299. return -EMSGSIZE;
  300. for (i = 0; i < hw_entries->num_entries; i++) {
  301. int rc;
  302. rc = net_dm_hw_entry_put(msg, &hw_entries->entries[i]);
  303. if (rc)
  304. goto nla_put_failure;
  305. }
  306. nla_nest_end(msg, attr);
  307. return 0;
  308. nla_put_failure:
  309. nla_nest_cancel(msg, attr);
  310. return -EMSGSIZE;
  311. }
  312. static int
  313. net_dm_hw_summary_report_fill(struct sk_buff *msg,
  314. const struct net_dm_hw_entries *hw_entries)
  315. {
  316. struct net_dm_alert_msg anc_hdr = { 0 };
  317. void *hdr;
  318. int rc;
  319. hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
  320. NET_DM_CMD_ALERT);
  321. if (!hdr)
  322. return -EMSGSIZE;
  323. /* We need to put the ancillary header in order not to break user
  324. * space.
  325. */
  326. if (nla_put(msg, NLA_UNSPEC, sizeof(anc_hdr), &anc_hdr))
  327. goto nla_put_failure;
  328. rc = net_dm_hw_entries_put(msg, hw_entries);
  329. if (rc)
  330. goto nla_put_failure;
  331. genlmsg_end(msg, hdr);
  332. return 0;
  333. nla_put_failure:
  334. genlmsg_cancel(msg, hdr);
  335. return -EMSGSIZE;
  336. }
  337. static void net_dm_hw_summary_work(struct work_struct *work)
  338. {
  339. struct net_dm_hw_entries *hw_entries;
  340. struct per_cpu_dm_data *hw_data;
  341. struct sk_buff *msg;
  342. int rc;
  343. hw_data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
  344. hw_entries = net_dm_hw_reset_per_cpu_data(hw_data);
  345. if (!hw_entries)
  346. return;
  347. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  348. if (!msg)
  349. goto out;
  350. rc = net_dm_hw_summary_report_fill(msg, hw_entries);
  351. if (rc) {
  352. nlmsg_free(msg);
  353. goto out;
  354. }
  355. genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
  356. out:
  357. kfree(hw_entries);
  358. }
  359. static void
  360. net_dm_hw_trap_summary_probe(void *ignore, const struct devlink *devlink,
  361. struct sk_buff *skb,
  362. const struct devlink_trap_metadata *metadata)
  363. {
  364. struct net_dm_hw_entries *hw_entries;
  365. struct net_dm_hw_entry *hw_entry;
  366. struct per_cpu_dm_data *hw_data;
  367. unsigned long flags;
  368. int i;
  369. if (metadata->trap_type == DEVLINK_TRAP_TYPE_CONTROL)
  370. return;
  371. hw_data = this_cpu_ptr(&dm_hw_cpu_data);
  372. raw_spin_lock_irqsave(&hw_data->lock, flags);
  373. hw_entries = hw_data->hw_entries;
  374. if (!hw_entries)
  375. goto out;
  376. for (i = 0; i < hw_entries->num_entries; i++) {
  377. hw_entry = &hw_entries->entries[i];
  378. if (!strncmp(hw_entry->trap_name, metadata->trap_name,
  379. NET_DM_MAX_HW_TRAP_NAME_LEN - 1)) {
  380. hw_entry->count++;
  381. goto out;
  382. }
  383. }
  384. if (WARN_ON_ONCE(hw_entries->num_entries == dm_hit_limit))
  385. goto out;
  386. hw_entry = &hw_entries->entries[hw_entries->num_entries];
  387. strscpy(hw_entry->trap_name, metadata->trap_name,
  388. NET_DM_MAX_HW_TRAP_NAME_LEN - 1);
  389. hw_entry->count = 1;
  390. hw_entries->num_entries++;
  391. if (!timer_pending(&hw_data->send_timer)) {
  392. hw_data->send_timer.expires = jiffies + dm_delay * HZ;
  393. add_timer(&hw_data->send_timer);
  394. }
  395. out:
  396. raw_spin_unlock_irqrestore(&hw_data->lock, flags);
  397. }
  398. static const struct net_dm_alert_ops net_dm_alert_summary_ops = {
  399. .kfree_skb_probe = trace_kfree_skb_hit,
  400. .napi_poll_probe = trace_napi_poll_hit,
  401. .work_item_func = send_dm_alert,
  402. .hw_work_item_func = net_dm_hw_summary_work,
  403. .hw_trap_probe = net_dm_hw_trap_summary_probe,
  404. };
  405. static void net_dm_packet_trace_kfree_skb_hit(void *ignore,
  406. struct sk_buff *skb,
  407. void *location,
  408. enum skb_drop_reason reason,
  409. struct sock *rx_sk)
  410. {
  411. ktime_t tstamp = ktime_get_real();
  412. struct per_cpu_dm_data *data;
  413. struct net_dm_skb_cb *cb;
  414. struct sk_buff *nskb;
  415. unsigned long flags;
  416. if (!skb_mac_header_was_set(skb))
  417. return;
  418. nskb = skb_clone(skb, GFP_ATOMIC);
  419. if (!nskb)
  420. return;
  421. cb = NET_DM_SKB_CB(nskb);
  422. cb->reason = reason;
  423. cb->pc = location;
  424. /* Override the timestamp because we care about the time when the
  425. * packet was dropped.
  426. */
  427. nskb->tstamp = tstamp;
  428. data = this_cpu_ptr(&dm_cpu_data);
  429. spin_lock_irqsave(&data->drop_queue.lock, flags);
  430. if (skb_queue_len(&data->drop_queue) < net_dm_queue_len)
  431. __skb_queue_tail(&data->drop_queue, nskb);
  432. else
  433. goto unlock_free;
  434. spin_unlock_irqrestore(&data->drop_queue.lock, flags);
  435. schedule_work(&data->dm_alert_work);
  436. return;
  437. unlock_free:
  438. spin_unlock_irqrestore(&data->drop_queue.lock, flags);
  439. u64_stats_update_begin(&data->stats.syncp);
  440. u64_stats_inc(&data->stats.dropped);
  441. u64_stats_update_end(&data->stats.syncp);
  442. consume_skb(nskb);
  443. }
  444. static void net_dm_packet_trace_napi_poll_hit(void *ignore,
  445. struct napi_struct *napi,
  446. int work, int budget)
  447. {
  448. }
  449. static size_t net_dm_in_port_size(void)
  450. {
  451. /* NET_DM_ATTR_IN_PORT nest */
  452. return nla_total_size(0) +
  453. /* NET_DM_ATTR_PORT_NETDEV_IFINDEX */
  454. nla_total_size(sizeof(u32)) +
  455. /* NET_DM_ATTR_PORT_NETDEV_NAME */
  456. nla_total_size(IFNAMSIZ + 1);
  457. }
  458. #define NET_DM_MAX_SYMBOL_LEN 40
  459. #define NET_DM_MAX_REASON_LEN 50
  460. static size_t net_dm_packet_report_size(size_t payload_len)
  461. {
  462. size_t size;
  463. size = nlmsg_msg_size(GENL_HDRLEN + net_drop_monitor_family.hdrsize);
  464. return NLMSG_ALIGN(size) +
  465. /* NET_DM_ATTR_ORIGIN */
  466. nla_total_size(sizeof(u16)) +
  467. /* NET_DM_ATTR_PC */
  468. nla_total_size(sizeof(u64)) +
  469. /* NET_DM_ATTR_SYMBOL */
  470. nla_total_size(NET_DM_MAX_SYMBOL_LEN + 1) +
  471. /* NET_DM_ATTR_IN_PORT */
  472. net_dm_in_port_size() +
  473. /* NET_DM_ATTR_TIMESTAMP */
  474. nla_total_size(sizeof(u64)) +
  475. /* NET_DM_ATTR_ORIG_LEN */
  476. nla_total_size(sizeof(u32)) +
  477. /* NET_DM_ATTR_PROTO */
  478. nla_total_size(sizeof(u16)) +
  479. /* NET_DM_ATTR_REASON */
  480. nla_total_size(NET_DM_MAX_REASON_LEN + 1) +
  481. /* NET_DM_ATTR_PAYLOAD */
  482. nla_total_size(payload_len);
  483. }
  484. static int net_dm_packet_report_in_port_put(struct sk_buff *msg, int ifindex,
  485. const char *name)
  486. {
  487. struct nlattr *attr;
  488. attr = nla_nest_start(msg, NET_DM_ATTR_IN_PORT);
  489. if (!attr)
  490. return -EMSGSIZE;
  491. if (ifindex &&
  492. nla_put_u32(msg, NET_DM_ATTR_PORT_NETDEV_IFINDEX, ifindex))
  493. goto nla_put_failure;
  494. if (name && nla_put_string(msg, NET_DM_ATTR_PORT_NETDEV_NAME, name))
  495. goto nla_put_failure;
  496. nla_nest_end(msg, attr);
  497. return 0;
  498. nla_put_failure:
  499. nla_nest_cancel(msg, attr);
  500. return -EMSGSIZE;
  501. }
  502. static int net_dm_packet_report_fill(struct sk_buff *msg, struct sk_buff *skb,
  503. size_t payload_len)
  504. {
  505. struct net_dm_skb_cb *cb = NET_DM_SKB_CB(skb);
  506. const struct drop_reason_list *list = NULL;
  507. unsigned int subsys, subsys_reason;
  508. char buf[NET_DM_MAX_SYMBOL_LEN];
  509. struct nlattr *attr;
  510. void *hdr;
  511. int rc;
  512. hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
  513. NET_DM_CMD_PACKET_ALERT);
  514. if (!hdr)
  515. return -EMSGSIZE;
  516. if (nla_put_u16(msg, NET_DM_ATTR_ORIGIN, NET_DM_ORIGIN_SW))
  517. goto nla_put_failure;
  518. if (nla_put_u64_64bit(msg, NET_DM_ATTR_PC, (u64)(uintptr_t)cb->pc,
  519. NET_DM_ATTR_PAD))
  520. goto nla_put_failure;
  521. rcu_read_lock();
  522. subsys = u32_get_bits(cb->reason, SKB_DROP_REASON_SUBSYS_MASK);
  523. if (subsys < SKB_DROP_REASON_SUBSYS_NUM)
  524. list = rcu_dereference(drop_reasons_by_subsys[subsys]);
  525. subsys_reason = cb->reason & ~SKB_DROP_REASON_SUBSYS_MASK;
  526. if (!list ||
  527. subsys_reason >= list->n_reasons ||
  528. !list->reasons[subsys_reason] ||
  529. strlen(list->reasons[subsys_reason]) > NET_DM_MAX_REASON_LEN) {
  530. list = rcu_dereference(drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_CORE]);
  531. subsys_reason = SKB_DROP_REASON_NOT_SPECIFIED;
  532. }
  533. if (nla_put_string(msg, NET_DM_ATTR_REASON,
  534. list->reasons[subsys_reason])) {
  535. rcu_read_unlock();
  536. goto nla_put_failure;
  537. }
  538. rcu_read_unlock();
  539. snprintf(buf, sizeof(buf), "%pS", cb->pc);
  540. if (nla_put_string(msg, NET_DM_ATTR_SYMBOL, buf))
  541. goto nla_put_failure;
  542. rc = net_dm_packet_report_in_port_put(msg, skb->skb_iif, NULL);
  543. if (rc)
  544. goto nla_put_failure;
  545. if (nla_put_u64_64bit(msg, NET_DM_ATTR_TIMESTAMP,
  546. ktime_to_ns(skb->tstamp), NET_DM_ATTR_PAD))
  547. goto nla_put_failure;
  548. if (nla_put_u32(msg, NET_DM_ATTR_ORIG_LEN, skb->len))
  549. goto nla_put_failure;
  550. if (!payload_len)
  551. goto out;
  552. if (nla_put_u16(msg, NET_DM_ATTR_PROTO, be16_to_cpu(skb->protocol)))
  553. goto nla_put_failure;
  554. attr = skb_put(msg, nla_total_size(payload_len));
  555. attr->nla_type = NET_DM_ATTR_PAYLOAD;
  556. attr->nla_len = nla_attr_size(payload_len);
  557. if (skb_copy_bits(skb, 0, nla_data(attr), payload_len))
  558. goto nla_put_failure;
  559. out:
  560. genlmsg_end(msg, hdr);
  561. return 0;
  562. nla_put_failure:
  563. genlmsg_cancel(msg, hdr);
  564. return -EMSGSIZE;
  565. }
  566. #define NET_DM_MAX_PACKET_SIZE (0xffff - NLA_HDRLEN - NLA_ALIGNTO)
  567. static void net_dm_packet_report(struct sk_buff *skb)
  568. {
  569. struct sk_buff *msg;
  570. size_t payload_len;
  571. int rc;
  572. /* Make sure we start copying the packet from the MAC header */
  573. if (skb->data > skb_mac_header(skb))
  574. skb_push(skb, skb->data - skb_mac_header(skb));
  575. else
  576. skb_pull(skb, skb_mac_header(skb) - skb->data);
  577. /* Ensure packet fits inside a single netlink attribute */
  578. payload_len = min_t(size_t, skb->len, NET_DM_MAX_PACKET_SIZE);
  579. if (net_dm_trunc_len)
  580. payload_len = min_t(size_t, net_dm_trunc_len, payload_len);
  581. msg = nlmsg_new(net_dm_packet_report_size(payload_len), GFP_KERNEL);
  582. if (!msg)
  583. goto out;
  584. rc = net_dm_packet_report_fill(msg, skb, payload_len);
  585. if (rc) {
  586. nlmsg_free(msg);
  587. goto out;
  588. }
  589. genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
  590. out:
  591. consume_skb(skb);
  592. }
  593. static void net_dm_packet_work(struct work_struct *work)
  594. {
  595. struct per_cpu_dm_data *data;
  596. struct sk_buff_head list;
  597. struct sk_buff *skb;
  598. unsigned long flags;
  599. data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
  600. __skb_queue_head_init(&list);
  601. spin_lock_irqsave(&data->drop_queue.lock, flags);
  602. skb_queue_splice_tail_init(&data->drop_queue, &list);
  603. spin_unlock_irqrestore(&data->drop_queue.lock, flags);
  604. while ((skb = __skb_dequeue(&list)))
  605. net_dm_packet_report(skb);
  606. }
  607. static size_t
  608. net_dm_flow_action_cookie_size(const struct devlink_trap_metadata *hw_metadata)
  609. {
  610. return hw_metadata->fa_cookie ?
  611. nla_total_size(hw_metadata->fa_cookie->cookie_len) : 0;
  612. }
  613. static size_t
  614. net_dm_hw_packet_report_size(size_t payload_len,
  615. const struct devlink_trap_metadata *hw_metadata)
  616. {
  617. size_t size;
  618. size = nlmsg_msg_size(GENL_HDRLEN + net_drop_monitor_family.hdrsize);
  619. return NLMSG_ALIGN(size) +
  620. /* NET_DM_ATTR_ORIGIN */
  621. nla_total_size(sizeof(u16)) +
  622. /* NET_DM_ATTR_HW_TRAP_GROUP_NAME */
  623. nla_total_size(strlen(hw_metadata->trap_group_name) + 1) +
  624. /* NET_DM_ATTR_HW_TRAP_NAME */
  625. nla_total_size(strlen(hw_metadata->trap_name) + 1) +
  626. /* NET_DM_ATTR_IN_PORT */
  627. net_dm_in_port_size() +
  628. /* NET_DM_ATTR_FLOW_ACTION_COOKIE */
  629. net_dm_flow_action_cookie_size(hw_metadata) +
  630. /* NET_DM_ATTR_TIMESTAMP */
  631. nla_total_size(sizeof(u64)) +
  632. /* NET_DM_ATTR_ORIG_LEN */
  633. nla_total_size(sizeof(u32)) +
  634. /* NET_DM_ATTR_PROTO */
  635. nla_total_size(sizeof(u16)) +
  636. /* NET_DM_ATTR_PAYLOAD */
  637. nla_total_size(payload_len);
  638. }
  639. static int net_dm_hw_packet_report_fill(struct sk_buff *msg,
  640. struct sk_buff *skb, size_t payload_len)
  641. {
  642. struct devlink_trap_metadata *hw_metadata;
  643. struct nlattr *attr;
  644. void *hdr;
  645. hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
  646. hdr = genlmsg_put(msg, 0, 0, &net_drop_monitor_family, 0,
  647. NET_DM_CMD_PACKET_ALERT);
  648. if (!hdr)
  649. return -EMSGSIZE;
  650. if (nla_put_u16(msg, NET_DM_ATTR_ORIGIN, NET_DM_ORIGIN_HW))
  651. goto nla_put_failure;
  652. if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_GROUP_NAME,
  653. hw_metadata->trap_group_name))
  654. goto nla_put_failure;
  655. if (nla_put_string(msg, NET_DM_ATTR_HW_TRAP_NAME,
  656. hw_metadata->trap_name))
  657. goto nla_put_failure;
  658. if (hw_metadata->input_dev) {
  659. struct net_device *dev = hw_metadata->input_dev;
  660. int rc;
  661. rc = net_dm_packet_report_in_port_put(msg, dev->ifindex,
  662. dev->name);
  663. if (rc)
  664. goto nla_put_failure;
  665. }
  666. if (hw_metadata->fa_cookie &&
  667. nla_put(msg, NET_DM_ATTR_FLOW_ACTION_COOKIE,
  668. hw_metadata->fa_cookie->cookie_len,
  669. hw_metadata->fa_cookie->cookie))
  670. goto nla_put_failure;
  671. if (nla_put_u64_64bit(msg, NET_DM_ATTR_TIMESTAMP,
  672. ktime_to_ns(skb->tstamp), NET_DM_ATTR_PAD))
  673. goto nla_put_failure;
  674. if (nla_put_u32(msg, NET_DM_ATTR_ORIG_LEN, skb->len))
  675. goto nla_put_failure;
  676. if (!payload_len)
  677. goto out;
  678. if (nla_put_u16(msg, NET_DM_ATTR_PROTO, be16_to_cpu(skb->protocol)))
  679. goto nla_put_failure;
  680. attr = skb_put(msg, nla_total_size(payload_len));
  681. attr->nla_type = NET_DM_ATTR_PAYLOAD;
  682. attr->nla_len = nla_attr_size(payload_len);
  683. if (skb_copy_bits(skb, 0, nla_data(attr), payload_len))
  684. goto nla_put_failure;
  685. out:
  686. genlmsg_end(msg, hdr);
  687. return 0;
  688. nla_put_failure:
  689. genlmsg_cancel(msg, hdr);
  690. return -EMSGSIZE;
  691. }
  692. static struct devlink_trap_metadata *
  693. net_dm_hw_metadata_copy(const struct devlink_trap_metadata *metadata)
  694. {
  695. const struct flow_action_cookie *fa_cookie;
  696. struct devlink_trap_metadata *hw_metadata;
  697. const char *trap_group_name;
  698. const char *trap_name;
  699. hw_metadata = kzalloc_obj(*hw_metadata, GFP_ATOMIC);
  700. if (!hw_metadata)
  701. return NULL;
  702. trap_group_name = kstrdup(metadata->trap_group_name, GFP_ATOMIC);
  703. if (!trap_group_name)
  704. goto free_hw_metadata;
  705. hw_metadata->trap_group_name = trap_group_name;
  706. trap_name = kstrdup(metadata->trap_name, GFP_ATOMIC);
  707. if (!trap_name)
  708. goto free_trap_group;
  709. hw_metadata->trap_name = trap_name;
  710. if (metadata->fa_cookie) {
  711. size_t cookie_size = sizeof(*fa_cookie) +
  712. metadata->fa_cookie->cookie_len;
  713. fa_cookie = kmemdup(metadata->fa_cookie, cookie_size,
  714. GFP_ATOMIC);
  715. if (!fa_cookie)
  716. goto free_trap_name;
  717. hw_metadata->fa_cookie = fa_cookie;
  718. }
  719. hw_metadata->input_dev = metadata->input_dev;
  720. netdev_hold(hw_metadata->input_dev, &hw_metadata->dev_tracker,
  721. GFP_ATOMIC);
  722. return hw_metadata;
  723. free_trap_name:
  724. kfree(trap_name);
  725. free_trap_group:
  726. kfree(trap_group_name);
  727. free_hw_metadata:
  728. kfree(hw_metadata);
  729. return NULL;
  730. }
  731. static void
  732. net_dm_hw_metadata_free(struct devlink_trap_metadata *hw_metadata)
  733. {
  734. netdev_put(hw_metadata->input_dev, &hw_metadata->dev_tracker);
  735. kfree(hw_metadata->fa_cookie);
  736. kfree(hw_metadata->trap_name);
  737. kfree(hw_metadata->trap_group_name);
  738. kfree(hw_metadata);
  739. }
  740. static void net_dm_hw_packet_report(struct sk_buff *skb)
  741. {
  742. struct devlink_trap_metadata *hw_metadata;
  743. struct sk_buff *msg;
  744. size_t payload_len;
  745. int rc;
  746. if (skb->data > skb_mac_header(skb))
  747. skb_push(skb, skb->data - skb_mac_header(skb));
  748. else
  749. skb_pull(skb, skb_mac_header(skb) - skb->data);
  750. payload_len = min_t(size_t, skb->len, NET_DM_MAX_PACKET_SIZE);
  751. if (net_dm_trunc_len)
  752. payload_len = min_t(size_t, net_dm_trunc_len, payload_len);
  753. hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
  754. msg = nlmsg_new(net_dm_hw_packet_report_size(payload_len, hw_metadata),
  755. GFP_KERNEL);
  756. if (!msg)
  757. goto out;
  758. rc = net_dm_hw_packet_report_fill(msg, skb, payload_len);
  759. if (rc) {
  760. nlmsg_free(msg);
  761. goto out;
  762. }
  763. genlmsg_multicast(&net_drop_monitor_family, msg, 0, 0, GFP_KERNEL);
  764. out:
  765. net_dm_hw_metadata_free(NET_DM_SKB_CB(skb)->hw_metadata);
  766. consume_skb(skb);
  767. }
  768. static void net_dm_hw_packet_work(struct work_struct *work)
  769. {
  770. struct per_cpu_dm_data *hw_data;
  771. struct sk_buff_head list;
  772. struct sk_buff *skb;
  773. unsigned long flags;
  774. hw_data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
  775. __skb_queue_head_init(&list);
  776. spin_lock_irqsave(&hw_data->drop_queue.lock, flags);
  777. skb_queue_splice_tail_init(&hw_data->drop_queue, &list);
  778. spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
  779. while ((skb = __skb_dequeue(&list)))
  780. net_dm_hw_packet_report(skb);
  781. }
  782. static void
  783. net_dm_hw_trap_packet_probe(void *ignore, const struct devlink *devlink,
  784. struct sk_buff *skb,
  785. const struct devlink_trap_metadata *metadata)
  786. {
  787. struct devlink_trap_metadata *n_hw_metadata;
  788. ktime_t tstamp = ktime_get_real();
  789. struct per_cpu_dm_data *hw_data;
  790. struct sk_buff *nskb;
  791. unsigned long flags;
  792. if (metadata->trap_type == DEVLINK_TRAP_TYPE_CONTROL)
  793. return;
  794. if (!skb_mac_header_was_set(skb))
  795. return;
  796. nskb = skb_clone(skb, GFP_ATOMIC);
  797. if (!nskb)
  798. return;
  799. n_hw_metadata = net_dm_hw_metadata_copy(metadata);
  800. if (!n_hw_metadata)
  801. goto free;
  802. NET_DM_SKB_CB(nskb)->hw_metadata = n_hw_metadata;
  803. nskb->tstamp = tstamp;
  804. hw_data = this_cpu_ptr(&dm_hw_cpu_data);
  805. spin_lock_irqsave(&hw_data->drop_queue.lock, flags);
  806. if (skb_queue_len(&hw_data->drop_queue) < net_dm_queue_len)
  807. __skb_queue_tail(&hw_data->drop_queue, nskb);
  808. else
  809. goto unlock_free;
  810. spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
  811. schedule_work(&hw_data->dm_alert_work);
  812. return;
  813. unlock_free:
  814. spin_unlock_irqrestore(&hw_data->drop_queue.lock, flags);
  815. u64_stats_update_begin(&hw_data->stats.syncp);
  816. u64_stats_inc(&hw_data->stats.dropped);
  817. u64_stats_update_end(&hw_data->stats.syncp);
  818. net_dm_hw_metadata_free(n_hw_metadata);
  819. free:
  820. consume_skb(nskb);
  821. }
  822. static const struct net_dm_alert_ops net_dm_alert_packet_ops = {
  823. .kfree_skb_probe = net_dm_packet_trace_kfree_skb_hit,
  824. .napi_poll_probe = net_dm_packet_trace_napi_poll_hit,
  825. .work_item_func = net_dm_packet_work,
  826. .hw_work_item_func = net_dm_hw_packet_work,
  827. .hw_trap_probe = net_dm_hw_trap_packet_probe,
  828. };
  829. static const struct net_dm_alert_ops *net_dm_alert_ops_arr[] = {
  830. [NET_DM_ALERT_MODE_SUMMARY] = &net_dm_alert_summary_ops,
  831. [NET_DM_ALERT_MODE_PACKET] = &net_dm_alert_packet_ops,
  832. };
  833. #if IS_ENABLED(CONFIG_NET_DEVLINK)
  834. static int net_dm_hw_probe_register(const struct net_dm_alert_ops *ops)
  835. {
  836. return register_trace_devlink_trap_report(ops->hw_trap_probe, NULL);
  837. }
  838. static void net_dm_hw_probe_unregister(const struct net_dm_alert_ops *ops)
  839. {
  840. unregister_trace_devlink_trap_report(ops->hw_trap_probe, NULL);
  841. tracepoint_synchronize_unregister();
  842. }
  843. #else
  844. static int net_dm_hw_probe_register(const struct net_dm_alert_ops *ops)
  845. {
  846. return -EOPNOTSUPP;
  847. }
  848. static void net_dm_hw_probe_unregister(const struct net_dm_alert_ops *ops)
  849. {
  850. }
  851. #endif
  852. static int net_dm_hw_monitor_start(struct netlink_ext_ack *extack)
  853. {
  854. const struct net_dm_alert_ops *ops;
  855. int cpu, rc;
  856. if (monitor_hw) {
  857. NL_SET_ERR_MSG_MOD(extack, "Hardware monitoring already enabled");
  858. return -EAGAIN;
  859. }
  860. ops = net_dm_alert_ops_arr[net_dm_alert_mode];
  861. if (!try_module_get(THIS_MODULE)) {
  862. NL_SET_ERR_MSG_MOD(extack, "Failed to take reference on module");
  863. return -ENODEV;
  864. }
  865. for_each_possible_cpu(cpu) {
  866. struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
  867. struct net_dm_hw_entries *hw_entries;
  868. INIT_WORK(&hw_data->dm_alert_work, ops->hw_work_item_func);
  869. timer_setup(&hw_data->send_timer, sched_send_work, 0);
  870. hw_entries = net_dm_hw_reset_per_cpu_data(hw_data);
  871. kfree(hw_entries);
  872. }
  873. rc = net_dm_hw_probe_register(ops);
  874. if (rc) {
  875. NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to devlink_trap_probe() tracepoint");
  876. goto err_module_put;
  877. }
  878. monitor_hw = true;
  879. return 0;
  880. err_module_put:
  881. for_each_possible_cpu(cpu) {
  882. struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
  883. struct sk_buff *skb;
  884. timer_delete_sync(&hw_data->send_timer);
  885. cancel_work_sync(&hw_data->dm_alert_work);
  886. while ((skb = __skb_dequeue(&hw_data->drop_queue))) {
  887. struct devlink_trap_metadata *hw_metadata;
  888. hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
  889. net_dm_hw_metadata_free(hw_metadata);
  890. consume_skb(skb);
  891. }
  892. }
  893. module_put(THIS_MODULE);
  894. return rc;
  895. }
  896. static void net_dm_hw_monitor_stop(struct netlink_ext_ack *extack)
  897. {
  898. const struct net_dm_alert_ops *ops;
  899. int cpu;
  900. if (!monitor_hw) {
  901. NL_SET_ERR_MSG_MOD(extack, "Hardware monitoring already disabled");
  902. return;
  903. }
  904. ops = net_dm_alert_ops_arr[net_dm_alert_mode];
  905. monitor_hw = false;
  906. net_dm_hw_probe_unregister(ops);
  907. for_each_possible_cpu(cpu) {
  908. struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
  909. struct sk_buff *skb;
  910. timer_delete_sync(&hw_data->send_timer);
  911. cancel_work_sync(&hw_data->dm_alert_work);
  912. while ((skb = __skb_dequeue(&hw_data->drop_queue))) {
  913. struct devlink_trap_metadata *hw_metadata;
  914. hw_metadata = NET_DM_SKB_CB(skb)->hw_metadata;
  915. net_dm_hw_metadata_free(hw_metadata);
  916. consume_skb(skb);
  917. }
  918. }
  919. module_put(THIS_MODULE);
  920. }
  921. static int net_dm_trace_on_set(struct netlink_ext_ack *extack)
  922. {
  923. const struct net_dm_alert_ops *ops;
  924. int cpu, rc;
  925. ops = net_dm_alert_ops_arr[net_dm_alert_mode];
  926. if (!try_module_get(THIS_MODULE)) {
  927. NL_SET_ERR_MSG_MOD(extack, "Failed to take reference on module");
  928. return -ENODEV;
  929. }
  930. for_each_possible_cpu(cpu) {
  931. struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
  932. struct sk_buff *skb;
  933. INIT_WORK(&data->dm_alert_work, ops->work_item_func);
  934. timer_setup(&data->send_timer, sched_send_work, 0);
  935. /* Allocate a new per-CPU skb for the summary alert message and
  936. * free the old one which might contain stale data from
  937. * previous tracing.
  938. */
  939. skb = reset_per_cpu_data(data);
  940. consume_skb(skb);
  941. }
  942. rc = register_trace_kfree_skb(ops->kfree_skb_probe, NULL);
  943. if (rc) {
  944. NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to kfree_skb() tracepoint");
  945. goto err_module_put;
  946. }
  947. rc = register_trace_napi_poll(ops->napi_poll_probe, NULL);
  948. if (rc) {
  949. NL_SET_ERR_MSG_MOD(extack, "Failed to connect probe to napi_poll() tracepoint");
  950. goto err_unregister_trace;
  951. }
  952. return 0;
  953. err_unregister_trace:
  954. unregister_trace_kfree_skb(ops->kfree_skb_probe, NULL);
  955. err_module_put:
  956. for_each_possible_cpu(cpu) {
  957. struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
  958. struct sk_buff *skb;
  959. timer_delete_sync(&data->send_timer);
  960. cancel_work_sync(&data->dm_alert_work);
  961. while ((skb = __skb_dequeue(&data->drop_queue)))
  962. consume_skb(skb);
  963. }
  964. module_put(THIS_MODULE);
  965. return rc;
  966. }
  967. static void net_dm_trace_off_set(void)
  968. {
  969. const struct net_dm_alert_ops *ops;
  970. int cpu;
  971. ops = net_dm_alert_ops_arr[net_dm_alert_mode];
  972. unregister_trace_napi_poll(ops->napi_poll_probe, NULL);
  973. unregister_trace_kfree_skb(ops->kfree_skb_probe, NULL);
  974. tracepoint_synchronize_unregister();
  975. /* Make sure we do not send notifications to user space after request
  976. * to stop tracing returns.
  977. */
  978. for_each_possible_cpu(cpu) {
  979. struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
  980. struct sk_buff *skb;
  981. timer_delete_sync(&data->send_timer);
  982. cancel_work_sync(&data->dm_alert_work);
  983. while ((skb = __skb_dequeue(&data->drop_queue)))
  984. consume_skb(skb);
  985. }
  986. module_put(THIS_MODULE);
  987. }
  988. static int set_all_monitor_traces(int state, struct netlink_ext_ack *extack)
  989. {
  990. int rc = 0;
  991. if (state == trace_state) {
  992. NL_SET_ERR_MSG_MOD(extack, "Trace state already set to requested state");
  993. return -EAGAIN;
  994. }
  995. switch (state) {
  996. case TRACE_ON:
  997. rc = net_dm_trace_on_set(extack);
  998. break;
  999. case TRACE_OFF:
  1000. net_dm_trace_off_set();
  1001. break;
  1002. default:
  1003. rc = 1;
  1004. break;
  1005. }
  1006. if (!rc)
  1007. trace_state = state;
  1008. else
  1009. rc = -EINPROGRESS;
  1010. return rc;
  1011. }
  1012. static bool net_dm_is_monitoring(void)
  1013. {
  1014. return trace_state == TRACE_ON || monitor_hw;
  1015. }
  1016. static int net_dm_alert_mode_get_from_info(struct genl_info *info,
  1017. enum net_dm_alert_mode *p_alert_mode)
  1018. {
  1019. u8 val;
  1020. val = nla_get_u8(info->attrs[NET_DM_ATTR_ALERT_MODE]);
  1021. switch (val) {
  1022. case NET_DM_ALERT_MODE_SUMMARY:
  1023. case NET_DM_ALERT_MODE_PACKET:
  1024. *p_alert_mode = val;
  1025. break;
  1026. default:
  1027. return -EINVAL;
  1028. }
  1029. return 0;
  1030. }
  1031. static int net_dm_alert_mode_set(struct genl_info *info)
  1032. {
  1033. struct netlink_ext_ack *extack = info->extack;
  1034. enum net_dm_alert_mode alert_mode;
  1035. int rc;
  1036. if (!info->attrs[NET_DM_ATTR_ALERT_MODE])
  1037. return 0;
  1038. rc = net_dm_alert_mode_get_from_info(info, &alert_mode);
  1039. if (rc) {
  1040. NL_SET_ERR_MSG_MOD(extack, "Invalid alert mode");
  1041. return -EINVAL;
  1042. }
  1043. net_dm_alert_mode = alert_mode;
  1044. return 0;
  1045. }
  1046. static void net_dm_trunc_len_set(struct genl_info *info)
  1047. {
  1048. if (!info->attrs[NET_DM_ATTR_TRUNC_LEN])
  1049. return;
  1050. net_dm_trunc_len = nla_get_u32(info->attrs[NET_DM_ATTR_TRUNC_LEN]);
  1051. }
  1052. static void net_dm_queue_len_set(struct genl_info *info)
  1053. {
  1054. if (!info->attrs[NET_DM_ATTR_QUEUE_LEN])
  1055. return;
  1056. net_dm_queue_len = nla_get_u32(info->attrs[NET_DM_ATTR_QUEUE_LEN]);
  1057. }
  1058. static int net_dm_cmd_config(struct sk_buff *skb,
  1059. struct genl_info *info)
  1060. {
  1061. struct netlink_ext_ack *extack = info->extack;
  1062. int rc;
  1063. if (net_dm_is_monitoring()) {
  1064. NL_SET_ERR_MSG_MOD(extack, "Cannot configure drop monitor during monitoring");
  1065. return -EBUSY;
  1066. }
  1067. rc = net_dm_alert_mode_set(info);
  1068. if (rc)
  1069. return rc;
  1070. net_dm_trunc_len_set(info);
  1071. net_dm_queue_len_set(info);
  1072. return 0;
  1073. }
  1074. static int net_dm_monitor_start(bool set_sw, bool set_hw,
  1075. struct netlink_ext_ack *extack)
  1076. {
  1077. bool sw_set = false;
  1078. int rc;
  1079. if (set_sw) {
  1080. rc = set_all_monitor_traces(TRACE_ON, extack);
  1081. if (rc)
  1082. return rc;
  1083. sw_set = true;
  1084. }
  1085. if (set_hw) {
  1086. rc = net_dm_hw_monitor_start(extack);
  1087. if (rc)
  1088. goto err_monitor_hw;
  1089. }
  1090. return 0;
  1091. err_monitor_hw:
  1092. if (sw_set)
  1093. set_all_monitor_traces(TRACE_OFF, extack);
  1094. return rc;
  1095. }
  1096. static void net_dm_monitor_stop(bool set_sw, bool set_hw,
  1097. struct netlink_ext_ack *extack)
  1098. {
  1099. if (set_hw)
  1100. net_dm_hw_monitor_stop(extack);
  1101. if (set_sw)
  1102. set_all_monitor_traces(TRACE_OFF, extack);
  1103. }
  1104. static int net_dm_cmd_trace(struct sk_buff *skb,
  1105. struct genl_info *info)
  1106. {
  1107. bool set_sw = !!info->attrs[NET_DM_ATTR_SW_DROPS];
  1108. bool set_hw = !!info->attrs[NET_DM_ATTR_HW_DROPS];
  1109. struct netlink_ext_ack *extack = info->extack;
  1110. /* To maintain backward compatibility, we start / stop monitoring of
  1111. * software drops if no flag is specified.
  1112. */
  1113. if (!set_sw && !set_hw)
  1114. set_sw = true;
  1115. switch (info->genlhdr->cmd) {
  1116. case NET_DM_CMD_START:
  1117. return net_dm_monitor_start(set_sw, set_hw, extack);
  1118. case NET_DM_CMD_STOP:
  1119. net_dm_monitor_stop(set_sw, set_hw, extack);
  1120. return 0;
  1121. }
  1122. return -EOPNOTSUPP;
  1123. }
  1124. static int net_dm_config_fill(struct sk_buff *msg, struct genl_info *info)
  1125. {
  1126. void *hdr;
  1127. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  1128. &net_drop_monitor_family, 0, NET_DM_CMD_CONFIG_NEW);
  1129. if (!hdr)
  1130. return -EMSGSIZE;
  1131. if (nla_put_u8(msg, NET_DM_ATTR_ALERT_MODE, net_dm_alert_mode))
  1132. goto nla_put_failure;
  1133. if (nla_put_u32(msg, NET_DM_ATTR_TRUNC_LEN, net_dm_trunc_len))
  1134. goto nla_put_failure;
  1135. if (nla_put_u32(msg, NET_DM_ATTR_QUEUE_LEN, net_dm_queue_len))
  1136. goto nla_put_failure;
  1137. genlmsg_end(msg, hdr);
  1138. return 0;
  1139. nla_put_failure:
  1140. genlmsg_cancel(msg, hdr);
  1141. return -EMSGSIZE;
  1142. }
  1143. static int net_dm_cmd_config_get(struct sk_buff *skb, struct genl_info *info)
  1144. {
  1145. struct sk_buff *msg;
  1146. int rc;
  1147. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1148. if (!msg)
  1149. return -ENOMEM;
  1150. rc = net_dm_config_fill(msg, info);
  1151. if (rc)
  1152. goto free_msg;
  1153. return genlmsg_reply(msg, info);
  1154. free_msg:
  1155. nlmsg_free(msg);
  1156. return rc;
  1157. }
  1158. static void net_dm_stats_read(struct net_dm_stats *stats)
  1159. {
  1160. int cpu;
  1161. memset(stats, 0, sizeof(*stats));
  1162. for_each_possible_cpu(cpu) {
  1163. struct per_cpu_dm_data *data = &per_cpu(dm_cpu_data, cpu);
  1164. struct net_dm_stats *cpu_stats = &data->stats;
  1165. unsigned int start;
  1166. u64 dropped;
  1167. do {
  1168. start = u64_stats_fetch_begin(&cpu_stats->syncp);
  1169. dropped = u64_stats_read(&cpu_stats->dropped);
  1170. } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
  1171. u64_stats_add(&stats->dropped, dropped);
  1172. }
  1173. }
  1174. static int net_dm_stats_put(struct sk_buff *msg)
  1175. {
  1176. struct net_dm_stats stats;
  1177. struct nlattr *attr;
  1178. net_dm_stats_read(&stats);
  1179. attr = nla_nest_start(msg, NET_DM_ATTR_STATS);
  1180. if (!attr)
  1181. return -EMSGSIZE;
  1182. if (nla_put_u64_64bit(msg, NET_DM_ATTR_STATS_DROPPED,
  1183. u64_stats_read(&stats.dropped), NET_DM_ATTR_PAD))
  1184. goto nla_put_failure;
  1185. nla_nest_end(msg, attr);
  1186. return 0;
  1187. nla_put_failure:
  1188. nla_nest_cancel(msg, attr);
  1189. return -EMSGSIZE;
  1190. }
  1191. static void net_dm_hw_stats_read(struct net_dm_stats *stats)
  1192. {
  1193. int cpu;
  1194. memset(stats, 0, sizeof(*stats));
  1195. for_each_possible_cpu(cpu) {
  1196. struct per_cpu_dm_data *hw_data = &per_cpu(dm_hw_cpu_data, cpu);
  1197. struct net_dm_stats *cpu_stats = &hw_data->stats;
  1198. unsigned int start;
  1199. u64 dropped;
  1200. do {
  1201. start = u64_stats_fetch_begin(&cpu_stats->syncp);
  1202. dropped = u64_stats_read(&cpu_stats->dropped);
  1203. } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
  1204. u64_stats_add(&stats->dropped, dropped);
  1205. }
  1206. }
  1207. static int net_dm_hw_stats_put(struct sk_buff *msg)
  1208. {
  1209. struct net_dm_stats stats;
  1210. struct nlattr *attr;
  1211. net_dm_hw_stats_read(&stats);
  1212. attr = nla_nest_start(msg, NET_DM_ATTR_HW_STATS);
  1213. if (!attr)
  1214. return -EMSGSIZE;
  1215. if (nla_put_u64_64bit(msg, NET_DM_ATTR_STATS_DROPPED,
  1216. u64_stats_read(&stats.dropped), NET_DM_ATTR_PAD))
  1217. goto nla_put_failure;
  1218. nla_nest_end(msg, attr);
  1219. return 0;
  1220. nla_put_failure:
  1221. nla_nest_cancel(msg, attr);
  1222. return -EMSGSIZE;
  1223. }
  1224. static int net_dm_stats_fill(struct sk_buff *msg, struct genl_info *info)
  1225. {
  1226. void *hdr;
  1227. int rc;
  1228. hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
  1229. &net_drop_monitor_family, 0, NET_DM_CMD_STATS_NEW);
  1230. if (!hdr)
  1231. return -EMSGSIZE;
  1232. rc = net_dm_stats_put(msg);
  1233. if (rc)
  1234. goto nla_put_failure;
  1235. rc = net_dm_hw_stats_put(msg);
  1236. if (rc)
  1237. goto nla_put_failure;
  1238. genlmsg_end(msg, hdr);
  1239. return 0;
  1240. nla_put_failure:
  1241. genlmsg_cancel(msg, hdr);
  1242. return -EMSGSIZE;
  1243. }
  1244. static int net_dm_cmd_stats_get(struct sk_buff *skb, struct genl_info *info)
  1245. {
  1246. struct sk_buff *msg;
  1247. int rc;
  1248. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1249. if (!msg)
  1250. return -ENOMEM;
  1251. rc = net_dm_stats_fill(msg, info);
  1252. if (rc)
  1253. goto free_msg;
  1254. return genlmsg_reply(msg, info);
  1255. free_msg:
  1256. nlmsg_free(msg);
  1257. return rc;
  1258. }
  1259. static int dropmon_net_event(struct notifier_block *ev_block,
  1260. unsigned long event, void *ptr)
  1261. {
  1262. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1263. struct dm_hw_stat_delta *stat;
  1264. switch (event) {
  1265. case NETDEV_REGISTER:
  1266. if (WARN_ON_ONCE(rtnl_dereference(dev->dm_private)))
  1267. break;
  1268. stat = kzalloc_obj(*stat);
  1269. if (!stat)
  1270. break;
  1271. stat->last_rx = jiffies;
  1272. rcu_assign_pointer(dev->dm_private, stat);
  1273. break;
  1274. case NETDEV_UNREGISTER:
  1275. stat = rtnl_dereference(dev->dm_private);
  1276. if (stat) {
  1277. rcu_assign_pointer(dev->dm_private, NULL);
  1278. kfree_rcu(stat, rcu);
  1279. }
  1280. break;
  1281. }
  1282. return NOTIFY_DONE;
  1283. }
  1284. static const struct nla_policy net_dm_nl_policy[NET_DM_ATTR_MAX + 1] = {
  1285. [NET_DM_ATTR_UNSPEC] = { .strict_start_type = NET_DM_ATTR_UNSPEC + 1 },
  1286. [NET_DM_ATTR_ALERT_MODE] = { .type = NLA_U8 },
  1287. [NET_DM_ATTR_TRUNC_LEN] = { .type = NLA_U32 },
  1288. [NET_DM_ATTR_QUEUE_LEN] = { .type = NLA_U32 },
  1289. [NET_DM_ATTR_SW_DROPS] = {. type = NLA_FLAG },
  1290. [NET_DM_ATTR_HW_DROPS] = {. type = NLA_FLAG },
  1291. };
  1292. static const struct genl_small_ops dropmon_ops[] = {
  1293. {
  1294. .cmd = NET_DM_CMD_CONFIG,
  1295. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1296. .doit = net_dm_cmd_config,
  1297. .flags = GENL_ADMIN_PERM,
  1298. },
  1299. {
  1300. .cmd = NET_DM_CMD_START,
  1301. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1302. .doit = net_dm_cmd_trace,
  1303. .flags = GENL_ADMIN_PERM,
  1304. },
  1305. {
  1306. .cmd = NET_DM_CMD_STOP,
  1307. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  1308. .doit = net_dm_cmd_trace,
  1309. .flags = GENL_ADMIN_PERM,
  1310. },
  1311. {
  1312. .cmd = NET_DM_CMD_CONFIG_GET,
  1313. .doit = net_dm_cmd_config_get,
  1314. },
  1315. {
  1316. .cmd = NET_DM_CMD_STATS_GET,
  1317. .doit = net_dm_cmd_stats_get,
  1318. },
  1319. };
  1320. static int net_dm_nl_pre_doit(const struct genl_split_ops *ops,
  1321. struct sk_buff *skb, struct genl_info *info)
  1322. {
  1323. mutex_lock(&net_dm_mutex);
  1324. return 0;
  1325. }
  1326. static void net_dm_nl_post_doit(const struct genl_split_ops *ops,
  1327. struct sk_buff *skb, struct genl_info *info)
  1328. {
  1329. mutex_unlock(&net_dm_mutex);
  1330. }
  1331. static struct genl_family net_drop_monitor_family __ro_after_init = {
  1332. .hdrsize = 0,
  1333. .name = "NET_DM",
  1334. .version = 2,
  1335. .maxattr = NET_DM_ATTR_MAX,
  1336. .policy = net_dm_nl_policy,
  1337. .pre_doit = net_dm_nl_pre_doit,
  1338. .post_doit = net_dm_nl_post_doit,
  1339. .module = THIS_MODULE,
  1340. .small_ops = dropmon_ops,
  1341. .n_small_ops = ARRAY_SIZE(dropmon_ops),
  1342. .resv_start_op = NET_DM_CMD_STATS_GET + 1,
  1343. .mcgrps = dropmon_mcgrps,
  1344. .n_mcgrps = ARRAY_SIZE(dropmon_mcgrps),
  1345. };
  1346. static struct notifier_block dropmon_net_notifier = {
  1347. .notifier_call = dropmon_net_event
  1348. };
  1349. static void __net_dm_cpu_data_init(struct per_cpu_dm_data *data)
  1350. {
  1351. raw_spin_lock_init(&data->lock);
  1352. skb_queue_head_init(&data->drop_queue);
  1353. u64_stats_init(&data->stats.syncp);
  1354. }
  1355. static void __net_dm_cpu_data_fini(struct per_cpu_dm_data *data)
  1356. {
  1357. WARN_ON(!skb_queue_empty(&data->drop_queue));
  1358. }
  1359. static void net_dm_cpu_data_init(int cpu)
  1360. {
  1361. struct per_cpu_dm_data *data;
  1362. data = &per_cpu(dm_cpu_data, cpu);
  1363. __net_dm_cpu_data_init(data);
  1364. }
  1365. static void net_dm_cpu_data_fini(int cpu)
  1366. {
  1367. struct per_cpu_dm_data *data;
  1368. data = &per_cpu(dm_cpu_data, cpu);
  1369. /* At this point, we should have exclusive access
  1370. * to this struct and can free the skb inside it.
  1371. */
  1372. consume_skb(data->skb);
  1373. __net_dm_cpu_data_fini(data);
  1374. }
  1375. static void net_dm_hw_cpu_data_init(int cpu)
  1376. {
  1377. struct per_cpu_dm_data *hw_data;
  1378. hw_data = &per_cpu(dm_hw_cpu_data, cpu);
  1379. __net_dm_cpu_data_init(hw_data);
  1380. }
  1381. static void net_dm_hw_cpu_data_fini(int cpu)
  1382. {
  1383. struct per_cpu_dm_data *hw_data;
  1384. hw_data = &per_cpu(dm_hw_cpu_data, cpu);
  1385. kfree(hw_data->hw_entries);
  1386. __net_dm_cpu_data_fini(hw_data);
  1387. }
  1388. static int __init init_net_drop_monitor(void)
  1389. {
  1390. int cpu, rc;
  1391. pr_info("Initializing network drop monitor service\n");
  1392. if (sizeof(void *) > 8) {
  1393. pr_err("Unable to store program counters on this arch, Drop monitor failed\n");
  1394. return -ENOSPC;
  1395. }
  1396. for_each_possible_cpu(cpu) {
  1397. net_dm_cpu_data_init(cpu);
  1398. net_dm_hw_cpu_data_init(cpu);
  1399. }
  1400. rc = register_netdevice_notifier(&dropmon_net_notifier);
  1401. if (rc < 0) {
  1402. pr_crit("Failed to register netdevice notifier\n");
  1403. return rc;
  1404. }
  1405. rc = genl_register_family(&net_drop_monitor_family);
  1406. if (rc) {
  1407. pr_err("Could not create drop monitor netlink family\n");
  1408. goto out_unreg;
  1409. }
  1410. WARN_ON(net_drop_monitor_family.mcgrp_offset != NET_DM_GRP_ALERT);
  1411. rc = 0;
  1412. goto out;
  1413. out_unreg:
  1414. WARN_ON(unregister_netdevice_notifier(&dropmon_net_notifier));
  1415. out:
  1416. return rc;
  1417. }
  1418. static void exit_net_drop_monitor(void)
  1419. {
  1420. int cpu;
  1421. /*
  1422. * Because of the module_get/put we do in the trace state change path
  1423. * we are guaranteed not to have any current users when we get here
  1424. */
  1425. BUG_ON(genl_unregister_family(&net_drop_monitor_family));
  1426. BUG_ON(unregister_netdevice_notifier(&dropmon_net_notifier));
  1427. for_each_possible_cpu(cpu) {
  1428. net_dm_hw_cpu_data_fini(cpu);
  1429. net_dm_cpu_data_fini(cpu);
  1430. }
  1431. }
  1432. module_init(init_net_drop_monitor);
  1433. module_exit(exit_net_drop_monitor);
  1434. MODULE_LICENSE("GPL v2");
  1435. MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
  1436. MODULE_ALIAS_GENL_FAMILY("NET_DM");
  1437. MODULE_DESCRIPTION("Monitoring code for network dropped packet alerts");