datapath.c 59 KB

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