datapath.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "meter.h"
  56. #include "vport-internal_dev.h"
  57. #include "vport-netdev.h"
  58. unsigned int ovs_net_id __read_mostly;
  59. static struct genl_family dp_packet_genl_family;
  60. static struct genl_family dp_flow_genl_family;
  61. static struct genl_family dp_datapath_genl_family;
  62. static const struct nla_policy flow_policy[];
  63. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  64. .name = OVS_FLOW_MCGROUP,
  65. };
  66. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  67. .name = OVS_DATAPATH_MCGROUP,
  68. };
  69. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  70. .name = OVS_VPORT_MCGROUP,
  71. };
  72. /* Check if need to build a reply message.
  73. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  74. static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  75. unsigned int group)
  76. {
  77. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  78. genl_has_listeners(family, genl_info_net(info), group);
  79. }
  80. static void ovs_notify(struct genl_family *family,
  81. struct sk_buff *skb, struct genl_info *info)
  82. {
  83. genl_notify(family, skb, info, 0, GFP_KERNEL);
  84. }
  85. /**
  86. * DOC: Locking:
  87. *
  88. * All writes e.g. Writes to device state (add/remove datapath, port, set
  89. * operations on vports, etc.), Writes to other state (flow table
  90. * modifications, set miscellaneous datapath parameters, etc.) are protected
  91. * by ovs_lock.
  92. *
  93. * Reads are protected by RCU.
  94. *
  95. * There are a few special cases (mostly stats) that have their own
  96. * synchronization but they nest under all of above and don't interact with
  97. * each other.
  98. *
  99. * The RTNL lock nests inside ovs_mutex.
  100. */
  101. static DEFINE_MUTEX(ovs_mutex);
  102. void ovs_lock(void)
  103. {
  104. mutex_lock(&ovs_mutex);
  105. }
  106. void ovs_unlock(void)
  107. {
  108. mutex_unlock(&ovs_mutex);
  109. }
  110. #ifdef CONFIG_LOCKDEP
  111. int lockdep_ovsl_is_held(void)
  112. {
  113. if (debug_locks)
  114. return lockdep_is_held(&ovs_mutex);
  115. else
  116. return 1;
  117. }
  118. #endif
  119. static struct vport *new_vport(const struct vport_parms *);
  120. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  121. const struct sw_flow_key *,
  122. const struct dp_upcall_info *,
  123. uint32_t cutlen);
  124. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  125. const struct sw_flow_key *,
  126. const struct dp_upcall_info *,
  127. uint32_t cutlen);
  128. /* Must be called with rcu_read_lock or ovs_mutex. */
  129. const char *ovs_dp_name(const struct datapath *dp)
  130. {
  131. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  132. return ovs_vport_name(vport);
  133. }
  134. static int get_dpifindex(const struct datapath *dp)
  135. {
  136. struct vport *local;
  137. int ifindex;
  138. rcu_read_lock();
  139. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  140. if (local)
  141. ifindex = local->dev->ifindex;
  142. else
  143. ifindex = 0;
  144. rcu_read_unlock();
  145. return ifindex;
  146. }
  147. static void destroy_dp_rcu(struct rcu_head *rcu)
  148. {
  149. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  150. ovs_flow_tbl_destroy(&dp->table);
  151. free_percpu(dp->stats_percpu);
  152. kfree(dp->ports);
  153. ovs_meters_exit(dp);
  154. kfree(dp);
  155. }
  156. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  157. u16 port_no)
  158. {
  159. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  160. }
  161. /* Called with ovs_mutex or RCU read lock. */
  162. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  163. {
  164. struct vport *vport;
  165. struct hlist_head *head;
  166. head = vport_hash_bucket(dp, port_no);
  167. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  168. if (vport->port_no == port_no)
  169. return vport;
  170. }
  171. return NULL;
  172. }
  173. /* Called with ovs_mutex. */
  174. static struct vport *new_vport(const struct vport_parms *parms)
  175. {
  176. struct vport *vport;
  177. vport = ovs_vport_add(parms);
  178. if (!IS_ERR(vport)) {
  179. struct datapath *dp = parms->dp;
  180. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  181. hlist_add_head_rcu(&vport->dp_hash_node, head);
  182. }
  183. return vport;
  184. }
  185. void ovs_dp_detach_port(struct vport *p)
  186. {
  187. ASSERT_OVSL();
  188. /* First drop references to device. */
  189. hlist_del_rcu(&p->dp_hash_node);
  190. /* Then destroy it. */
  191. ovs_vport_del(p);
  192. }
  193. /* Must be called with rcu_read_lock. */
  194. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  195. {
  196. const struct vport *p = OVS_CB(skb)->input_vport;
  197. struct datapath *dp = p->dp;
  198. struct sw_flow *flow;
  199. struct sw_flow_actions *sf_acts;
  200. struct dp_stats_percpu *stats;
  201. u64 *stats_counter;
  202. u32 n_mask_hit;
  203. stats = this_cpu_ptr(dp->stats_percpu);
  204. /* Look up flow. */
  205. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  206. if (unlikely(!flow)) {
  207. struct dp_upcall_info upcall;
  208. int error;
  209. memset(&upcall, 0, sizeof(upcall));
  210. upcall.cmd = OVS_PACKET_CMD_MISS;
  211. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  212. upcall.mru = OVS_CB(skb)->mru;
  213. error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
  214. if (unlikely(error))
  215. kfree_skb(skb);
  216. else
  217. consume_skb(skb);
  218. stats_counter = &stats->n_missed;
  219. goto out;
  220. }
  221. ovs_flow_stats_update(flow, key->tp.flags, skb);
  222. sf_acts = rcu_dereference(flow->sf_acts);
  223. ovs_execute_actions(dp, skb, sf_acts, key);
  224. stats_counter = &stats->n_hit;
  225. out:
  226. /* Update datapath statistics. */
  227. u64_stats_update_begin(&stats->syncp);
  228. (*stats_counter)++;
  229. stats->n_mask_hit += n_mask_hit;
  230. u64_stats_update_end(&stats->syncp);
  231. }
  232. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  233. const struct sw_flow_key *key,
  234. const struct dp_upcall_info *upcall_info,
  235. uint32_t cutlen)
  236. {
  237. struct dp_stats_percpu *stats;
  238. int err;
  239. if (upcall_info->portid == 0) {
  240. err = -ENOTCONN;
  241. goto err;
  242. }
  243. if (!skb_is_gso(skb))
  244. err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
  245. else
  246. err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
  247. if (err)
  248. goto err;
  249. return 0;
  250. err:
  251. stats = this_cpu_ptr(dp->stats_percpu);
  252. u64_stats_update_begin(&stats->syncp);
  253. stats->n_lost++;
  254. u64_stats_update_end(&stats->syncp);
  255. return err;
  256. }
  257. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  258. const struct sw_flow_key *key,
  259. const struct dp_upcall_info *upcall_info,
  260. uint32_t cutlen)
  261. {
  262. unsigned int gso_type = skb_shinfo(skb)->gso_type;
  263. struct sw_flow_key later_key;
  264. struct sk_buff *segs, *nskb;
  265. int err;
  266. BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
  267. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  268. if (IS_ERR(segs))
  269. return PTR_ERR(segs);
  270. if (segs == NULL)
  271. return -EINVAL;
  272. if (gso_type & SKB_GSO_UDP) {
  273. /* The initial flow key extracted by ovs_flow_key_extract()
  274. * in this case is for a first fragment, so we need to
  275. * properly mark later fragments.
  276. */
  277. later_key = *key;
  278. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  279. }
  280. /* Queue all of the segments. */
  281. skb = segs;
  282. do {
  283. if (gso_type & SKB_GSO_UDP && skb != segs)
  284. key = &later_key;
  285. err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
  286. if (err)
  287. break;
  288. } while ((skb = skb->next));
  289. /* Free all of the segments. */
  290. skb = segs;
  291. do {
  292. nskb = skb->next;
  293. if (err)
  294. kfree_skb(skb);
  295. else
  296. consume_skb(skb);
  297. } while ((skb = nskb));
  298. return err;
  299. }
  300. static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
  301. unsigned int hdrlen, int actions_attrlen)
  302. {
  303. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  304. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  305. + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
  306. + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
  307. /* OVS_PACKET_ATTR_USERDATA */
  308. if (upcall_info->userdata)
  309. size += NLA_ALIGN(upcall_info->userdata->nla_len);
  310. /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
  311. if (upcall_info->egress_tun_info)
  312. size += nla_total_size(ovs_tun_key_attr_size());
  313. /* OVS_PACKET_ATTR_ACTIONS */
  314. if (upcall_info->actions_len)
  315. size += nla_total_size(actions_attrlen);
  316. /* OVS_PACKET_ATTR_MRU */
  317. if (upcall_info->mru)
  318. size += nla_total_size(sizeof(upcall_info->mru));
  319. return size;
  320. }
  321. static void pad_packet(struct datapath *dp, struct sk_buff *skb)
  322. {
  323. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  324. size_t plen = NLA_ALIGN(skb->len) - skb->len;
  325. if (plen > 0)
  326. skb_put_zero(skb, plen);
  327. }
  328. }
  329. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  330. const struct sw_flow_key *key,
  331. const struct dp_upcall_info *upcall_info,
  332. uint32_t cutlen)
  333. {
  334. struct ovs_header *upcall;
  335. struct sk_buff *nskb = NULL;
  336. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  337. struct nlattr *nla;
  338. size_t len;
  339. unsigned int hlen;
  340. int err, dp_ifindex;
  341. dp_ifindex = get_dpifindex(dp);
  342. if (!dp_ifindex)
  343. return -ENODEV;
  344. if (skb_vlan_tag_present(skb)) {
  345. nskb = skb_clone(skb, GFP_ATOMIC);
  346. if (!nskb)
  347. return -ENOMEM;
  348. nskb = __vlan_hwaccel_push_inside(nskb);
  349. if (!nskb)
  350. return -ENOMEM;
  351. skb = nskb;
  352. }
  353. if (nla_attr_size(skb->len) > USHRT_MAX) {
  354. err = -EFBIG;
  355. goto out;
  356. }
  357. /* Complete checksum if needed */
  358. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  359. (err = skb_csum_hwoffload_help(skb, 0)))
  360. goto out;
  361. /* Older versions of OVS user space enforce alignment of the last
  362. * Netlink attribute to NLA_ALIGNTO which would require extensive
  363. * padding logic. Only perform zerocopy if padding is not required.
  364. */
  365. if (dp->user_features & OVS_DP_F_UNALIGNED)
  366. hlen = skb_zerocopy_headlen(skb);
  367. else
  368. hlen = skb->len;
  369. len = upcall_msg_size(upcall_info, hlen - cutlen,
  370. OVS_CB(skb)->acts_origlen);
  371. user_skb = genlmsg_new(len, GFP_ATOMIC);
  372. if (!user_skb) {
  373. err = -ENOMEM;
  374. goto out;
  375. }
  376. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  377. 0, upcall_info->cmd);
  378. upcall->dp_ifindex = dp_ifindex;
  379. err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
  380. BUG_ON(err);
  381. if (upcall_info->userdata)
  382. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  383. nla_len(upcall_info->userdata),
  384. nla_data(upcall_info->userdata));
  385. if (upcall_info->egress_tun_info) {
  386. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
  387. err = ovs_nla_put_tunnel_info(user_skb,
  388. upcall_info->egress_tun_info);
  389. BUG_ON(err);
  390. nla_nest_end(user_skb, nla);
  391. }
  392. if (upcall_info->actions_len) {
  393. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
  394. err = ovs_nla_put_actions(upcall_info->actions,
  395. upcall_info->actions_len,
  396. user_skb);
  397. if (!err)
  398. nla_nest_end(user_skb, nla);
  399. else
  400. nla_nest_cancel(user_skb, nla);
  401. }
  402. /* Add OVS_PACKET_ATTR_MRU */
  403. if (upcall_info->mru) {
  404. if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
  405. upcall_info->mru)) {
  406. err = -ENOBUFS;
  407. goto out;
  408. }
  409. pad_packet(dp, user_skb);
  410. }
  411. /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
  412. if (cutlen > 0) {
  413. if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
  414. skb->len)) {
  415. err = -ENOBUFS;
  416. goto out;
  417. }
  418. pad_packet(dp, user_skb);
  419. }
  420. /* Only reserve room for attribute header, packet data is added
  421. * in skb_zerocopy() */
  422. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  423. err = -ENOBUFS;
  424. goto out;
  425. }
  426. nla->nla_len = nla_attr_size(skb->len - cutlen);
  427. err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
  428. if (err)
  429. goto out;
  430. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  431. pad_packet(dp, user_skb);
  432. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  433. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  434. user_skb = NULL;
  435. out:
  436. if (err)
  437. skb_tx_error(skb);
  438. kfree_skb(user_skb);
  439. kfree_skb(nskb);
  440. return err;
  441. }
  442. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  443. {
  444. struct ovs_header *ovs_header = info->userhdr;
  445. struct net *net = sock_net(skb->sk);
  446. struct nlattr **a = info->attrs;
  447. struct sw_flow_actions *acts;
  448. struct sk_buff *packet;
  449. struct sw_flow *flow;
  450. struct sw_flow_actions *sf_acts;
  451. struct datapath *dp;
  452. struct vport *input_vport;
  453. u16 mru = 0;
  454. int len;
  455. int err;
  456. bool log = !a[OVS_PACKET_ATTR_PROBE];
  457. err = -EINVAL;
  458. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  459. !a[OVS_PACKET_ATTR_ACTIONS])
  460. goto err;
  461. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  462. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  463. err = -ENOMEM;
  464. if (!packet)
  465. goto err;
  466. skb_reserve(packet, NET_IP_ALIGN);
  467. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  468. /* Set packet's mru */
  469. if (a[OVS_PACKET_ATTR_MRU]) {
  470. mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
  471. packet->ignore_df = 1;
  472. }
  473. OVS_CB(packet)->mru = mru;
  474. /* Build an sw_flow for sending this packet. */
  475. flow = ovs_flow_alloc();
  476. err = PTR_ERR(flow);
  477. if (IS_ERR(flow))
  478. goto err_kfree_skb;
  479. err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
  480. packet, &flow->key, log);
  481. if (err)
  482. goto err_flow_free;
  483. err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
  484. &flow->key, &acts, log);
  485. if (err)
  486. goto err_flow_free;
  487. rcu_assign_pointer(flow->sf_acts, acts);
  488. packet->priority = flow->key.phy.priority;
  489. packet->mark = flow->key.phy.skb_mark;
  490. rcu_read_lock();
  491. dp = get_dp_rcu(net, ovs_header->dp_ifindex);
  492. err = -ENODEV;
  493. if (!dp)
  494. goto err_unlock;
  495. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  496. if (!input_vport)
  497. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  498. if (!input_vport)
  499. goto err_unlock;
  500. packet->dev = input_vport->dev;
  501. OVS_CB(packet)->input_vport = input_vport;
  502. sf_acts = rcu_dereference(flow->sf_acts);
  503. local_bh_disable();
  504. err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
  505. local_bh_enable();
  506. rcu_read_unlock();
  507. ovs_flow_free(flow, false);
  508. return err;
  509. err_unlock:
  510. rcu_read_unlock();
  511. err_flow_free:
  512. ovs_flow_free(flow, false);
  513. err_kfree_skb:
  514. kfree_skb(packet);
  515. err:
  516. return err;
  517. }
  518. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  519. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  520. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  521. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  522. [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
  523. [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
  524. };
  525. static const struct genl_ops dp_packet_genl_ops[] = {
  526. { .cmd = OVS_PACKET_CMD_EXECUTE,
  527. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  528. .policy = packet_policy,
  529. .doit = ovs_packet_cmd_execute
  530. }
  531. };
  532. static struct genl_family dp_packet_genl_family __ro_after_init = {
  533. .hdrsize = sizeof(struct ovs_header),
  534. .name = OVS_PACKET_FAMILY,
  535. .version = OVS_PACKET_VERSION,
  536. .maxattr = OVS_PACKET_ATTR_MAX,
  537. .netnsok = true,
  538. .parallel_ops = true,
  539. .ops = dp_packet_genl_ops,
  540. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  541. .module = THIS_MODULE,
  542. };
  543. static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
  544. struct ovs_dp_megaflow_stats *mega_stats)
  545. {
  546. int i;
  547. memset(mega_stats, 0, sizeof(*mega_stats));
  548. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  549. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  550. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  551. for_each_possible_cpu(i) {
  552. const struct dp_stats_percpu *percpu_stats;
  553. struct dp_stats_percpu local_stats;
  554. unsigned int start;
  555. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  556. do {
  557. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  558. local_stats = *percpu_stats;
  559. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  560. stats->n_hit += local_stats.n_hit;
  561. stats->n_missed += local_stats.n_missed;
  562. stats->n_lost += local_stats.n_lost;
  563. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  564. }
  565. }
  566. static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
  567. {
  568. return ovs_identifier_is_ufid(sfid) &&
  569. !(ufid_flags & OVS_UFID_F_OMIT_KEY);
  570. }
  571. static bool should_fill_mask(uint32_t ufid_flags)
  572. {
  573. return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
  574. }
  575. static bool should_fill_actions(uint32_t ufid_flags)
  576. {
  577. return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
  578. }
  579. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
  580. const struct sw_flow_id *sfid,
  581. uint32_t ufid_flags)
  582. {
  583. size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
  584. /* OVS_FLOW_ATTR_UFID */
  585. if (sfid && ovs_identifier_is_ufid(sfid))
  586. len += nla_total_size(sfid->ufid_len);
  587. /* OVS_FLOW_ATTR_KEY */
  588. if (!sfid || should_fill_key(sfid, ufid_flags))
  589. len += nla_total_size(ovs_key_attr_size());
  590. /* OVS_FLOW_ATTR_MASK */
  591. if (should_fill_mask(ufid_flags))
  592. len += nla_total_size(ovs_key_attr_size());
  593. /* OVS_FLOW_ATTR_ACTIONS */
  594. if (should_fill_actions(ufid_flags))
  595. len += nla_total_size(acts->orig_len);
  596. return len
  597. + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  598. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  599. + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
  600. }
  601. /* Called with ovs_mutex or RCU read lock. */
  602. static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
  603. struct sk_buff *skb)
  604. {
  605. struct ovs_flow_stats stats;
  606. __be16 tcp_flags;
  607. unsigned long used;
  608. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  609. if (used &&
  610. nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
  611. OVS_FLOW_ATTR_PAD))
  612. return -EMSGSIZE;
  613. if (stats.n_packets &&
  614. nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
  615. sizeof(struct ovs_flow_stats), &stats,
  616. OVS_FLOW_ATTR_PAD))
  617. return -EMSGSIZE;
  618. if ((u8)ntohs(tcp_flags) &&
  619. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  620. return -EMSGSIZE;
  621. return 0;
  622. }
  623. /* Called with ovs_mutex or RCU read lock. */
  624. static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
  625. struct sk_buff *skb, int skb_orig_len)
  626. {
  627. struct nlattr *start;
  628. int err;
  629. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  630. * this is the first flow to be dumped into 'skb'. This is unusual for
  631. * Netlink but individual action lists can be longer than
  632. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  633. * The userspace caller can always fetch the actions separately if it
  634. * really wants them. (Most userspace callers in fact don't care.)
  635. *
  636. * This can only fail for dump operations because the skb is always
  637. * properly sized for single flows.
  638. */
  639. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  640. if (start) {
  641. const struct sw_flow_actions *sf_acts;
  642. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  643. err = ovs_nla_put_actions(sf_acts->actions,
  644. sf_acts->actions_len, skb);
  645. if (!err)
  646. nla_nest_end(skb, start);
  647. else {
  648. if (skb_orig_len)
  649. return err;
  650. nla_nest_cancel(skb, start);
  651. }
  652. } else if (skb_orig_len) {
  653. return -EMSGSIZE;
  654. }
  655. return 0;
  656. }
  657. /* Called with ovs_mutex or RCU read lock. */
  658. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  659. struct sk_buff *skb, u32 portid,
  660. u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
  661. {
  662. const int skb_orig_len = skb->len;
  663. struct ovs_header *ovs_header;
  664. int err;
  665. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
  666. flags, cmd);
  667. if (!ovs_header)
  668. return -EMSGSIZE;
  669. ovs_header->dp_ifindex = dp_ifindex;
  670. err = ovs_nla_put_identifier(flow, skb);
  671. if (err)
  672. goto error;
  673. if (should_fill_key(&flow->id, ufid_flags)) {
  674. err = ovs_nla_put_masked_key(flow, skb);
  675. if (err)
  676. goto error;
  677. }
  678. if (should_fill_mask(ufid_flags)) {
  679. err = ovs_nla_put_mask(flow, skb);
  680. if (err)
  681. goto error;
  682. }
  683. err = ovs_flow_cmd_fill_stats(flow, skb);
  684. if (err)
  685. goto error;
  686. if (should_fill_actions(ufid_flags)) {
  687. err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
  688. if (err)
  689. goto error;
  690. }
  691. genlmsg_end(skb, ovs_header);
  692. return 0;
  693. error:
  694. genlmsg_cancel(skb, ovs_header);
  695. return err;
  696. }
  697. /* May not be called with RCU read lock. */
  698. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  699. const struct sw_flow_id *sfid,
  700. struct genl_info *info,
  701. bool always,
  702. uint32_t ufid_flags)
  703. {
  704. struct sk_buff *skb;
  705. size_t len;
  706. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  707. return NULL;
  708. len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
  709. skb = genlmsg_new(len, GFP_KERNEL);
  710. if (!skb)
  711. return ERR_PTR(-ENOMEM);
  712. return skb;
  713. }
  714. /* Called with ovs_mutex. */
  715. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  716. int dp_ifindex,
  717. struct genl_info *info, u8 cmd,
  718. bool always, u32 ufid_flags)
  719. {
  720. struct sk_buff *skb;
  721. int retval;
  722. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
  723. &flow->id, info, always, ufid_flags);
  724. if (IS_ERR_OR_NULL(skb))
  725. return skb;
  726. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  727. info->snd_portid, info->snd_seq, 0,
  728. cmd, ufid_flags);
  729. BUG_ON(retval < 0);
  730. return skb;
  731. }
  732. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  733. {
  734. struct net *net = sock_net(skb->sk);
  735. struct nlattr **a = info->attrs;
  736. struct ovs_header *ovs_header = info->userhdr;
  737. struct sw_flow *flow = NULL, *new_flow;
  738. struct sw_flow_mask mask;
  739. struct sk_buff *reply;
  740. struct datapath *dp;
  741. struct sw_flow_actions *acts;
  742. struct sw_flow_match match;
  743. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  744. int error;
  745. bool log = !a[OVS_FLOW_ATTR_PROBE];
  746. /* Must have key and actions. */
  747. error = -EINVAL;
  748. if (!a[OVS_FLOW_ATTR_KEY]) {
  749. OVS_NLERR(log, "Flow key attr not present in new flow.");
  750. goto error;
  751. }
  752. if (!a[OVS_FLOW_ATTR_ACTIONS]) {
  753. OVS_NLERR(log, "Flow actions attr not present in new flow.");
  754. goto error;
  755. }
  756. /* Most of the time we need to allocate a new flow, do it before
  757. * locking.
  758. */
  759. new_flow = ovs_flow_alloc();
  760. if (IS_ERR(new_flow)) {
  761. error = PTR_ERR(new_flow);
  762. goto error;
  763. }
  764. /* Extract key. */
  765. ovs_match_init(&match, &new_flow->key, false, &mask);
  766. error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
  767. a[OVS_FLOW_ATTR_MASK], log);
  768. if (error)
  769. goto err_kfree_flow;
  770. /* Extract flow identifier. */
  771. error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
  772. &new_flow->key, log);
  773. if (error)
  774. goto err_kfree_flow;
  775. /* unmasked key is needed to match when ufid is not used. */
  776. if (ovs_identifier_is_key(&new_flow->id))
  777. match.key = new_flow->id.unmasked_key;
  778. ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
  779. /* Validate actions. */
  780. error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
  781. &new_flow->key, &acts, log);
  782. if (error) {
  783. OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
  784. goto err_kfree_flow;
  785. }
  786. reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
  787. ufid_flags);
  788. if (IS_ERR(reply)) {
  789. error = PTR_ERR(reply);
  790. goto err_kfree_acts;
  791. }
  792. ovs_lock();
  793. dp = get_dp(net, ovs_header->dp_ifindex);
  794. if (unlikely(!dp)) {
  795. error = -ENODEV;
  796. goto err_unlock_ovs;
  797. }
  798. /* Check if this is a duplicate flow */
  799. if (ovs_identifier_is_ufid(&new_flow->id))
  800. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
  801. if (!flow)
  802. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
  803. if (likely(!flow)) {
  804. rcu_assign_pointer(new_flow->sf_acts, acts);
  805. /* Put flow in bucket. */
  806. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  807. if (unlikely(error)) {
  808. acts = NULL;
  809. goto err_unlock_ovs;
  810. }
  811. if (unlikely(reply)) {
  812. error = ovs_flow_cmd_fill_info(new_flow,
  813. ovs_header->dp_ifindex,
  814. reply, info->snd_portid,
  815. info->snd_seq, 0,
  816. OVS_FLOW_CMD_NEW,
  817. ufid_flags);
  818. BUG_ON(error < 0);
  819. }
  820. ovs_unlock();
  821. } else {
  822. struct sw_flow_actions *old_acts;
  823. /* Bail out if we're not allowed to modify an existing flow.
  824. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  825. * because Generic Netlink treats the latter as a dump
  826. * request. We also accept NLM_F_EXCL in case that bug ever
  827. * gets fixed.
  828. */
  829. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  830. | NLM_F_EXCL))) {
  831. error = -EEXIST;
  832. goto err_unlock_ovs;
  833. }
  834. /* The flow identifier has to be the same for flow updates.
  835. * Look for any overlapping flow.
  836. */
  837. if (unlikely(!ovs_flow_cmp(flow, &match))) {
  838. if (ovs_identifier_is_key(&flow->id))
  839. flow = ovs_flow_tbl_lookup_exact(&dp->table,
  840. &match);
  841. else /* UFID matches but key is different */
  842. flow = NULL;
  843. if (!flow) {
  844. error = -ENOENT;
  845. goto err_unlock_ovs;
  846. }
  847. }
  848. /* Update actions. */
  849. old_acts = ovsl_dereference(flow->sf_acts);
  850. rcu_assign_pointer(flow->sf_acts, acts);
  851. if (unlikely(reply)) {
  852. error = ovs_flow_cmd_fill_info(flow,
  853. ovs_header->dp_ifindex,
  854. reply, info->snd_portid,
  855. info->snd_seq, 0,
  856. OVS_FLOW_CMD_NEW,
  857. ufid_flags);
  858. BUG_ON(error < 0);
  859. }
  860. ovs_unlock();
  861. ovs_nla_free_flow_actions_rcu(old_acts);
  862. ovs_flow_free(new_flow, false);
  863. }
  864. if (reply)
  865. ovs_notify(&dp_flow_genl_family, reply, info);
  866. return 0;
  867. err_unlock_ovs:
  868. ovs_unlock();
  869. kfree_skb(reply);
  870. err_kfree_acts:
  871. ovs_nla_free_flow_actions(acts);
  872. err_kfree_flow:
  873. ovs_flow_free(new_flow, false);
  874. error:
  875. return error;
  876. }
  877. /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
  878. static struct sw_flow_actions *get_flow_actions(struct net *net,
  879. const struct nlattr *a,
  880. const struct sw_flow_key *key,
  881. const struct sw_flow_mask *mask,
  882. bool log)
  883. {
  884. struct sw_flow_actions *acts;
  885. struct sw_flow_key masked_key;
  886. int error;
  887. ovs_flow_mask_key(&masked_key, key, true, mask);
  888. error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
  889. if (error) {
  890. OVS_NLERR(log,
  891. "Actions may not be safe on all matching packets");
  892. return ERR_PTR(error);
  893. }
  894. return acts;
  895. }
  896. /* Factor out match-init and action-copy to avoid
  897. * "Wframe-larger-than=1024" warning. Because mask is only
  898. * used to get actions, we new a function to save some
  899. * stack space.
  900. *
  901. * If there are not key and action attrs, we return 0
  902. * directly. In the case, the caller will also not use the
  903. * match as before. If there is action attr, we try to get
  904. * actions and save them to *acts. Before returning from
  905. * the function, we reset the match->mask pointer. Because
  906. * we should not to return match object with dangling reference
  907. * to mask.
  908. * */
  909. static int ovs_nla_init_match_and_action(struct net *net,
  910. struct sw_flow_match *match,
  911. struct sw_flow_key *key,
  912. struct nlattr **a,
  913. struct sw_flow_actions **acts,
  914. bool log)
  915. {
  916. struct sw_flow_mask mask;
  917. int error = 0;
  918. if (a[OVS_FLOW_ATTR_KEY]) {
  919. ovs_match_init(match, key, true, &mask);
  920. error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
  921. a[OVS_FLOW_ATTR_MASK], log);
  922. if (error)
  923. goto error;
  924. }
  925. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  926. if (!a[OVS_FLOW_ATTR_KEY]) {
  927. OVS_NLERR(log,
  928. "Flow key attribute not present in set flow.");
  929. error = -EINVAL;
  930. goto error;
  931. }
  932. *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
  933. &mask, log);
  934. if (IS_ERR(*acts)) {
  935. error = PTR_ERR(*acts);
  936. goto error;
  937. }
  938. }
  939. /* On success, error is 0. */
  940. error:
  941. match->mask = NULL;
  942. return error;
  943. }
  944. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  945. {
  946. struct net *net = sock_net(skb->sk);
  947. struct nlattr **a = info->attrs;
  948. struct ovs_header *ovs_header = info->userhdr;
  949. struct sw_flow_key key;
  950. struct sw_flow *flow;
  951. struct sk_buff *reply = NULL;
  952. struct datapath *dp;
  953. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  954. struct sw_flow_match match;
  955. struct sw_flow_id sfid;
  956. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  957. int error = 0;
  958. bool log = !a[OVS_FLOW_ATTR_PROBE];
  959. bool ufid_present;
  960. ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
  961. if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
  962. OVS_NLERR(log,
  963. "Flow set message rejected, Key attribute missing.");
  964. return -EINVAL;
  965. }
  966. error = ovs_nla_init_match_and_action(net, &match, &key, a,
  967. &acts, log);
  968. if (error)
  969. goto error;
  970. if (acts) {
  971. /* Can allocate before locking if have acts. */
  972. reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
  973. ufid_flags);
  974. if (IS_ERR(reply)) {
  975. error = PTR_ERR(reply);
  976. goto err_kfree_acts;
  977. }
  978. }
  979. ovs_lock();
  980. dp = get_dp(net, ovs_header->dp_ifindex);
  981. if (unlikely(!dp)) {
  982. error = -ENODEV;
  983. goto err_unlock_ovs;
  984. }
  985. /* Check that the flow exists. */
  986. if (ufid_present)
  987. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
  988. else
  989. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  990. if (unlikely(!flow)) {
  991. error = -ENOENT;
  992. goto err_unlock_ovs;
  993. }
  994. /* Update actions, if present. */
  995. if (likely(acts)) {
  996. old_acts = ovsl_dereference(flow->sf_acts);
  997. rcu_assign_pointer(flow->sf_acts, acts);
  998. if (unlikely(reply)) {
  999. error = ovs_flow_cmd_fill_info(flow,
  1000. ovs_header->dp_ifindex,
  1001. reply, info->snd_portid,
  1002. info->snd_seq, 0,
  1003. OVS_FLOW_CMD_NEW,
  1004. ufid_flags);
  1005. BUG_ON(error < 0);
  1006. }
  1007. } else {
  1008. /* Could not alloc without acts before locking. */
  1009. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  1010. info, OVS_FLOW_CMD_NEW, false,
  1011. ufid_flags);
  1012. if (IS_ERR(reply)) {
  1013. error = PTR_ERR(reply);
  1014. goto err_unlock_ovs;
  1015. }
  1016. }
  1017. /* Clear stats. */
  1018. if (a[OVS_FLOW_ATTR_CLEAR])
  1019. ovs_flow_stats_clear(flow);
  1020. ovs_unlock();
  1021. if (reply)
  1022. ovs_notify(&dp_flow_genl_family, reply, info);
  1023. if (old_acts)
  1024. ovs_nla_free_flow_actions_rcu(old_acts);
  1025. return 0;
  1026. err_unlock_ovs:
  1027. ovs_unlock();
  1028. kfree_skb(reply);
  1029. err_kfree_acts:
  1030. ovs_nla_free_flow_actions(acts);
  1031. error:
  1032. return error;
  1033. }
  1034. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1035. {
  1036. struct nlattr **a = info->attrs;
  1037. struct ovs_header *ovs_header = info->userhdr;
  1038. struct net *net = sock_net(skb->sk);
  1039. struct sw_flow_key key;
  1040. struct sk_buff *reply;
  1041. struct sw_flow *flow;
  1042. struct datapath *dp;
  1043. struct sw_flow_match match;
  1044. struct sw_flow_id ufid;
  1045. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1046. int err = 0;
  1047. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1048. bool ufid_present;
  1049. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1050. if (a[OVS_FLOW_ATTR_KEY]) {
  1051. ovs_match_init(&match, &key, true, NULL);
  1052. err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
  1053. log);
  1054. } else if (!ufid_present) {
  1055. OVS_NLERR(log,
  1056. "Flow get message rejected, Key attribute missing.");
  1057. err = -EINVAL;
  1058. }
  1059. if (err)
  1060. return err;
  1061. ovs_lock();
  1062. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1063. if (!dp) {
  1064. err = -ENODEV;
  1065. goto unlock;
  1066. }
  1067. if (ufid_present)
  1068. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1069. else
  1070. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1071. if (!flow) {
  1072. err = -ENOENT;
  1073. goto unlock;
  1074. }
  1075. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  1076. OVS_FLOW_CMD_NEW, true, ufid_flags);
  1077. if (IS_ERR(reply)) {
  1078. err = PTR_ERR(reply);
  1079. goto unlock;
  1080. }
  1081. ovs_unlock();
  1082. return genlmsg_reply(reply, info);
  1083. unlock:
  1084. ovs_unlock();
  1085. return err;
  1086. }
  1087. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1088. {
  1089. struct nlattr **a = info->attrs;
  1090. struct ovs_header *ovs_header = info->userhdr;
  1091. struct net *net = sock_net(skb->sk);
  1092. struct sw_flow_key key;
  1093. struct sk_buff *reply;
  1094. struct sw_flow *flow = NULL;
  1095. struct datapath *dp;
  1096. struct sw_flow_match match;
  1097. struct sw_flow_id ufid;
  1098. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1099. int err;
  1100. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1101. bool ufid_present;
  1102. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1103. if (a[OVS_FLOW_ATTR_KEY]) {
  1104. ovs_match_init(&match, &key, true, NULL);
  1105. err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
  1106. NULL, log);
  1107. if (unlikely(err))
  1108. return err;
  1109. }
  1110. ovs_lock();
  1111. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1112. if (unlikely(!dp)) {
  1113. err = -ENODEV;
  1114. goto unlock;
  1115. }
  1116. if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
  1117. err = ovs_flow_tbl_flush(&dp->table);
  1118. goto unlock;
  1119. }
  1120. if (ufid_present)
  1121. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1122. else
  1123. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1124. if (unlikely(!flow)) {
  1125. err = -ENOENT;
  1126. goto unlock;
  1127. }
  1128. ovs_flow_tbl_remove(&dp->table, flow);
  1129. ovs_unlock();
  1130. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  1131. &flow->id, info, false, ufid_flags);
  1132. if (likely(reply)) {
  1133. if (likely(!IS_ERR(reply))) {
  1134. rcu_read_lock(); /*To keep RCU checker happy. */
  1135. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  1136. reply, info->snd_portid,
  1137. info->snd_seq, 0,
  1138. OVS_FLOW_CMD_DEL,
  1139. ufid_flags);
  1140. rcu_read_unlock();
  1141. BUG_ON(err < 0);
  1142. ovs_notify(&dp_flow_genl_family, reply, info);
  1143. } else {
  1144. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  1145. }
  1146. }
  1147. ovs_flow_free(flow, true);
  1148. return 0;
  1149. unlock:
  1150. ovs_unlock();
  1151. return err;
  1152. }
  1153. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1154. {
  1155. struct nlattr *a[__OVS_FLOW_ATTR_MAX];
  1156. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1157. struct table_instance *ti;
  1158. struct datapath *dp;
  1159. u32 ufid_flags;
  1160. int err;
  1161. err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
  1162. OVS_FLOW_ATTR_MAX, flow_policy, NULL);
  1163. if (err)
  1164. return err;
  1165. ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1166. rcu_read_lock();
  1167. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1168. if (!dp) {
  1169. rcu_read_unlock();
  1170. return -ENODEV;
  1171. }
  1172. ti = rcu_dereference(dp->table.ti);
  1173. for (;;) {
  1174. struct sw_flow *flow;
  1175. u32 bucket, obj;
  1176. bucket = cb->args[0];
  1177. obj = cb->args[1];
  1178. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1179. if (!flow)
  1180. break;
  1181. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1182. NETLINK_CB(cb->skb).portid,
  1183. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1184. OVS_FLOW_CMD_NEW, ufid_flags) < 0)
  1185. break;
  1186. cb->args[0] = bucket;
  1187. cb->args[1] = obj;
  1188. }
  1189. rcu_read_unlock();
  1190. return skb->len;
  1191. }
  1192. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1193. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1194. [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
  1195. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1196. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1197. [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
  1198. [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
  1199. [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
  1200. };
  1201. static const struct genl_ops dp_flow_genl_ops[] = {
  1202. { .cmd = OVS_FLOW_CMD_NEW,
  1203. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1204. .policy = flow_policy,
  1205. .doit = ovs_flow_cmd_new
  1206. },
  1207. { .cmd = OVS_FLOW_CMD_DEL,
  1208. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1209. .policy = flow_policy,
  1210. .doit = ovs_flow_cmd_del
  1211. },
  1212. { .cmd = OVS_FLOW_CMD_GET,
  1213. .flags = 0, /* OK for unprivileged users. */
  1214. .policy = flow_policy,
  1215. .doit = ovs_flow_cmd_get,
  1216. .dumpit = ovs_flow_cmd_dump
  1217. },
  1218. { .cmd = OVS_FLOW_CMD_SET,
  1219. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1220. .policy = flow_policy,
  1221. .doit = ovs_flow_cmd_set,
  1222. },
  1223. };
  1224. static struct genl_family dp_flow_genl_family __ro_after_init = {
  1225. .hdrsize = sizeof(struct ovs_header),
  1226. .name = OVS_FLOW_FAMILY,
  1227. .version = OVS_FLOW_VERSION,
  1228. .maxattr = OVS_FLOW_ATTR_MAX,
  1229. .netnsok = true,
  1230. .parallel_ops = true,
  1231. .ops = dp_flow_genl_ops,
  1232. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1233. .mcgrps = &ovs_dp_flow_multicast_group,
  1234. .n_mcgrps = 1,
  1235. .module = THIS_MODULE,
  1236. };
  1237. static size_t ovs_dp_cmd_msg_size(void)
  1238. {
  1239. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1240. msgsize += nla_total_size(IFNAMSIZ);
  1241. msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
  1242. msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
  1243. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1244. return msgsize;
  1245. }
  1246. /* Called with ovs_mutex. */
  1247. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1248. u32 portid, u32 seq, u32 flags, u8 cmd)
  1249. {
  1250. struct ovs_header *ovs_header;
  1251. struct ovs_dp_stats dp_stats;
  1252. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1253. int err;
  1254. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1255. flags, cmd);
  1256. if (!ovs_header)
  1257. goto error;
  1258. ovs_header->dp_ifindex = get_dpifindex(dp);
  1259. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1260. if (err)
  1261. goto nla_put_failure;
  1262. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1263. if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1264. &dp_stats, OVS_DP_ATTR_PAD))
  1265. goto nla_put_failure;
  1266. if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1267. sizeof(struct ovs_dp_megaflow_stats),
  1268. &dp_megaflow_stats, OVS_DP_ATTR_PAD))
  1269. goto nla_put_failure;
  1270. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1271. goto nla_put_failure;
  1272. genlmsg_end(skb, ovs_header);
  1273. return 0;
  1274. nla_put_failure:
  1275. genlmsg_cancel(skb, ovs_header);
  1276. error:
  1277. return -EMSGSIZE;
  1278. }
  1279. static struct sk_buff *ovs_dp_cmd_alloc_info(void)
  1280. {
  1281. return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  1282. }
  1283. /* Called with rcu_read_lock or ovs_mutex. */
  1284. static struct datapath *lookup_datapath(struct net *net,
  1285. const struct ovs_header *ovs_header,
  1286. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1287. {
  1288. struct datapath *dp;
  1289. if (!a[OVS_DP_ATTR_NAME])
  1290. dp = get_dp(net, ovs_header->dp_ifindex);
  1291. else {
  1292. struct vport *vport;
  1293. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1294. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1295. }
  1296. return dp ? dp : ERR_PTR(-ENODEV);
  1297. }
  1298. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1299. {
  1300. struct datapath *dp;
  1301. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1302. if (IS_ERR(dp))
  1303. return;
  1304. WARN(dp->user_features, "Dropping previously announced user features\n");
  1305. dp->user_features = 0;
  1306. }
  1307. static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
  1308. {
  1309. if (a[OVS_DP_ATTR_USER_FEATURES])
  1310. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1311. }
  1312. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1313. {
  1314. struct nlattr **a = info->attrs;
  1315. struct vport_parms parms;
  1316. struct sk_buff *reply;
  1317. struct datapath *dp;
  1318. struct vport *vport;
  1319. struct ovs_net *ovs_net;
  1320. int err, i;
  1321. err = -EINVAL;
  1322. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1323. goto err;
  1324. reply = ovs_dp_cmd_alloc_info();
  1325. if (!reply)
  1326. return -ENOMEM;
  1327. err = -ENOMEM;
  1328. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1329. if (dp == NULL)
  1330. goto err_free_reply;
  1331. ovs_dp_set_net(dp, sock_net(skb->sk));
  1332. /* Allocate table. */
  1333. err = ovs_flow_tbl_init(&dp->table);
  1334. if (err)
  1335. goto err_free_dp;
  1336. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1337. if (!dp->stats_percpu) {
  1338. err = -ENOMEM;
  1339. goto err_destroy_table;
  1340. }
  1341. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1342. GFP_KERNEL);
  1343. if (!dp->ports) {
  1344. err = -ENOMEM;
  1345. goto err_destroy_percpu;
  1346. }
  1347. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1348. INIT_HLIST_HEAD(&dp->ports[i]);
  1349. err = ovs_meters_init(dp);
  1350. if (err)
  1351. goto err_destroy_ports_array;
  1352. /* Set up our datapath device. */
  1353. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1354. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1355. parms.options = NULL;
  1356. parms.dp = dp;
  1357. parms.port_no = OVSP_LOCAL;
  1358. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1359. ovs_dp_change(dp, a);
  1360. /* So far only local changes have been made, now need the lock. */
  1361. ovs_lock();
  1362. vport = new_vport(&parms);
  1363. if (IS_ERR(vport)) {
  1364. err = PTR_ERR(vport);
  1365. if (err == -EBUSY)
  1366. err = -EEXIST;
  1367. if (err == -EEXIST) {
  1368. /* An outdated user space instance that does not understand
  1369. * the concept of user_features has attempted to create a new
  1370. * datapath and is likely to reuse it. Drop all user features.
  1371. */
  1372. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1373. ovs_dp_reset_user_features(skb, info);
  1374. }
  1375. goto err_destroy_meters;
  1376. }
  1377. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1378. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1379. BUG_ON(err < 0);
  1380. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1381. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1382. ovs_unlock();
  1383. ovs_notify(&dp_datapath_genl_family, reply, info);
  1384. return 0;
  1385. err_destroy_meters:
  1386. ovs_unlock();
  1387. ovs_meters_exit(dp);
  1388. err_destroy_ports_array:
  1389. kfree(dp->ports);
  1390. err_destroy_percpu:
  1391. free_percpu(dp->stats_percpu);
  1392. err_destroy_table:
  1393. ovs_flow_tbl_destroy(&dp->table);
  1394. err_free_dp:
  1395. kfree(dp);
  1396. err_free_reply:
  1397. kfree_skb(reply);
  1398. err:
  1399. return err;
  1400. }
  1401. /* Called with ovs_mutex. */
  1402. static void __dp_destroy(struct datapath *dp)
  1403. {
  1404. int i;
  1405. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1406. struct vport *vport;
  1407. struct hlist_node *n;
  1408. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1409. if (vport->port_no != OVSP_LOCAL)
  1410. ovs_dp_detach_port(vport);
  1411. }
  1412. list_del_rcu(&dp->list_node);
  1413. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1414. * all ports in datapath are destroyed first before freeing datapath.
  1415. */
  1416. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1417. /* RCU destroy the flow table */
  1418. call_rcu(&dp->rcu, destroy_dp_rcu);
  1419. }
  1420. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1421. {
  1422. struct sk_buff *reply;
  1423. struct datapath *dp;
  1424. int err;
  1425. reply = ovs_dp_cmd_alloc_info();
  1426. if (!reply)
  1427. return -ENOMEM;
  1428. ovs_lock();
  1429. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1430. err = PTR_ERR(dp);
  1431. if (IS_ERR(dp))
  1432. goto err_unlock_free;
  1433. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1434. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1435. BUG_ON(err < 0);
  1436. __dp_destroy(dp);
  1437. ovs_unlock();
  1438. ovs_notify(&dp_datapath_genl_family, reply, info);
  1439. return 0;
  1440. err_unlock_free:
  1441. ovs_unlock();
  1442. kfree_skb(reply);
  1443. return err;
  1444. }
  1445. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1446. {
  1447. struct sk_buff *reply;
  1448. struct datapath *dp;
  1449. int err;
  1450. reply = ovs_dp_cmd_alloc_info();
  1451. if (!reply)
  1452. return -ENOMEM;
  1453. ovs_lock();
  1454. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1455. err = PTR_ERR(dp);
  1456. if (IS_ERR(dp))
  1457. goto err_unlock_free;
  1458. ovs_dp_change(dp, info->attrs);
  1459. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1460. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1461. BUG_ON(err < 0);
  1462. ovs_unlock();
  1463. ovs_notify(&dp_datapath_genl_family, reply, info);
  1464. return 0;
  1465. err_unlock_free:
  1466. ovs_unlock();
  1467. kfree_skb(reply);
  1468. return err;
  1469. }
  1470. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1471. {
  1472. struct sk_buff *reply;
  1473. struct datapath *dp;
  1474. int err;
  1475. reply = ovs_dp_cmd_alloc_info();
  1476. if (!reply)
  1477. return -ENOMEM;
  1478. ovs_lock();
  1479. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1480. if (IS_ERR(dp)) {
  1481. err = PTR_ERR(dp);
  1482. goto err_unlock_free;
  1483. }
  1484. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1485. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1486. BUG_ON(err < 0);
  1487. ovs_unlock();
  1488. return genlmsg_reply(reply, info);
  1489. err_unlock_free:
  1490. ovs_unlock();
  1491. kfree_skb(reply);
  1492. return err;
  1493. }
  1494. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1495. {
  1496. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1497. struct datapath *dp;
  1498. int skip = cb->args[0];
  1499. int i = 0;
  1500. ovs_lock();
  1501. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1502. if (i >= skip &&
  1503. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1504. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1505. OVS_DP_CMD_NEW) < 0)
  1506. break;
  1507. i++;
  1508. }
  1509. ovs_unlock();
  1510. cb->args[0] = i;
  1511. return skb->len;
  1512. }
  1513. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1514. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1515. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1516. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1517. };
  1518. static const struct genl_ops dp_datapath_genl_ops[] = {
  1519. { .cmd = OVS_DP_CMD_NEW,
  1520. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1521. .policy = datapath_policy,
  1522. .doit = ovs_dp_cmd_new
  1523. },
  1524. { .cmd = OVS_DP_CMD_DEL,
  1525. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1526. .policy = datapath_policy,
  1527. .doit = ovs_dp_cmd_del
  1528. },
  1529. { .cmd = OVS_DP_CMD_GET,
  1530. .flags = 0, /* OK for unprivileged users. */
  1531. .policy = datapath_policy,
  1532. .doit = ovs_dp_cmd_get,
  1533. .dumpit = ovs_dp_cmd_dump
  1534. },
  1535. { .cmd = OVS_DP_CMD_SET,
  1536. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1537. .policy = datapath_policy,
  1538. .doit = ovs_dp_cmd_set,
  1539. },
  1540. };
  1541. static struct genl_family dp_datapath_genl_family __ro_after_init = {
  1542. .hdrsize = sizeof(struct ovs_header),
  1543. .name = OVS_DATAPATH_FAMILY,
  1544. .version = OVS_DATAPATH_VERSION,
  1545. .maxattr = OVS_DP_ATTR_MAX,
  1546. .netnsok = true,
  1547. .parallel_ops = true,
  1548. .ops = dp_datapath_genl_ops,
  1549. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1550. .mcgrps = &ovs_dp_datapath_multicast_group,
  1551. .n_mcgrps = 1,
  1552. .module = THIS_MODULE,
  1553. };
  1554. /* Called with ovs_mutex or RCU read lock. */
  1555. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1556. struct net *net, u32 portid, u32 seq,
  1557. u32 flags, u8 cmd)
  1558. {
  1559. struct ovs_header *ovs_header;
  1560. struct ovs_vport_stats vport_stats;
  1561. int err;
  1562. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1563. flags, cmd);
  1564. if (!ovs_header)
  1565. return -EMSGSIZE;
  1566. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1567. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1568. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1569. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1570. ovs_vport_name(vport)) ||
  1571. nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
  1572. goto nla_put_failure;
  1573. if (!net_eq(net, dev_net(vport->dev))) {
  1574. int id = peernet2id_alloc(net, dev_net(vport->dev));
  1575. if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
  1576. goto nla_put_failure;
  1577. }
  1578. ovs_vport_get_stats(vport, &vport_stats);
  1579. if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
  1580. sizeof(struct ovs_vport_stats), &vport_stats,
  1581. OVS_VPORT_ATTR_PAD))
  1582. goto nla_put_failure;
  1583. if (ovs_vport_get_upcall_portids(vport, skb))
  1584. goto nla_put_failure;
  1585. err = ovs_vport_get_options(vport, skb);
  1586. if (err == -EMSGSIZE)
  1587. goto error;
  1588. genlmsg_end(skb, ovs_header);
  1589. return 0;
  1590. nla_put_failure:
  1591. err = -EMSGSIZE;
  1592. error:
  1593. genlmsg_cancel(skb, ovs_header);
  1594. return err;
  1595. }
  1596. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1597. {
  1598. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1599. }
  1600. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1601. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
  1602. u32 portid, u32 seq, u8 cmd)
  1603. {
  1604. struct sk_buff *skb;
  1605. int retval;
  1606. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1607. if (!skb)
  1608. return ERR_PTR(-ENOMEM);
  1609. retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd);
  1610. BUG_ON(retval < 0);
  1611. return skb;
  1612. }
  1613. /* Called with ovs_mutex or RCU read lock. */
  1614. static struct vport *lookup_vport(struct net *net,
  1615. const struct ovs_header *ovs_header,
  1616. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1617. {
  1618. struct datapath *dp;
  1619. struct vport *vport;
  1620. if (a[OVS_VPORT_ATTR_IFINDEX])
  1621. return ERR_PTR(-EOPNOTSUPP);
  1622. if (a[OVS_VPORT_ATTR_NAME]) {
  1623. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1624. if (!vport)
  1625. return ERR_PTR(-ENODEV);
  1626. if (ovs_header->dp_ifindex &&
  1627. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1628. return ERR_PTR(-ENODEV);
  1629. return vport;
  1630. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1631. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1632. if (port_no >= DP_MAX_PORTS)
  1633. return ERR_PTR(-EFBIG);
  1634. dp = get_dp(net, ovs_header->dp_ifindex);
  1635. if (!dp)
  1636. return ERR_PTR(-ENODEV);
  1637. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1638. if (!vport)
  1639. return ERR_PTR(-ENODEV);
  1640. return vport;
  1641. } else
  1642. return ERR_PTR(-EINVAL);
  1643. }
  1644. /* Called with ovs_mutex */
  1645. static void update_headroom(struct datapath *dp)
  1646. {
  1647. unsigned dev_headroom, max_headroom = 0;
  1648. struct net_device *dev;
  1649. struct vport *vport;
  1650. int i;
  1651. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1652. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1653. dev = vport->dev;
  1654. dev_headroom = netdev_get_fwd_headroom(dev);
  1655. if (dev_headroom > max_headroom)
  1656. max_headroom = dev_headroom;
  1657. }
  1658. }
  1659. dp->max_headroom = max_headroom;
  1660. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1661. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
  1662. netdev_set_rx_headroom(vport->dev, max_headroom);
  1663. }
  1664. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1665. {
  1666. struct nlattr **a = info->attrs;
  1667. struct ovs_header *ovs_header = info->userhdr;
  1668. struct vport_parms parms;
  1669. struct sk_buff *reply;
  1670. struct vport *vport;
  1671. struct datapath *dp;
  1672. u32 port_no;
  1673. int err;
  1674. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1675. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1676. return -EINVAL;
  1677. if (a[OVS_VPORT_ATTR_IFINDEX])
  1678. return -EOPNOTSUPP;
  1679. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1680. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1681. if (port_no >= DP_MAX_PORTS)
  1682. return -EFBIG;
  1683. reply = ovs_vport_cmd_alloc_info();
  1684. if (!reply)
  1685. return -ENOMEM;
  1686. ovs_lock();
  1687. restart:
  1688. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1689. err = -ENODEV;
  1690. if (!dp)
  1691. goto exit_unlock_free;
  1692. if (port_no) {
  1693. vport = ovs_vport_ovsl(dp, port_no);
  1694. err = -EBUSY;
  1695. if (vport)
  1696. goto exit_unlock_free;
  1697. } else {
  1698. for (port_no = 1; ; port_no++) {
  1699. if (port_no >= DP_MAX_PORTS) {
  1700. err = -EFBIG;
  1701. goto exit_unlock_free;
  1702. }
  1703. vport = ovs_vport_ovsl(dp, port_no);
  1704. if (!vport)
  1705. break;
  1706. }
  1707. }
  1708. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1709. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1710. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1711. parms.dp = dp;
  1712. parms.port_no = port_no;
  1713. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1714. vport = new_vport(&parms);
  1715. err = PTR_ERR(vport);
  1716. if (IS_ERR(vport)) {
  1717. if (err == -EAGAIN)
  1718. goto restart;
  1719. goto exit_unlock_free;
  1720. }
  1721. err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
  1722. info->snd_portid, info->snd_seq, 0,
  1723. OVS_VPORT_CMD_NEW);
  1724. if (netdev_get_fwd_headroom(vport->dev) > dp->max_headroom)
  1725. update_headroom(dp);
  1726. else
  1727. netdev_set_rx_headroom(vport->dev, dp->max_headroom);
  1728. BUG_ON(err < 0);
  1729. ovs_unlock();
  1730. ovs_notify(&dp_vport_genl_family, reply, info);
  1731. return 0;
  1732. exit_unlock_free:
  1733. ovs_unlock();
  1734. kfree_skb(reply);
  1735. return err;
  1736. }
  1737. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1738. {
  1739. struct nlattr **a = info->attrs;
  1740. struct sk_buff *reply;
  1741. struct vport *vport;
  1742. int err;
  1743. reply = ovs_vport_cmd_alloc_info();
  1744. if (!reply)
  1745. return -ENOMEM;
  1746. ovs_lock();
  1747. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1748. err = PTR_ERR(vport);
  1749. if (IS_ERR(vport))
  1750. goto exit_unlock_free;
  1751. if (a[OVS_VPORT_ATTR_TYPE] &&
  1752. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1753. err = -EINVAL;
  1754. goto exit_unlock_free;
  1755. }
  1756. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1757. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1758. if (err)
  1759. goto exit_unlock_free;
  1760. }
  1761. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1762. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1763. err = ovs_vport_set_upcall_portids(vport, ids);
  1764. if (err)
  1765. goto exit_unlock_free;
  1766. }
  1767. err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
  1768. info->snd_portid, info->snd_seq, 0,
  1769. OVS_VPORT_CMD_NEW);
  1770. BUG_ON(err < 0);
  1771. ovs_unlock();
  1772. ovs_notify(&dp_vport_genl_family, reply, info);
  1773. return 0;
  1774. exit_unlock_free:
  1775. ovs_unlock();
  1776. kfree_skb(reply);
  1777. return err;
  1778. }
  1779. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1780. {
  1781. bool must_update_headroom = false;
  1782. struct nlattr **a = info->attrs;
  1783. struct sk_buff *reply;
  1784. struct datapath *dp;
  1785. struct vport *vport;
  1786. int err;
  1787. reply = ovs_vport_cmd_alloc_info();
  1788. if (!reply)
  1789. return -ENOMEM;
  1790. ovs_lock();
  1791. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1792. err = PTR_ERR(vport);
  1793. if (IS_ERR(vport))
  1794. goto exit_unlock_free;
  1795. if (vport->port_no == OVSP_LOCAL) {
  1796. err = -EINVAL;
  1797. goto exit_unlock_free;
  1798. }
  1799. err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
  1800. info->snd_portid, info->snd_seq, 0,
  1801. OVS_VPORT_CMD_DEL);
  1802. BUG_ON(err < 0);
  1803. /* the vport deletion may trigger dp headroom update */
  1804. dp = vport->dp;
  1805. if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
  1806. must_update_headroom = true;
  1807. netdev_reset_rx_headroom(vport->dev);
  1808. ovs_dp_detach_port(vport);
  1809. if (must_update_headroom)
  1810. update_headroom(dp);
  1811. ovs_unlock();
  1812. ovs_notify(&dp_vport_genl_family, reply, info);
  1813. return 0;
  1814. exit_unlock_free:
  1815. ovs_unlock();
  1816. kfree_skb(reply);
  1817. return err;
  1818. }
  1819. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1820. {
  1821. struct nlattr **a = info->attrs;
  1822. struct ovs_header *ovs_header = info->userhdr;
  1823. struct sk_buff *reply;
  1824. struct vport *vport;
  1825. int err;
  1826. reply = ovs_vport_cmd_alloc_info();
  1827. if (!reply)
  1828. return -ENOMEM;
  1829. rcu_read_lock();
  1830. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1831. err = PTR_ERR(vport);
  1832. if (IS_ERR(vport))
  1833. goto exit_unlock_free;
  1834. err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
  1835. info->snd_portid, info->snd_seq, 0,
  1836. OVS_VPORT_CMD_NEW);
  1837. BUG_ON(err < 0);
  1838. rcu_read_unlock();
  1839. return genlmsg_reply(reply, info);
  1840. exit_unlock_free:
  1841. rcu_read_unlock();
  1842. kfree_skb(reply);
  1843. return err;
  1844. }
  1845. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1846. {
  1847. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1848. struct datapath *dp;
  1849. int bucket = cb->args[0], skip = cb->args[1];
  1850. int i, j = 0;
  1851. rcu_read_lock();
  1852. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1853. if (!dp) {
  1854. rcu_read_unlock();
  1855. return -ENODEV;
  1856. }
  1857. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1858. struct vport *vport;
  1859. j = 0;
  1860. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1861. if (j >= skip &&
  1862. ovs_vport_cmd_fill_info(vport, skb,
  1863. sock_net(skb->sk),
  1864. NETLINK_CB(cb->skb).portid,
  1865. cb->nlh->nlmsg_seq,
  1866. NLM_F_MULTI,
  1867. OVS_VPORT_CMD_NEW) < 0)
  1868. goto out;
  1869. j++;
  1870. }
  1871. skip = 0;
  1872. }
  1873. out:
  1874. rcu_read_unlock();
  1875. cb->args[0] = i;
  1876. cb->args[1] = j;
  1877. return skb->len;
  1878. }
  1879. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1880. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1881. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1882. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1883. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1884. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1885. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1886. [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
  1887. [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
  1888. };
  1889. static const struct genl_ops dp_vport_genl_ops[] = {
  1890. { .cmd = OVS_VPORT_CMD_NEW,
  1891. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1892. .policy = vport_policy,
  1893. .doit = ovs_vport_cmd_new
  1894. },
  1895. { .cmd = OVS_VPORT_CMD_DEL,
  1896. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1897. .policy = vport_policy,
  1898. .doit = ovs_vport_cmd_del
  1899. },
  1900. { .cmd = OVS_VPORT_CMD_GET,
  1901. .flags = 0, /* OK for unprivileged users. */
  1902. .policy = vport_policy,
  1903. .doit = ovs_vport_cmd_get,
  1904. .dumpit = ovs_vport_cmd_dump
  1905. },
  1906. { .cmd = OVS_VPORT_CMD_SET,
  1907. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1908. .policy = vport_policy,
  1909. .doit = ovs_vport_cmd_set,
  1910. },
  1911. };
  1912. struct genl_family dp_vport_genl_family __ro_after_init = {
  1913. .hdrsize = sizeof(struct ovs_header),
  1914. .name = OVS_VPORT_FAMILY,
  1915. .version = OVS_VPORT_VERSION,
  1916. .maxattr = OVS_VPORT_ATTR_MAX,
  1917. .netnsok = true,
  1918. .parallel_ops = true,
  1919. .ops = dp_vport_genl_ops,
  1920. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1921. .mcgrps = &ovs_dp_vport_multicast_group,
  1922. .n_mcgrps = 1,
  1923. .module = THIS_MODULE,
  1924. };
  1925. static struct genl_family * const dp_genl_families[] = {
  1926. &dp_datapath_genl_family,
  1927. &dp_vport_genl_family,
  1928. &dp_flow_genl_family,
  1929. &dp_packet_genl_family,
  1930. &dp_meter_genl_family,
  1931. };
  1932. static void dp_unregister_genl(int n_families)
  1933. {
  1934. int i;
  1935. for (i = 0; i < n_families; i++)
  1936. genl_unregister_family(dp_genl_families[i]);
  1937. }
  1938. static int __init dp_register_genl(void)
  1939. {
  1940. int err;
  1941. int i;
  1942. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1943. err = genl_register_family(dp_genl_families[i]);
  1944. if (err)
  1945. goto error;
  1946. }
  1947. return 0;
  1948. error:
  1949. dp_unregister_genl(i);
  1950. return err;
  1951. }
  1952. static int __net_init ovs_init_net(struct net *net)
  1953. {
  1954. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1955. INIT_LIST_HEAD(&ovs_net->dps);
  1956. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1957. ovs_ct_init(net);
  1958. return 0;
  1959. }
  1960. static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
  1961. struct list_head *head)
  1962. {
  1963. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1964. struct datapath *dp;
  1965. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1966. int i;
  1967. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1968. struct vport *vport;
  1969. hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
  1970. if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
  1971. continue;
  1972. if (dev_net(vport->dev) == dnet)
  1973. list_add(&vport->detach_list, head);
  1974. }
  1975. }
  1976. }
  1977. }
  1978. static void __net_exit ovs_exit_net(struct net *dnet)
  1979. {
  1980. struct datapath *dp, *dp_next;
  1981. struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
  1982. struct vport *vport, *vport_next;
  1983. struct net *net;
  1984. LIST_HEAD(head);
  1985. ovs_ct_exit(dnet);
  1986. ovs_lock();
  1987. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1988. __dp_destroy(dp);
  1989. rtnl_lock();
  1990. down_read(&net_rwsem);
  1991. for_each_net(net)
  1992. list_vports_from_net(net, dnet, &head);
  1993. up_read(&net_rwsem);
  1994. rtnl_unlock();
  1995. /* Detach all vports from given namespace. */
  1996. list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
  1997. list_del(&vport->detach_list);
  1998. ovs_dp_detach_port(vport);
  1999. }
  2000. ovs_unlock();
  2001. cancel_work_sync(&ovs_net->dp_notify_work);
  2002. }
  2003. static struct pernet_operations ovs_net_ops = {
  2004. .init = ovs_init_net,
  2005. .exit = ovs_exit_net,
  2006. .id = &ovs_net_id,
  2007. .size = sizeof(struct ovs_net),
  2008. };
  2009. static int __init dp_init(void)
  2010. {
  2011. int err;
  2012. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  2013. pr_info("Open vSwitch switching datapath\n");
  2014. err = action_fifos_init();
  2015. if (err)
  2016. goto error;
  2017. err = ovs_internal_dev_rtnl_link_register();
  2018. if (err)
  2019. goto error_action_fifos_exit;
  2020. err = ovs_flow_init();
  2021. if (err)
  2022. goto error_unreg_rtnl_link;
  2023. err = ovs_vport_init();
  2024. if (err)
  2025. goto error_flow_exit;
  2026. err = register_pernet_device(&ovs_net_ops);
  2027. if (err)
  2028. goto error_vport_exit;
  2029. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  2030. if (err)
  2031. goto error_netns_exit;
  2032. err = ovs_netdev_init();
  2033. if (err)
  2034. goto error_unreg_notifier;
  2035. err = dp_register_genl();
  2036. if (err < 0)
  2037. goto error_unreg_netdev;
  2038. return 0;
  2039. error_unreg_netdev:
  2040. ovs_netdev_exit();
  2041. error_unreg_notifier:
  2042. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2043. error_netns_exit:
  2044. unregister_pernet_device(&ovs_net_ops);
  2045. error_vport_exit:
  2046. ovs_vport_exit();
  2047. error_flow_exit:
  2048. ovs_flow_exit();
  2049. error_unreg_rtnl_link:
  2050. ovs_internal_dev_rtnl_link_unregister();
  2051. error_action_fifos_exit:
  2052. action_fifos_exit();
  2053. error:
  2054. return err;
  2055. }
  2056. static void dp_cleanup(void)
  2057. {
  2058. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  2059. ovs_netdev_exit();
  2060. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2061. unregister_pernet_device(&ovs_net_ops);
  2062. rcu_barrier();
  2063. ovs_vport_exit();
  2064. ovs_flow_exit();
  2065. ovs_internal_dev_rtnl_link_unregister();
  2066. action_fifos_exit();
  2067. }
  2068. module_init(dp_init);
  2069. module_exit(dp_cleanup);
  2070. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  2071. MODULE_LICENSE("GPL");
  2072. MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
  2073. MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
  2074. MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
  2075. MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
  2076. MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);