drop_monitor.c 42 KB

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