datapath.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  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. int ovs_net_id __read_mostly;
  58. EXPORT_SYMBOL_GPL(ovs_net_id);
  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. EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
  119. #endif
  120. static struct vport *new_vport(const struct vport_parms *);
  121. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  122. const struct sw_flow_key *,
  123. const struct dp_upcall_info *,
  124. uint32_t cutlen);
  125. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  126. const struct sw_flow_key *,
  127. const struct dp_upcall_info *,
  128. uint32_t cutlen);
  129. /* Must be called with rcu_read_lock. */
  130. static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
  131. {
  132. struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
  133. if (dev) {
  134. struct vport *vport = ovs_internal_dev_get_vport(dev);
  135. if (vport)
  136. return vport->dp;
  137. }
  138. return NULL;
  139. }
  140. /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
  141. * returned dp pointer valid.
  142. */
  143. static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
  144. {
  145. struct datapath *dp;
  146. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  147. rcu_read_lock();
  148. dp = get_dp_rcu(net, dp_ifindex);
  149. rcu_read_unlock();
  150. return dp;
  151. }
  152. /* Must be called with rcu_read_lock or ovs_mutex. */
  153. const char *ovs_dp_name(const struct datapath *dp)
  154. {
  155. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  156. return ovs_vport_name(vport);
  157. }
  158. static int get_dpifindex(const struct datapath *dp)
  159. {
  160. struct vport *local;
  161. int ifindex;
  162. rcu_read_lock();
  163. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  164. if (local)
  165. ifindex = local->dev->ifindex;
  166. else
  167. ifindex = 0;
  168. rcu_read_unlock();
  169. return ifindex;
  170. }
  171. static void destroy_dp_rcu(struct rcu_head *rcu)
  172. {
  173. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  174. ovs_flow_tbl_destroy(&dp->table);
  175. free_percpu(dp->stats_percpu);
  176. kfree(dp->ports);
  177. kfree(dp);
  178. }
  179. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  180. u16 port_no)
  181. {
  182. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  183. }
  184. /* Called with ovs_mutex or RCU read lock. */
  185. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  186. {
  187. struct vport *vport;
  188. struct hlist_head *head;
  189. head = vport_hash_bucket(dp, port_no);
  190. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  191. if (vport->port_no == port_no)
  192. return vport;
  193. }
  194. return NULL;
  195. }
  196. /* Called with ovs_mutex. */
  197. static struct vport *new_vport(const struct vport_parms *parms)
  198. {
  199. struct vport *vport;
  200. vport = ovs_vport_add(parms);
  201. if (!IS_ERR(vport)) {
  202. struct datapath *dp = parms->dp;
  203. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  204. hlist_add_head_rcu(&vport->dp_hash_node, head);
  205. }
  206. return vport;
  207. }
  208. void ovs_dp_detach_port(struct vport *p)
  209. {
  210. ASSERT_OVSL();
  211. /* First drop references to device. */
  212. hlist_del_rcu(&p->dp_hash_node);
  213. /* Then destroy it. */
  214. ovs_vport_del(p);
  215. }
  216. /* Must be called with rcu_read_lock. */
  217. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  218. {
  219. const struct vport *p = OVS_CB(skb)->input_vport;
  220. struct datapath *dp = p->dp;
  221. struct sw_flow *flow;
  222. struct sw_flow_actions *sf_acts;
  223. struct dp_stats_percpu *stats;
  224. u64 *stats_counter;
  225. u32 n_mask_hit;
  226. stats = this_cpu_ptr(dp->stats_percpu);
  227. /* Look up flow. */
  228. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  229. if (unlikely(!flow)) {
  230. struct dp_upcall_info upcall;
  231. int error;
  232. memset(&upcall, 0, sizeof(upcall));
  233. upcall.cmd = OVS_PACKET_CMD_MISS;
  234. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  235. upcall.mru = OVS_CB(skb)->mru;
  236. error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
  237. if (unlikely(error))
  238. kfree_skb(skb);
  239. else
  240. consume_skb(skb);
  241. stats_counter = &stats->n_missed;
  242. goto out;
  243. }
  244. ovs_flow_stats_update(flow, key->tp.flags, skb);
  245. sf_acts = rcu_dereference(flow->sf_acts);
  246. ovs_execute_actions(dp, skb, sf_acts, key);
  247. stats_counter = &stats->n_hit;
  248. out:
  249. /* Update datapath statistics. */
  250. u64_stats_update_begin(&stats->syncp);
  251. (*stats_counter)++;
  252. stats->n_mask_hit += n_mask_hit;
  253. u64_stats_update_end(&stats->syncp);
  254. }
  255. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  256. const struct sw_flow_key *key,
  257. const struct dp_upcall_info *upcall_info,
  258. uint32_t cutlen)
  259. {
  260. struct dp_stats_percpu *stats;
  261. int err;
  262. if (upcall_info->portid == 0) {
  263. err = -ENOTCONN;
  264. goto err;
  265. }
  266. if (!skb_is_gso(skb))
  267. err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
  268. else
  269. err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
  270. if (err)
  271. goto err;
  272. return 0;
  273. err:
  274. stats = this_cpu_ptr(dp->stats_percpu);
  275. u64_stats_update_begin(&stats->syncp);
  276. stats->n_lost++;
  277. u64_stats_update_end(&stats->syncp);
  278. return err;
  279. }
  280. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  281. const struct sw_flow_key *key,
  282. const struct dp_upcall_info *upcall_info,
  283. uint32_t cutlen)
  284. {
  285. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  286. struct sw_flow_key later_key;
  287. struct sk_buff *segs, *nskb;
  288. int err;
  289. BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
  290. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  291. if (IS_ERR(segs))
  292. return PTR_ERR(segs);
  293. if (segs == NULL)
  294. return -EINVAL;
  295. if (gso_type & SKB_GSO_UDP) {
  296. /* The initial flow key extracted by ovs_flow_key_extract()
  297. * in this case is for a first fragment, so we need to
  298. * properly mark later fragments.
  299. */
  300. later_key = *key;
  301. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  302. }
  303. /* Queue all of the segments. */
  304. skb = segs;
  305. do {
  306. if (gso_type & SKB_GSO_UDP && skb != segs)
  307. key = &later_key;
  308. err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
  309. if (err)
  310. break;
  311. } while ((skb = skb->next));
  312. /* Free all of the segments. */
  313. skb = segs;
  314. do {
  315. nskb = skb->next;
  316. if (err)
  317. kfree_skb(skb);
  318. else
  319. consume_skb(skb);
  320. } while ((skb = nskb));
  321. return err;
  322. }
  323. static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
  324. unsigned int hdrlen)
  325. {
  326. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  327. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  328. + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
  329. + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
  330. /* OVS_PACKET_ATTR_USERDATA */
  331. if (upcall_info->userdata)
  332. size += NLA_ALIGN(upcall_info->userdata->nla_len);
  333. /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
  334. if (upcall_info->egress_tun_info)
  335. size += nla_total_size(ovs_tun_key_attr_size());
  336. /* OVS_PACKET_ATTR_ACTIONS */
  337. if (upcall_info->actions_len)
  338. size += nla_total_size(upcall_info->actions_len);
  339. /* OVS_PACKET_ATTR_MRU */
  340. if (upcall_info->mru)
  341. size += nla_total_size(sizeof(upcall_info->mru));
  342. return size;
  343. }
  344. static void pad_packet(struct datapath *dp, struct sk_buff *skb)
  345. {
  346. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  347. size_t plen = NLA_ALIGN(skb->len) - skb->len;
  348. if (plen > 0)
  349. memset(skb_put(skb, plen), 0, plen);
  350. }
  351. }
  352. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  353. const struct sw_flow_key *key,
  354. const struct dp_upcall_info *upcall_info,
  355. uint32_t cutlen)
  356. {
  357. struct ovs_header *upcall;
  358. struct sk_buff *nskb = NULL;
  359. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  360. struct nlattr *nla;
  361. size_t len;
  362. unsigned int hlen;
  363. int err, dp_ifindex;
  364. dp_ifindex = get_dpifindex(dp);
  365. if (!dp_ifindex)
  366. return -ENODEV;
  367. if (skb_vlan_tag_present(skb)) {
  368. nskb = skb_clone(skb, GFP_ATOMIC);
  369. if (!nskb)
  370. return -ENOMEM;
  371. nskb = __vlan_hwaccel_push_inside(nskb);
  372. if (!nskb)
  373. return -ENOMEM;
  374. skb = nskb;
  375. }
  376. if (nla_attr_size(skb->len) > USHRT_MAX) {
  377. err = -EFBIG;
  378. goto out;
  379. }
  380. /* Complete checksum if needed */
  381. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  382. (err = skb_checksum_help(skb)))
  383. goto out;
  384. /* Older versions of OVS user space enforce alignment of the last
  385. * Netlink attribute to NLA_ALIGNTO which would require extensive
  386. * padding logic. Only perform zerocopy if padding is not required.
  387. */
  388. if (dp->user_features & OVS_DP_F_UNALIGNED)
  389. hlen = skb_zerocopy_headlen(skb);
  390. else
  391. hlen = skb->len;
  392. len = upcall_msg_size(upcall_info, hlen - cutlen);
  393. user_skb = genlmsg_new(len, GFP_ATOMIC);
  394. if (!user_skb) {
  395. err = -ENOMEM;
  396. goto out;
  397. }
  398. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  399. 0, upcall_info->cmd);
  400. upcall->dp_ifindex = dp_ifindex;
  401. err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
  402. BUG_ON(err);
  403. if (upcall_info->userdata)
  404. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  405. nla_len(upcall_info->userdata),
  406. nla_data(upcall_info->userdata));
  407. if (upcall_info->egress_tun_info) {
  408. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
  409. err = ovs_nla_put_tunnel_info(user_skb,
  410. upcall_info->egress_tun_info);
  411. BUG_ON(err);
  412. nla_nest_end(user_skb, nla);
  413. }
  414. if (upcall_info->actions_len) {
  415. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
  416. err = ovs_nla_put_actions(upcall_info->actions,
  417. upcall_info->actions_len,
  418. user_skb);
  419. if (!err)
  420. nla_nest_end(user_skb, nla);
  421. else
  422. nla_nest_cancel(user_skb, nla);
  423. }
  424. /* Add OVS_PACKET_ATTR_MRU */
  425. if (upcall_info->mru) {
  426. if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
  427. upcall_info->mru)) {
  428. err = -ENOBUFS;
  429. goto out;
  430. }
  431. pad_packet(dp, user_skb);
  432. }
  433. /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
  434. if (cutlen > 0) {
  435. if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
  436. skb->len)) {
  437. err = -ENOBUFS;
  438. goto out;
  439. }
  440. pad_packet(dp, user_skb);
  441. }
  442. /* Only reserve room for attribute header, packet data is added
  443. * in skb_zerocopy() */
  444. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  445. err = -ENOBUFS;
  446. goto out;
  447. }
  448. nla->nla_len = nla_attr_size(skb->len - cutlen);
  449. err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
  450. if (err)
  451. goto out;
  452. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  453. pad_packet(dp, user_skb);
  454. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  455. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  456. user_skb = NULL;
  457. out:
  458. if (err)
  459. skb_tx_error(skb);
  460. kfree_skb(user_skb);
  461. kfree_skb(nskb);
  462. return err;
  463. }
  464. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  465. {
  466. struct ovs_header *ovs_header = info->userhdr;
  467. struct net *net = sock_net(skb->sk);
  468. struct nlattr **a = info->attrs;
  469. struct sw_flow_actions *acts;
  470. struct sk_buff *packet;
  471. struct sw_flow *flow;
  472. struct sw_flow_actions *sf_acts;
  473. struct datapath *dp;
  474. struct ethhdr *eth;
  475. struct vport *input_vport;
  476. u16 mru = 0;
  477. int len;
  478. int err;
  479. bool log = !a[OVS_PACKET_ATTR_PROBE];
  480. err = -EINVAL;
  481. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  482. !a[OVS_PACKET_ATTR_ACTIONS])
  483. goto err;
  484. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  485. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  486. err = -ENOMEM;
  487. if (!packet)
  488. goto err;
  489. skb_reserve(packet, NET_IP_ALIGN);
  490. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  491. skb_reset_mac_header(packet);
  492. eth = eth_hdr(packet);
  493. /* Normally, setting the skb 'protocol' field would be handled by a
  494. * call to eth_type_trans(), but it assumes there's a sending
  495. * device, which we may not have. */
  496. if (eth_proto_is_802_3(eth->h_proto))
  497. packet->protocol = eth->h_proto;
  498. else
  499. packet->protocol = htons(ETH_P_802_2);
  500. /* Set packet's mru */
  501. if (a[OVS_PACKET_ATTR_MRU]) {
  502. mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
  503. packet->ignore_df = 1;
  504. }
  505. OVS_CB(packet)->mru = mru;
  506. /* Build an sw_flow for sending this packet. */
  507. flow = ovs_flow_alloc();
  508. err = PTR_ERR(flow);
  509. if (IS_ERR(flow))
  510. goto err_kfree_skb;
  511. err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
  512. packet, &flow->key, log);
  513. if (err)
  514. goto err_flow_free;
  515. err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
  516. &flow->key, &acts, log);
  517. if (err)
  518. goto err_flow_free;
  519. rcu_assign_pointer(flow->sf_acts, acts);
  520. packet->priority = flow->key.phy.priority;
  521. packet->mark = flow->key.phy.skb_mark;
  522. rcu_read_lock();
  523. dp = get_dp_rcu(net, ovs_header->dp_ifindex);
  524. err = -ENODEV;
  525. if (!dp)
  526. goto err_unlock;
  527. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  528. if (!input_vport)
  529. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  530. if (!input_vport)
  531. goto err_unlock;
  532. packet->dev = input_vport->dev;
  533. OVS_CB(packet)->input_vport = input_vport;
  534. sf_acts = rcu_dereference(flow->sf_acts);
  535. local_bh_disable();
  536. err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
  537. local_bh_enable();
  538. rcu_read_unlock();
  539. ovs_flow_free(flow, false);
  540. return err;
  541. err_unlock:
  542. rcu_read_unlock();
  543. err_flow_free:
  544. ovs_flow_free(flow, false);
  545. err_kfree_skb:
  546. kfree_skb(packet);
  547. err:
  548. return err;
  549. }
  550. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  551. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  552. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  553. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  554. [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
  555. [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
  556. };
  557. static const struct genl_ops dp_packet_genl_ops[] = {
  558. { .cmd = OVS_PACKET_CMD_EXECUTE,
  559. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  560. .policy = packet_policy,
  561. .doit = ovs_packet_cmd_execute
  562. }
  563. };
  564. static struct genl_family dp_packet_genl_family = {
  565. .id = GENL_ID_GENERATE,
  566. .hdrsize = sizeof(struct ovs_header),
  567. .name = OVS_PACKET_FAMILY,
  568. .version = OVS_PACKET_VERSION,
  569. .maxattr = OVS_PACKET_ATTR_MAX,
  570. .netnsok = true,
  571. .parallel_ops = true,
  572. .ops = dp_packet_genl_ops,
  573. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  574. };
  575. static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
  576. struct ovs_dp_megaflow_stats *mega_stats)
  577. {
  578. int i;
  579. memset(mega_stats, 0, sizeof(*mega_stats));
  580. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  581. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  582. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  583. for_each_possible_cpu(i) {
  584. const struct dp_stats_percpu *percpu_stats;
  585. struct dp_stats_percpu local_stats;
  586. unsigned int start;
  587. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  588. do {
  589. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  590. local_stats = *percpu_stats;
  591. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  592. stats->n_hit += local_stats.n_hit;
  593. stats->n_missed += local_stats.n_missed;
  594. stats->n_lost += local_stats.n_lost;
  595. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  596. }
  597. }
  598. static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
  599. {
  600. return ovs_identifier_is_ufid(sfid) &&
  601. !(ufid_flags & OVS_UFID_F_OMIT_KEY);
  602. }
  603. static bool should_fill_mask(uint32_t ufid_flags)
  604. {
  605. return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
  606. }
  607. static bool should_fill_actions(uint32_t ufid_flags)
  608. {
  609. return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
  610. }
  611. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
  612. const struct sw_flow_id *sfid,
  613. uint32_t ufid_flags)
  614. {
  615. size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
  616. /* OVS_FLOW_ATTR_UFID */
  617. if (sfid && ovs_identifier_is_ufid(sfid))
  618. len += nla_total_size(sfid->ufid_len);
  619. /* OVS_FLOW_ATTR_KEY */
  620. if (!sfid || should_fill_key(sfid, ufid_flags))
  621. len += nla_total_size(ovs_key_attr_size());
  622. /* OVS_FLOW_ATTR_MASK */
  623. if (should_fill_mask(ufid_flags))
  624. len += nla_total_size(ovs_key_attr_size());
  625. /* OVS_FLOW_ATTR_ACTIONS */
  626. if (should_fill_actions(ufid_flags))
  627. len += nla_total_size(acts->orig_len);
  628. return len
  629. + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  630. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  631. + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
  632. }
  633. /* Called with ovs_mutex or RCU read lock. */
  634. static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
  635. struct sk_buff *skb)
  636. {
  637. struct ovs_flow_stats stats;
  638. __be16 tcp_flags;
  639. unsigned long used;
  640. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  641. if (used &&
  642. nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
  643. OVS_FLOW_ATTR_PAD))
  644. return -EMSGSIZE;
  645. if (stats.n_packets &&
  646. nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
  647. sizeof(struct ovs_flow_stats), &stats,
  648. OVS_FLOW_ATTR_PAD))
  649. return -EMSGSIZE;
  650. if ((u8)ntohs(tcp_flags) &&
  651. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  652. return -EMSGSIZE;
  653. return 0;
  654. }
  655. /* Called with ovs_mutex or RCU read lock. */
  656. static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
  657. struct sk_buff *skb, int skb_orig_len)
  658. {
  659. struct nlattr *start;
  660. int err;
  661. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  662. * this is the first flow to be dumped into 'skb'. This is unusual for
  663. * Netlink but individual action lists can be longer than
  664. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  665. * The userspace caller can always fetch the actions separately if it
  666. * really wants them. (Most userspace callers in fact don't care.)
  667. *
  668. * This can only fail for dump operations because the skb is always
  669. * properly sized for single flows.
  670. */
  671. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  672. if (start) {
  673. const struct sw_flow_actions *sf_acts;
  674. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  675. err = ovs_nla_put_actions(sf_acts->actions,
  676. sf_acts->actions_len, skb);
  677. if (!err)
  678. nla_nest_end(skb, start);
  679. else {
  680. if (skb_orig_len)
  681. return err;
  682. nla_nest_cancel(skb, start);
  683. }
  684. } else if (skb_orig_len) {
  685. return -EMSGSIZE;
  686. }
  687. return 0;
  688. }
  689. /* Called with ovs_mutex or RCU read lock. */
  690. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  691. struct sk_buff *skb, u32 portid,
  692. u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
  693. {
  694. const int skb_orig_len = skb->len;
  695. struct ovs_header *ovs_header;
  696. int err;
  697. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
  698. flags, cmd);
  699. if (!ovs_header)
  700. return -EMSGSIZE;
  701. ovs_header->dp_ifindex = dp_ifindex;
  702. err = ovs_nla_put_identifier(flow, skb);
  703. if (err)
  704. goto error;
  705. if (should_fill_key(&flow->id, ufid_flags)) {
  706. err = ovs_nla_put_masked_key(flow, skb);
  707. if (err)
  708. goto error;
  709. }
  710. if (should_fill_mask(ufid_flags)) {
  711. err = ovs_nla_put_mask(flow, skb);
  712. if (err)
  713. goto error;
  714. }
  715. err = ovs_flow_cmd_fill_stats(flow, skb);
  716. if (err)
  717. goto error;
  718. if (should_fill_actions(ufid_flags)) {
  719. err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
  720. if (err)
  721. goto error;
  722. }
  723. genlmsg_end(skb, ovs_header);
  724. return 0;
  725. error:
  726. genlmsg_cancel(skb, ovs_header);
  727. return err;
  728. }
  729. /* May not be called with RCU read lock. */
  730. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  731. const struct sw_flow_id *sfid,
  732. struct genl_info *info,
  733. bool always,
  734. uint32_t ufid_flags)
  735. {
  736. struct sk_buff *skb;
  737. size_t len;
  738. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  739. return NULL;
  740. len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
  741. skb = genlmsg_new(len, GFP_KERNEL);
  742. if (!skb)
  743. return ERR_PTR(-ENOMEM);
  744. return skb;
  745. }
  746. /* Called with ovs_mutex. */
  747. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  748. int dp_ifindex,
  749. struct genl_info *info, u8 cmd,
  750. bool always, u32 ufid_flags)
  751. {
  752. struct sk_buff *skb;
  753. int retval;
  754. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
  755. &flow->id, info, always, ufid_flags);
  756. if (IS_ERR_OR_NULL(skb))
  757. return skb;
  758. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  759. info->snd_portid, info->snd_seq, 0,
  760. cmd, ufid_flags);
  761. BUG_ON(retval < 0);
  762. return skb;
  763. }
  764. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  765. {
  766. struct net *net = sock_net(skb->sk);
  767. struct nlattr **a = info->attrs;
  768. struct ovs_header *ovs_header = info->userhdr;
  769. struct sw_flow *flow = NULL, *new_flow;
  770. struct sw_flow_mask mask;
  771. struct sk_buff *reply;
  772. struct datapath *dp;
  773. struct sw_flow_actions *acts;
  774. struct sw_flow_match match;
  775. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  776. int error;
  777. bool log = !a[OVS_FLOW_ATTR_PROBE];
  778. /* Must have key and actions. */
  779. error = -EINVAL;
  780. if (!a[OVS_FLOW_ATTR_KEY]) {
  781. OVS_NLERR(log, "Flow key attr not present in new flow.");
  782. goto error;
  783. }
  784. if (!a[OVS_FLOW_ATTR_ACTIONS]) {
  785. OVS_NLERR(log, "Flow actions attr not present in new flow.");
  786. goto error;
  787. }
  788. /* Most of the time we need to allocate a new flow, do it before
  789. * locking.
  790. */
  791. new_flow = ovs_flow_alloc();
  792. if (IS_ERR(new_flow)) {
  793. error = PTR_ERR(new_flow);
  794. goto error;
  795. }
  796. /* Extract key. */
  797. ovs_match_init(&match, &new_flow->key, false, &mask);
  798. error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
  799. a[OVS_FLOW_ATTR_MASK], log);
  800. if (error)
  801. goto err_kfree_flow;
  802. /* Extract flow identifier. */
  803. error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
  804. &new_flow->key, log);
  805. if (error)
  806. goto err_kfree_flow;
  807. /* unmasked key is needed to match when ufid is not used. */
  808. if (ovs_identifier_is_key(&new_flow->id))
  809. match.key = new_flow->id.unmasked_key;
  810. ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
  811. /* Validate actions. */
  812. error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
  813. &new_flow->key, &acts, log);
  814. if (error) {
  815. OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
  816. goto err_kfree_flow;
  817. }
  818. reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
  819. ufid_flags);
  820. if (IS_ERR(reply)) {
  821. error = PTR_ERR(reply);
  822. goto err_kfree_acts;
  823. }
  824. ovs_lock();
  825. dp = get_dp(net, ovs_header->dp_ifindex);
  826. if (unlikely(!dp)) {
  827. error = -ENODEV;
  828. goto err_unlock_ovs;
  829. }
  830. /* Check if this is a duplicate flow */
  831. if (ovs_identifier_is_ufid(&new_flow->id))
  832. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
  833. if (!flow)
  834. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
  835. if (likely(!flow)) {
  836. rcu_assign_pointer(new_flow->sf_acts, acts);
  837. /* Put flow in bucket. */
  838. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  839. if (unlikely(error)) {
  840. acts = NULL;
  841. goto err_unlock_ovs;
  842. }
  843. if (unlikely(reply)) {
  844. error = ovs_flow_cmd_fill_info(new_flow,
  845. ovs_header->dp_ifindex,
  846. reply, info->snd_portid,
  847. info->snd_seq, 0,
  848. OVS_FLOW_CMD_NEW,
  849. ufid_flags);
  850. BUG_ON(error < 0);
  851. }
  852. ovs_unlock();
  853. } else {
  854. struct sw_flow_actions *old_acts;
  855. /* Bail out if we're not allowed to modify an existing flow.
  856. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  857. * because Generic Netlink treats the latter as a dump
  858. * request. We also accept NLM_F_EXCL in case that bug ever
  859. * gets fixed.
  860. */
  861. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  862. | NLM_F_EXCL))) {
  863. error = -EEXIST;
  864. goto err_unlock_ovs;
  865. }
  866. /* The flow identifier has to be the same for flow updates.
  867. * Look for any overlapping flow.
  868. */
  869. if (unlikely(!ovs_flow_cmp(flow, &match))) {
  870. if (ovs_identifier_is_key(&flow->id))
  871. flow = ovs_flow_tbl_lookup_exact(&dp->table,
  872. &match);
  873. else /* UFID matches but key is different */
  874. flow = NULL;
  875. if (!flow) {
  876. error = -ENOENT;
  877. goto err_unlock_ovs;
  878. }
  879. }
  880. /* Update actions. */
  881. old_acts = ovsl_dereference(flow->sf_acts);
  882. rcu_assign_pointer(flow->sf_acts, acts);
  883. if (unlikely(reply)) {
  884. error = ovs_flow_cmd_fill_info(flow,
  885. ovs_header->dp_ifindex,
  886. reply, info->snd_portid,
  887. info->snd_seq, 0,
  888. OVS_FLOW_CMD_NEW,
  889. ufid_flags);
  890. BUG_ON(error < 0);
  891. }
  892. ovs_unlock();
  893. ovs_nla_free_flow_actions_rcu(old_acts);
  894. ovs_flow_free(new_flow, false);
  895. }
  896. if (reply)
  897. ovs_notify(&dp_flow_genl_family, reply, info);
  898. return 0;
  899. err_unlock_ovs:
  900. ovs_unlock();
  901. kfree_skb(reply);
  902. err_kfree_acts:
  903. ovs_nla_free_flow_actions(acts);
  904. err_kfree_flow:
  905. ovs_flow_free(new_flow, false);
  906. error:
  907. return error;
  908. }
  909. /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
  910. static struct sw_flow_actions *get_flow_actions(struct net *net,
  911. const struct nlattr *a,
  912. const struct sw_flow_key *key,
  913. const struct sw_flow_mask *mask,
  914. bool log)
  915. {
  916. struct sw_flow_actions *acts;
  917. struct sw_flow_key masked_key;
  918. int error;
  919. ovs_flow_mask_key(&masked_key, key, true, mask);
  920. error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
  921. if (error) {
  922. OVS_NLERR(log,
  923. "Actions may not be safe on all matching packets");
  924. return ERR_PTR(error);
  925. }
  926. return acts;
  927. }
  928. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  929. {
  930. struct net *net = sock_net(skb->sk);
  931. struct nlattr **a = info->attrs;
  932. struct ovs_header *ovs_header = info->userhdr;
  933. struct sw_flow_key key;
  934. struct sw_flow *flow;
  935. struct sw_flow_mask mask;
  936. struct sk_buff *reply = NULL;
  937. struct datapath *dp;
  938. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  939. struct sw_flow_match match;
  940. struct sw_flow_id sfid;
  941. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  942. int error = 0;
  943. bool log = !a[OVS_FLOW_ATTR_PROBE];
  944. bool ufid_present;
  945. ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
  946. if (a[OVS_FLOW_ATTR_KEY]) {
  947. ovs_match_init(&match, &key, true, &mask);
  948. error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
  949. a[OVS_FLOW_ATTR_MASK], log);
  950. } else if (!ufid_present) {
  951. OVS_NLERR(log,
  952. "Flow set message rejected, Key attribute missing.");
  953. error = -EINVAL;
  954. }
  955. if (error)
  956. goto error;
  957. /* Validate actions. */
  958. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  959. if (!a[OVS_FLOW_ATTR_KEY]) {
  960. OVS_NLERR(log,
  961. "Flow key attribute not present in set flow.");
  962. error = -EINVAL;
  963. goto error;
  964. }
  965. acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], &key,
  966. &mask, log);
  967. if (IS_ERR(acts)) {
  968. error = PTR_ERR(acts);
  969. goto error;
  970. }
  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);
  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 = {
  1225. .id = GENL_ID_GENERATE,
  1226. .hdrsize = sizeof(struct ovs_header),
  1227. .name = OVS_FLOW_FAMILY,
  1228. .version = OVS_FLOW_VERSION,
  1229. .maxattr = OVS_FLOW_ATTR_MAX,
  1230. .netnsok = true,
  1231. .parallel_ops = true,
  1232. .ops = dp_flow_genl_ops,
  1233. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1234. .mcgrps = &ovs_dp_flow_multicast_group,
  1235. .n_mcgrps = 1,
  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. /* Set up our datapath device. */
  1350. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1351. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1352. parms.options = NULL;
  1353. parms.dp = dp;
  1354. parms.port_no = OVSP_LOCAL;
  1355. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1356. ovs_dp_change(dp, a);
  1357. /* So far only local changes have been made, now need the lock. */
  1358. ovs_lock();
  1359. vport = new_vport(&parms);
  1360. if (IS_ERR(vport)) {
  1361. err = PTR_ERR(vport);
  1362. if (err == -EBUSY)
  1363. err = -EEXIST;
  1364. if (err == -EEXIST) {
  1365. /* An outdated user space instance that does not understand
  1366. * the concept of user_features has attempted to create a new
  1367. * datapath and is likely to reuse it. Drop all user features.
  1368. */
  1369. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1370. ovs_dp_reset_user_features(skb, info);
  1371. }
  1372. goto err_destroy_ports_array;
  1373. }
  1374. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1375. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1376. BUG_ON(err < 0);
  1377. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1378. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1379. ovs_unlock();
  1380. ovs_notify(&dp_datapath_genl_family, reply, info);
  1381. return 0;
  1382. err_destroy_ports_array:
  1383. ovs_unlock();
  1384. kfree(dp->ports);
  1385. err_destroy_percpu:
  1386. free_percpu(dp->stats_percpu);
  1387. err_destroy_table:
  1388. ovs_flow_tbl_destroy(&dp->table);
  1389. err_free_dp:
  1390. kfree(dp);
  1391. err_free_reply:
  1392. kfree_skb(reply);
  1393. err:
  1394. return err;
  1395. }
  1396. /* Called with ovs_mutex. */
  1397. static void __dp_destroy(struct datapath *dp)
  1398. {
  1399. int i;
  1400. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1401. struct vport *vport;
  1402. struct hlist_node *n;
  1403. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1404. if (vport->port_no != OVSP_LOCAL)
  1405. ovs_dp_detach_port(vport);
  1406. }
  1407. list_del_rcu(&dp->list_node);
  1408. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1409. * all ports in datapath are destroyed first before freeing datapath.
  1410. */
  1411. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1412. /* RCU destroy the flow table */
  1413. call_rcu(&dp->rcu, destroy_dp_rcu);
  1414. }
  1415. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1416. {
  1417. struct sk_buff *reply;
  1418. struct datapath *dp;
  1419. int err;
  1420. reply = ovs_dp_cmd_alloc_info();
  1421. if (!reply)
  1422. return -ENOMEM;
  1423. ovs_lock();
  1424. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1425. err = PTR_ERR(dp);
  1426. if (IS_ERR(dp))
  1427. goto err_unlock_free;
  1428. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1429. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1430. BUG_ON(err < 0);
  1431. __dp_destroy(dp);
  1432. ovs_unlock();
  1433. ovs_notify(&dp_datapath_genl_family, reply, info);
  1434. return 0;
  1435. err_unlock_free:
  1436. ovs_unlock();
  1437. kfree_skb(reply);
  1438. return err;
  1439. }
  1440. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1441. {
  1442. struct sk_buff *reply;
  1443. struct datapath *dp;
  1444. int err;
  1445. reply = ovs_dp_cmd_alloc_info();
  1446. if (!reply)
  1447. return -ENOMEM;
  1448. ovs_lock();
  1449. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1450. err = PTR_ERR(dp);
  1451. if (IS_ERR(dp))
  1452. goto err_unlock_free;
  1453. ovs_dp_change(dp, info->attrs);
  1454. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1455. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1456. BUG_ON(err < 0);
  1457. ovs_unlock();
  1458. ovs_notify(&dp_datapath_genl_family, reply, info);
  1459. return 0;
  1460. err_unlock_free:
  1461. ovs_unlock();
  1462. kfree_skb(reply);
  1463. return err;
  1464. }
  1465. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1466. {
  1467. struct sk_buff *reply;
  1468. struct datapath *dp;
  1469. int err;
  1470. reply = ovs_dp_cmd_alloc_info();
  1471. if (!reply)
  1472. return -ENOMEM;
  1473. ovs_lock();
  1474. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1475. if (IS_ERR(dp)) {
  1476. err = PTR_ERR(dp);
  1477. goto err_unlock_free;
  1478. }
  1479. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1480. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1481. BUG_ON(err < 0);
  1482. ovs_unlock();
  1483. return genlmsg_reply(reply, info);
  1484. err_unlock_free:
  1485. ovs_unlock();
  1486. kfree_skb(reply);
  1487. return err;
  1488. }
  1489. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1490. {
  1491. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1492. struct datapath *dp;
  1493. int skip = cb->args[0];
  1494. int i = 0;
  1495. ovs_lock();
  1496. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1497. if (i >= skip &&
  1498. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1499. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1500. OVS_DP_CMD_NEW) < 0)
  1501. break;
  1502. i++;
  1503. }
  1504. ovs_unlock();
  1505. cb->args[0] = i;
  1506. return skb->len;
  1507. }
  1508. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1509. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1510. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1511. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1512. };
  1513. static const struct genl_ops dp_datapath_genl_ops[] = {
  1514. { .cmd = OVS_DP_CMD_NEW,
  1515. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1516. .policy = datapath_policy,
  1517. .doit = ovs_dp_cmd_new
  1518. },
  1519. { .cmd = OVS_DP_CMD_DEL,
  1520. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1521. .policy = datapath_policy,
  1522. .doit = ovs_dp_cmd_del
  1523. },
  1524. { .cmd = OVS_DP_CMD_GET,
  1525. .flags = 0, /* OK for unprivileged users. */
  1526. .policy = datapath_policy,
  1527. .doit = ovs_dp_cmd_get,
  1528. .dumpit = ovs_dp_cmd_dump
  1529. },
  1530. { .cmd = OVS_DP_CMD_SET,
  1531. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1532. .policy = datapath_policy,
  1533. .doit = ovs_dp_cmd_set,
  1534. },
  1535. };
  1536. static struct genl_family dp_datapath_genl_family = {
  1537. .id = GENL_ID_GENERATE,
  1538. .hdrsize = sizeof(struct ovs_header),
  1539. .name = OVS_DATAPATH_FAMILY,
  1540. .version = OVS_DATAPATH_VERSION,
  1541. .maxattr = OVS_DP_ATTR_MAX,
  1542. .netnsok = true,
  1543. .parallel_ops = true,
  1544. .ops = dp_datapath_genl_ops,
  1545. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1546. .mcgrps = &ovs_dp_datapath_multicast_group,
  1547. .n_mcgrps = 1,
  1548. };
  1549. /* Called with ovs_mutex or RCU read lock. */
  1550. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1551. u32 portid, u32 seq, u32 flags, u8 cmd)
  1552. {
  1553. struct ovs_header *ovs_header;
  1554. struct ovs_vport_stats vport_stats;
  1555. int err;
  1556. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1557. flags, cmd);
  1558. if (!ovs_header)
  1559. return -EMSGSIZE;
  1560. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1561. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1562. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1563. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1564. ovs_vport_name(vport)))
  1565. goto nla_put_failure;
  1566. ovs_vport_get_stats(vport, &vport_stats);
  1567. if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
  1568. sizeof(struct ovs_vport_stats), &vport_stats,
  1569. OVS_VPORT_ATTR_PAD))
  1570. goto nla_put_failure;
  1571. if (ovs_vport_get_upcall_portids(vport, skb))
  1572. goto nla_put_failure;
  1573. err = ovs_vport_get_options(vport, skb);
  1574. if (err == -EMSGSIZE)
  1575. goto error;
  1576. genlmsg_end(skb, ovs_header);
  1577. return 0;
  1578. nla_put_failure:
  1579. err = -EMSGSIZE;
  1580. error:
  1581. genlmsg_cancel(skb, ovs_header);
  1582. return err;
  1583. }
  1584. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1585. {
  1586. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1587. }
  1588. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1589. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1590. u32 seq, u8 cmd)
  1591. {
  1592. struct sk_buff *skb;
  1593. int retval;
  1594. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1595. if (!skb)
  1596. return ERR_PTR(-ENOMEM);
  1597. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1598. BUG_ON(retval < 0);
  1599. return skb;
  1600. }
  1601. /* Called with ovs_mutex or RCU read lock. */
  1602. static struct vport *lookup_vport(struct net *net,
  1603. const struct ovs_header *ovs_header,
  1604. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1605. {
  1606. struct datapath *dp;
  1607. struct vport *vport;
  1608. if (a[OVS_VPORT_ATTR_NAME]) {
  1609. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1610. if (!vport)
  1611. return ERR_PTR(-ENODEV);
  1612. if (ovs_header->dp_ifindex &&
  1613. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1614. return ERR_PTR(-ENODEV);
  1615. return vport;
  1616. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1617. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1618. if (port_no >= DP_MAX_PORTS)
  1619. return ERR_PTR(-EFBIG);
  1620. dp = get_dp(net, ovs_header->dp_ifindex);
  1621. if (!dp)
  1622. return ERR_PTR(-ENODEV);
  1623. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1624. if (!vport)
  1625. return ERR_PTR(-ENODEV);
  1626. return vport;
  1627. } else
  1628. return ERR_PTR(-EINVAL);
  1629. }
  1630. /* Called with ovs_mutex */
  1631. static void update_headroom(struct datapath *dp)
  1632. {
  1633. unsigned dev_headroom, max_headroom = 0;
  1634. struct net_device *dev;
  1635. struct vport *vport;
  1636. int i;
  1637. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1638. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1639. dev = vport->dev;
  1640. dev_headroom = netdev_get_fwd_headroom(dev);
  1641. if (dev_headroom > max_headroom)
  1642. max_headroom = dev_headroom;
  1643. }
  1644. }
  1645. dp->max_headroom = max_headroom;
  1646. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1647. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
  1648. netdev_set_rx_headroom(vport->dev, max_headroom);
  1649. }
  1650. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1651. {
  1652. struct nlattr **a = info->attrs;
  1653. struct ovs_header *ovs_header = info->userhdr;
  1654. struct vport_parms parms;
  1655. struct sk_buff *reply;
  1656. struct vport *vport;
  1657. struct datapath *dp;
  1658. u32 port_no;
  1659. int err;
  1660. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1661. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1662. return -EINVAL;
  1663. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1664. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1665. if (port_no >= DP_MAX_PORTS)
  1666. return -EFBIG;
  1667. reply = ovs_vport_cmd_alloc_info();
  1668. if (!reply)
  1669. return -ENOMEM;
  1670. ovs_lock();
  1671. restart:
  1672. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1673. err = -ENODEV;
  1674. if (!dp)
  1675. goto exit_unlock_free;
  1676. if (port_no) {
  1677. vport = ovs_vport_ovsl(dp, port_no);
  1678. err = -EBUSY;
  1679. if (vport)
  1680. goto exit_unlock_free;
  1681. } else {
  1682. for (port_no = 1; ; port_no++) {
  1683. if (port_no >= DP_MAX_PORTS) {
  1684. err = -EFBIG;
  1685. goto exit_unlock_free;
  1686. }
  1687. vport = ovs_vport_ovsl(dp, port_no);
  1688. if (!vport)
  1689. break;
  1690. }
  1691. }
  1692. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1693. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1694. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1695. parms.dp = dp;
  1696. parms.port_no = port_no;
  1697. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1698. vport = new_vport(&parms);
  1699. err = PTR_ERR(vport);
  1700. if (IS_ERR(vport)) {
  1701. if (err == -EAGAIN)
  1702. goto restart;
  1703. goto exit_unlock_free;
  1704. }
  1705. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1706. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1707. if (netdev_get_fwd_headroom(vport->dev) > dp->max_headroom)
  1708. update_headroom(dp);
  1709. else
  1710. netdev_set_rx_headroom(vport->dev, dp->max_headroom);
  1711. BUG_ON(err < 0);
  1712. ovs_unlock();
  1713. ovs_notify(&dp_vport_genl_family, reply, info);
  1714. return 0;
  1715. exit_unlock_free:
  1716. ovs_unlock();
  1717. kfree_skb(reply);
  1718. return err;
  1719. }
  1720. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1721. {
  1722. struct nlattr **a = info->attrs;
  1723. struct sk_buff *reply;
  1724. struct vport *vport;
  1725. int err;
  1726. reply = ovs_vport_cmd_alloc_info();
  1727. if (!reply)
  1728. return -ENOMEM;
  1729. ovs_lock();
  1730. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1731. err = PTR_ERR(vport);
  1732. if (IS_ERR(vport))
  1733. goto exit_unlock_free;
  1734. if (a[OVS_VPORT_ATTR_TYPE] &&
  1735. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1736. err = -EINVAL;
  1737. goto exit_unlock_free;
  1738. }
  1739. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1740. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1741. if (err)
  1742. goto exit_unlock_free;
  1743. }
  1744. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1745. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1746. err = ovs_vport_set_upcall_portids(vport, ids);
  1747. if (err)
  1748. goto exit_unlock_free;
  1749. }
  1750. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1751. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1752. BUG_ON(err < 0);
  1753. ovs_unlock();
  1754. ovs_notify(&dp_vport_genl_family, reply, info);
  1755. return 0;
  1756. exit_unlock_free:
  1757. ovs_unlock();
  1758. kfree_skb(reply);
  1759. return err;
  1760. }
  1761. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1762. {
  1763. bool must_update_headroom = false;
  1764. struct nlattr **a = info->attrs;
  1765. struct sk_buff *reply;
  1766. struct datapath *dp;
  1767. struct vport *vport;
  1768. int err;
  1769. reply = ovs_vport_cmd_alloc_info();
  1770. if (!reply)
  1771. return -ENOMEM;
  1772. ovs_lock();
  1773. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1774. err = PTR_ERR(vport);
  1775. if (IS_ERR(vport))
  1776. goto exit_unlock_free;
  1777. if (vport->port_no == OVSP_LOCAL) {
  1778. err = -EINVAL;
  1779. goto exit_unlock_free;
  1780. }
  1781. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1782. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1783. BUG_ON(err < 0);
  1784. /* the vport deletion may trigger dp headroom update */
  1785. dp = vport->dp;
  1786. if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
  1787. must_update_headroom = true;
  1788. netdev_reset_rx_headroom(vport->dev);
  1789. ovs_dp_detach_port(vport);
  1790. if (must_update_headroom)
  1791. update_headroom(dp);
  1792. ovs_unlock();
  1793. ovs_notify(&dp_vport_genl_family, reply, info);
  1794. return 0;
  1795. exit_unlock_free:
  1796. ovs_unlock();
  1797. kfree_skb(reply);
  1798. return err;
  1799. }
  1800. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1801. {
  1802. struct nlattr **a = info->attrs;
  1803. struct ovs_header *ovs_header = info->userhdr;
  1804. struct sk_buff *reply;
  1805. struct vport *vport;
  1806. int err;
  1807. reply = ovs_vport_cmd_alloc_info();
  1808. if (!reply)
  1809. return -ENOMEM;
  1810. rcu_read_lock();
  1811. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1812. err = PTR_ERR(vport);
  1813. if (IS_ERR(vport))
  1814. goto exit_unlock_free;
  1815. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1816. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1817. BUG_ON(err < 0);
  1818. rcu_read_unlock();
  1819. return genlmsg_reply(reply, info);
  1820. exit_unlock_free:
  1821. rcu_read_unlock();
  1822. kfree_skb(reply);
  1823. return err;
  1824. }
  1825. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1826. {
  1827. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1828. struct datapath *dp;
  1829. int bucket = cb->args[0], skip = cb->args[1];
  1830. int i, j = 0;
  1831. rcu_read_lock();
  1832. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1833. if (!dp) {
  1834. rcu_read_unlock();
  1835. return -ENODEV;
  1836. }
  1837. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1838. struct vport *vport;
  1839. j = 0;
  1840. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1841. if (j >= skip &&
  1842. ovs_vport_cmd_fill_info(vport, skb,
  1843. NETLINK_CB(cb->skb).portid,
  1844. cb->nlh->nlmsg_seq,
  1845. NLM_F_MULTI,
  1846. OVS_VPORT_CMD_NEW) < 0)
  1847. goto out;
  1848. j++;
  1849. }
  1850. skip = 0;
  1851. }
  1852. out:
  1853. rcu_read_unlock();
  1854. cb->args[0] = i;
  1855. cb->args[1] = j;
  1856. return skb->len;
  1857. }
  1858. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1859. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1860. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1861. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1862. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1863. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1864. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1865. };
  1866. static const struct genl_ops dp_vport_genl_ops[] = {
  1867. { .cmd = OVS_VPORT_CMD_NEW,
  1868. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1869. .policy = vport_policy,
  1870. .doit = ovs_vport_cmd_new
  1871. },
  1872. { .cmd = OVS_VPORT_CMD_DEL,
  1873. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1874. .policy = vport_policy,
  1875. .doit = ovs_vport_cmd_del
  1876. },
  1877. { .cmd = OVS_VPORT_CMD_GET,
  1878. .flags = 0, /* OK for unprivileged users. */
  1879. .policy = vport_policy,
  1880. .doit = ovs_vport_cmd_get,
  1881. .dumpit = ovs_vport_cmd_dump
  1882. },
  1883. { .cmd = OVS_VPORT_CMD_SET,
  1884. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1885. .policy = vport_policy,
  1886. .doit = ovs_vport_cmd_set,
  1887. },
  1888. };
  1889. struct genl_family dp_vport_genl_family = {
  1890. .id = GENL_ID_GENERATE,
  1891. .hdrsize = sizeof(struct ovs_header),
  1892. .name = OVS_VPORT_FAMILY,
  1893. .version = OVS_VPORT_VERSION,
  1894. .maxattr = OVS_VPORT_ATTR_MAX,
  1895. .netnsok = true,
  1896. .parallel_ops = true,
  1897. .ops = dp_vport_genl_ops,
  1898. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1899. .mcgrps = &ovs_dp_vport_multicast_group,
  1900. .n_mcgrps = 1,
  1901. };
  1902. static struct genl_family * const dp_genl_families[] = {
  1903. &dp_datapath_genl_family,
  1904. &dp_vport_genl_family,
  1905. &dp_flow_genl_family,
  1906. &dp_packet_genl_family,
  1907. };
  1908. static void dp_unregister_genl(int n_families)
  1909. {
  1910. int i;
  1911. for (i = 0; i < n_families; i++)
  1912. genl_unregister_family(dp_genl_families[i]);
  1913. }
  1914. static int dp_register_genl(void)
  1915. {
  1916. int err;
  1917. int i;
  1918. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1919. err = genl_register_family(dp_genl_families[i]);
  1920. if (err)
  1921. goto error;
  1922. }
  1923. return 0;
  1924. error:
  1925. dp_unregister_genl(i);
  1926. return err;
  1927. }
  1928. static int __net_init ovs_init_net(struct net *net)
  1929. {
  1930. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1931. INIT_LIST_HEAD(&ovs_net->dps);
  1932. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1933. ovs_ct_init(net);
  1934. return 0;
  1935. }
  1936. static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
  1937. struct list_head *head)
  1938. {
  1939. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1940. struct datapath *dp;
  1941. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1942. int i;
  1943. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1944. struct vport *vport;
  1945. hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
  1946. if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
  1947. continue;
  1948. if (dev_net(vport->dev) == dnet)
  1949. list_add(&vport->detach_list, head);
  1950. }
  1951. }
  1952. }
  1953. }
  1954. static void __net_exit ovs_exit_net(struct net *dnet)
  1955. {
  1956. struct datapath *dp, *dp_next;
  1957. struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
  1958. struct vport *vport, *vport_next;
  1959. struct net *net;
  1960. LIST_HEAD(head);
  1961. ovs_ct_exit(dnet);
  1962. ovs_lock();
  1963. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1964. __dp_destroy(dp);
  1965. rtnl_lock();
  1966. for_each_net(net)
  1967. list_vports_from_net(net, dnet, &head);
  1968. rtnl_unlock();
  1969. /* Detach all vports from given namespace. */
  1970. list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
  1971. list_del(&vport->detach_list);
  1972. ovs_dp_detach_port(vport);
  1973. }
  1974. ovs_unlock();
  1975. cancel_work_sync(&ovs_net->dp_notify_work);
  1976. }
  1977. static struct pernet_operations ovs_net_ops = {
  1978. .init = ovs_init_net,
  1979. .exit = ovs_exit_net,
  1980. .id = &ovs_net_id,
  1981. .size = sizeof(struct ovs_net),
  1982. };
  1983. static int __init dp_init(void)
  1984. {
  1985. int err;
  1986. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1987. pr_info("Open vSwitch switching datapath\n");
  1988. err = action_fifos_init();
  1989. if (err)
  1990. goto error;
  1991. err = ovs_internal_dev_rtnl_link_register();
  1992. if (err)
  1993. goto error_action_fifos_exit;
  1994. err = ovs_flow_init();
  1995. if (err)
  1996. goto error_unreg_rtnl_link;
  1997. err = ovs_vport_init();
  1998. if (err)
  1999. goto error_flow_exit;
  2000. err = register_pernet_device(&ovs_net_ops);
  2001. if (err)
  2002. goto error_vport_exit;
  2003. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  2004. if (err)
  2005. goto error_netns_exit;
  2006. err = ovs_netdev_init();
  2007. if (err)
  2008. goto error_unreg_notifier;
  2009. err = dp_register_genl();
  2010. if (err < 0)
  2011. goto error_unreg_netdev;
  2012. return 0;
  2013. error_unreg_netdev:
  2014. ovs_netdev_exit();
  2015. error_unreg_notifier:
  2016. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2017. error_netns_exit:
  2018. unregister_pernet_device(&ovs_net_ops);
  2019. error_vport_exit:
  2020. ovs_vport_exit();
  2021. error_flow_exit:
  2022. ovs_flow_exit();
  2023. error_unreg_rtnl_link:
  2024. ovs_internal_dev_rtnl_link_unregister();
  2025. error_action_fifos_exit:
  2026. action_fifos_exit();
  2027. error:
  2028. return err;
  2029. }
  2030. static void dp_cleanup(void)
  2031. {
  2032. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  2033. ovs_netdev_exit();
  2034. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2035. unregister_pernet_device(&ovs_net_ops);
  2036. rcu_barrier();
  2037. ovs_vport_exit();
  2038. ovs_flow_exit();
  2039. ovs_internal_dev_rtnl_link_unregister();
  2040. action_fifos_exit();
  2041. }
  2042. module_init(dp_init);
  2043. module_exit(dp_cleanup);
  2044. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  2045. MODULE_LICENSE("GPL");
  2046. MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
  2047. MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
  2048. MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
  2049. MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);