datapath.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  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 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, genl_info_net(info), info->snd_portid,
  83. 0, info->nlhdr, 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. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  125. const struct sw_flow_key *,
  126. const struct dp_upcall_info *);
  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 vport->ops->get_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 = netdev_vport_priv(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. release_net(ovs_dp_get_net(dp));
  175. kfree(dp->ports);
  176. kfree(dp);
  177. }
  178. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  179. u16 port_no)
  180. {
  181. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  182. }
  183. /* Called with ovs_mutex or RCU read lock. */
  184. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  185. {
  186. struct vport *vport;
  187. struct hlist_head *head;
  188. head = vport_hash_bucket(dp, port_no);
  189. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  190. if (vport->port_no == port_no)
  191. return vport;
  192. }
  193. return NULL;
  194. }
  195. /* Called with ovs_mutex. */
  196. static struct vport *new_vport(const struct vport_parms *parms)
  197. {
  198. struct vport *vport;
  199. vport = ovs_vport_add(parms);
  200. if (!IS_ERR(vport)) {
  201. struct datapath *dp = parms->dp;
  202. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  203. hlist_add_head_rcu(&vport->dp_hash_node, head);
  204. }
  205. return vport;
  206. }
  207. void ovs_dp_detach_port(struct vport *p)
  208. {
  209. ASSERT_OVSL();
  210. /* First drop references to device. */
  211. hlist_del_rcu(&p->dp_hash_node);
  212. /* Then destroy it. */
  213. ovs_vport_del(p);
  214. }
  215. /* Must be called with rcu_read_lock. */
  216. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  217. {
  218. const struct vport *p = OVS_CB(skb)->input_vport;
  219. struct datapath *dp = p->dp;
  220. struct sw_flow *flow;
  221. struct sw_flow_actions *sf_acts;
  222. struct dp_stats_percpu *stats;
  223. u64 *stats_counter;
  224. u32 n_mask_hit;
  225. stats = this_cpu_ptr(dp->stats_percpu);
  226. /* Look up flow. */
  227. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  228. if (unlikely(!flow)) {
  229. struct dp_upcall_info upcall;
  230. int error;
  231. upcall.cmd = OVS_PACKET_CMD_MISS;
  232. upcall.userdata = NULL;
  233. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  234. upcall.egress_tun_info = NULL;
  235. error = ovs_dp_upcall(dp, skb, key, &upcall);
  236. if (unlikely(error))
  237. kfree_skb(skb);
  238. else
  239. consume_skb(skb);
  240. stats_counter = &stats->n_missed;
  241. goto out;
  242. }
  243. ovs_flow_stats_update(flow, key->tp.flags, skb);
  244. sf_acts = rcu_dereference(flow->sf_acts);
  245. ovs_execute_actions(dp, skb, sf_acts, key);
  246. stats_counter = &stats->n_hit;
  247. out:
  248. /* Update datapath statistics. */
  249. u64_stats_update_begin(&stats->syncp);
  250. (*stats_counter)++;
  251. stats->n_mask_hit += n_mask_hit;
  252. u64_stats_update_end(&stats->syncp);
  253. }
  254. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  255. const struct sw_flow_key *key,
  256. const struct dp_upcall_info *upcall_info)
  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);
  266. else
  267. err = queue_gso_packets(dp, skb, key, upcall_info);
  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. {
  282. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  283. struct sw_flow_key later_key;
  284. struct sk_buff *segs, *nskb;
  285. struct ovs_skb_cb ovs_cb;
  286. int err;
  287. ovs_cb = *OVS_CB(skb);
  288. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  289. *OVS_CB(skb) = ovs_cb;
  290. if (IS_ERR(segs))
  291. return PTR_ERR(segs);
  292. if (segs == NULL)
  293. return -EINVAL;
  294. if (gso_type & SKB_GSO_UDP) {
  295. /* The initial flow key extracted by ovs_flow_key_extract()
  296. * in this case is for a first fragment, so we need to
  297. * properly mark later fragments.
  298. */
  299. later_key = *key;
  300. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  301. }
  302. /* Queue all of the segments. */
  303. skb = segs;
  304. do {
  305. *OVS_CB(skb) = ovs_cb;
  306. if (gso_type & SKB_GSO_UDP && skb != segs)
  307. key = &later_key;
  308. err = queue_userspace_packet(dp, skb, key, upcall_info);
  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. /* OVS_PACKET_ATTR_USERDATA */
  330. if (upcall_info->userdata)
  331. size += NLA_ALIGN(upcall_info->userdata->nla_len);
  332. /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
  333. if (upcall_info->egress_tun_info)
  334. size += nla_total_size(ovs_tun_key_attr_size());
  335. return size;
  336. }
  337. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  338. const struct sw_flow_key *key,
  339. const struct dp_upcall_info *upcall_info)
  340. {
  341. struct ovs_header *upcall;
  342. struct sk_buff *nskb = NULL;
  343. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  344. struct nlattr *nla;
  345. struct genl_info info = {
  346. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  347. .snd_portid = upcall_info->portid,
  348. };
  349. size_t len;
  350. unsigned int hlen;
  351. int err, dp_ifindex;
  352. dp_ifindex = get_dpifindex(dp);
  353. if (!dp_ifindex)
  354. return -ENODEV;
  355. if (vlan_tx_tag_present(skb)) {
  356. nskb = skb_clone(skb, GFP_ATOMIC);
  357. if (!nskb)
  358. return -ENOMEM;
  359. nskb = __vlan_hwaccel_push_inside(nskb);
  360. if (!nskb)
  361. return -ENOMEM;
  362. skb = nskb;
  363. }
  364. if (nla_attr_size(skb->len) > USHRT_MAX) {
  365. err = -EFBIG;
  366. goto out;
  367. }
  368. /* Complete checksum if needed */
  369. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  370. (err = skb_checksum_help(skb)))
  371. goto out;
  372. /* Older versions of OVS user space enforce alignment of the last
  373. * Netlink attribute to NLA_ALIGNTO which would require extensive
  374. * padding logic. Only perform zerocopy if padding is not required.
  375. */
  376. if (dp->user_features & OVS_DP_F_UNALIGNED)
  377. hlen = skb_zerocopy_headlen(skb);
  378. else
  379. hlen = skb->len;
  380. len = upcall_msg_size(upcall_info, hlen);
  381. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  382. if (!user_skb) {
  383. err = -ENOMEM;
  384. goto out;
  385. }
  386. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  387. 0, upcall_info->cmd);
  388. upcall->dp_ifindex = dp_ifindex;
  389. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  390. err = ovs_nla_put_flow(key, key, user_skb);
  391. BUG_ON(err);
  392. nla_nest_end(user_skb, nla);
  393. if (upcall_info->userdata)
  394. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  395. nla_len(upcall_info->userdata),
  396. nla_data(upcall_info->userdata));
  397. if (upcall_info->egress_tun_info) {
  398. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
  399. err = ovs_nla_put_egress_tunnel_key(user_skb,
  400. upcall_info->egress_tun_info);
  401. BUG_ON(err);
  402. nla_nest_end(user_skb, nla);
  403. }
  404. /* Only reserve room for attribute header, packet data is added
  405. * in skb_zerocopy() */
  406. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  407. err = -ENOBUFS;
  408. goto out;
  409. }
  410. nla->nla_len = nla_attr_size(skb->len);
  411. err = skb_zerocopy(user_skb, skb, skb->len, hlen);
  412. if (err)
  413. goto out;
  414. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  415. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  416. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  417. if (plen > 0)
  418. memset(skb_put(user_skb, plen), 0, plen);
  419. }
  420. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  421. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  422. user_skb = NULL;
  423. out:
  424. if (err)
  425. skb_tx_error(skb);
  426. kfree_skb(user_skb);
  427. kfree_skb(nskb);
  428. return err;
  429. }
  430. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  431. {
  432. struct ovs_header *ovs_header = info->userhdr;
  433. struct nlattr **a = info->attrs;
  434. struct sw_flow_actions *acts;
  435. struct sk_buff *packet;
  436. struct sw_flow *flow;
  437. struct sw_flow_actions *sf_acts;
  438. struct datapath *dp;
  439. struct ethhdr *eth;
  440. struct vport *input_vport;
  441. int len;
  442. int err;
  443. bool log = !a[OVS_FLOW_ATTR_PROBE];
  444. err = -EINVAL;
  445. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  446. !a[OVS_PACKET_ATTR_ACTIONS])
  447. goto err;
  448. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  449. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  450. err = -ENOMEM;
  451. if (!packet)
  452. goto err;
  453. skb_reserve(packet, NET_IP_ALIGN);
  454. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  455. skb_reset_mac_header(packet);
  456. eth = eth_hdr(packet);
  457. /* Normally, setting the skb 'protocol' field would be handled by a
  458. * call to eth_type_trans(), but it assumes there's a sending
  459. * device, which we may not have. */
  460. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  461. packet->protocol = eth->h_proto;
  462. else
  463. packet->protocol = htons(ETH_P_802_2);
  464. /* Build an sw_flow for sending this packet. */
  465. flow = ovs_flow_alloc();
  466. err = PTR_ERR(flow);
  467. if (IS_ERR(flow))
  468. goto err_kfree_skb;
  469. err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
  470. &flow->key, log);
  471. if (err)
  472. goto err_flow_free;
  473. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  474. &flow->key, &acts, log);
  475. if (err)
  476. goto err_flow_free;
  477. rcu_assign_pointer(flow->sf_acts, acts);
  478. OVS_CB(packet)->egress_tun_info = NULL;
  479. packet->priority = flow->key.phy.priority;
  480. packet->mark = flow->key.phy.skb_mark;
  481. rcu_read_lock();
  482. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  483. err = -ENODEV;
  484. if (!dp)
  485. goto err_unlock;
  486. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  487. if (!input_vport)
  488. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  489. if (!input_vport)
  490. goto err_unlock;
  491. OVS_CB(packet)->input_vport = input_vport;
  492. sf_acts = rcu_dereference(flow->sf_acts);
  493. local_bh_disable();
  494. err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
  495. local_bh_enable();
  496. rcu_read_unlock();
  497. ovs_flow_free(flow, false);
  498. return err;
  499. err_unlock:
  500. rcu_read_unlock();
  501. err_flow_free:
  502. ovs_flow_free(flow, false);
  503. err_kfree_skb:
  504. kfree_skb(packet);
  505. err:
  506. return err;
  507. }
  508. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  509. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  510. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  511. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  512. };
  513. static const struct genl_ops dp_packet_genl_ops[] = {
  514. { .cmd = OVS_PACKET_CMD_EXECUTE,
  515. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  516. .policy = packet_policy,
  517. .doit = ovs_packet_cmd_execute
  518. }
  519. };
  520. static struct genl_family dp_packet_genl_family = {
  521. .id = GENL_ID_GENERATE,
  522. .hdrsize = sizeof(struct ovs_header),
  523. .name = OVS_PACKET_FAMILY,
  524. .version = OVS_PACKET_VERSION,
  525. .maxattr = OVS_PACKET_ATTR_MAX,
  526. .netnsok = true,
  527. .parallel_ops = true,
  528. .ops = dp_packet_genl_ops,
  529. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  530. };
  531. static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
  532. struct ovs_dp_megaflow_stats *mega_stats)
  533. {
  534. int i;
  535. memset(mega_stats, 0, sizeof(*mega_stats));
  536. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  537. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  538. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  539. for_each_possible_cpu(i) {
  540. const struct dp_stats_percpu *percpu_stats;
  541. struct dp_stats_percpu local_stats;
  542. unsigned int start;
  543. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  544. do {
  545. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  546. local_stats = *percpu_stats;
  547. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  548. stats->n_hit += local_stats.n_hit;
  549. stats->n_missed += local_stats.n_missed;
  550. stats->n_lost += local_stats.n_lost;
  551. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  552. }
  553. }
  554. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  555. {
  556. return NLMSG_ALIGN(sizeof(struct ovs_header))
  557. + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  558. + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  559. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  560. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  561. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  562. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  563. }
  564. /* Called with ovs_mutex or RCU read lock. */
  565. static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
  566. struct sk_buff *skb)
  567. {
  568. struct nlattr *nla;
  569. int err;
  570. /* Fill flow key. */
  571. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  572. if (!nla)
  573. return -EMSGSIZE;
  574. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  575. if (err)
  576. return err;
  577. nla_nest_end(skb, nla);
  578. /* Fill flow mask. */
  579. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  580. if (!nla)
  581. return -EMSGSIZE;
  582. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  583. if (err)
  584. return err;
  585. nla_nest_end(skb, nla);
  586. return 0;
  587. }
  588. /* Called with ovs_mutex or RCU read lock. */
  589. static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
  590. struct sk_buff *skb)
  591. {
  592. struct ovs_flow_stats stats;
  593. __be16 tcp_flags;
  594. unsigned long used;
  595. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  596. if (used &&
  597. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  598. return -EMSGSIZE;
  599. if (stats.n_packets &&
  600. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  601. return -EMSGSIZE;
  602. if ((u8)ntohs(tcp_flags) &&
  603. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  604. return -EMSGSIZE;
  605. return 0;
  606. }
  607. /* Called with ovs_mutex or RCU read lock. */
  608. static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
  609. struct sk_buff *skb, int skb_orig_len)
  610. {
  611. struct nlattr *start;
  612. int err;
  613. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  614. * this is the first flow to be dumped into 'skb'. This is unusual for
  615. * Netlink but individual action lists can be longer than
  616. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  617. * The userspace caller can always fetch the actions separately if it
  618. * really wants them. (Most userspace callers in fact don't care.)
  619. *
  620. * This can only fail for dump operations because the skb is always
  621. * properly sized for single flows.
  622. */
  623. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  624. if (start) {
  625. const struct sw_flow_actions *sf_acts;
  626. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  627. err = ovs_nla_put_actions(sf_acts->actions,
  628. sf_acts->actions_len, skb);
  629. if (!err)
  630. nla_nest_end(skb, start);
  631. else {
  632. if (skb_orig_len)
  633. return err;
  634. nla_nest_cancel(skb, start);
  635. }
  636. } else if (skb_orig_len) {
  637. return -EMSGSIZE;
  638. }
  639. return 0;
  640. }
  641. /* Called with ovs_mutex or RCU read lock. */
  642. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  643. struct sk_buff *skb, u32 portid,
  644. u32 seq, u32 flags, u8 cmd)
  645. {
  646. const int skb_orig_len = skb->len;
  647. struct ovs_header *ovs_header;
  648. int err;
  649. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
  650. flags, cmd);
  651. if (!ovs_header)
  652. return -EMSGSIZE;
  653. ovs_header->dp_ifindex = dp_ifindex;
  654. err = ovs_flow_cmd_fill_match(flow, skb);
  655. if (err)
  656. goto error;
  657. err = ovs_flow_cmd_fill_stats(flow, skb);
  658. if (err)
  659. goto error;
  660. err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
  661. if (err)
  662. goto error;
  663. return genlmsg_end(skb, ovs_header);
  664. error:
  665. genlmsg_cancel(skb, ovs_header);
  666. return err;
  667. }
  668. /* May not be called with RCU read lock. */
  669. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  670. struct genl_info *info,
  671. bool always)
  672. {
  673. struct sk_buff *skb;
  674. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  675. return NULL;
  676. skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
  677. if (!skb)
  678. return ERR_PTR(-ENOMEM);
  679. return skb;
  680. }
  681. /* Called with ovs_mutex. */
  682. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  683. int dp_ifindex,
  684. struct genl_info *info, u8 cmd,
  685. bool always)
  686. {
  687. struct sk_buff *skb;
  688. int retval;
  689. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
  690. always);
  691. if (IS_ERR_OR_NULL(skb))
  692. return skb;
  693. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  694. info->snd_portid, info->snd_seq, 0,
  695. cmd);
  696. BUG_ON(retval < 0);
  697. return skb;
  698. }
  699. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  700. {
  701. struct nlattr **a = info->attrs;
  702. struct ovs_header *ovs_header = info->userhdr;
  703. struct sw_flow *flow, *new_flow;
  704. struct sw_flow_mask mask;
  705. struct sk_buff *reply;
  706. struct datapath *dp;
  707. struct sw_flow_actions *acts;
  708. struct sw_flow_match match;
  709. int error;
  710. bool log = !a[OVS_FLOW_ATTR_PROBE];
  711. /* Must have key and actions. */
  712. error = -EINVAL;
  713. if (!a[OVS_FLOW_ATTR_KEY]) {
  714. OVS_NLERR(log, "Flow key attr not present in new flow.");
  715. goto error;
  716. }
  717. if (!a[OVS_FLOW_ATTR_ACTIONS]) {
  718. OVS_NLERR(log, "Flow actions attr not present in new flow.");
  719. goto error;
  720. }
  721. /* Most of the time we need to allocate a new flow, do it before
  722. * locking.
  723. */
  724. new_flow = ovs_flow_alloc();
  725. if (IS_ERR(new_flow)) {
  726. error = PTR_ERR(new_flow);
  727. goto error;
  728. }
  729. /* Extract key. */
  730. ovs_match_init(&match, &new_flow->unmasked_key, &mask);
  731. error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
  732. a[OVS_FLOW_ATTR_MASK], log);
  733. if (error)
  734. goto err_kfree_flow;
  735. ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
  736. /* Validate actions. */
  737. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
  738. &acts, log);
  739. if (error) {
  740. OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
  741. goto err_kfree_flow;
  742. }
  743. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  744. if (IS_ERR(reply)) {
  745. error = PTR_ERR(reply);
  746. goto err_kfree_acts;
  747. }
  748. ovs_lock();
  749. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  750. if (unlikely(!dp)) {
  751. error = -ENODEV;
  752. goto err_unlock_ovs;
  753. }
  754. /* Check if this is a duplicate flow */
  755. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
  756. if (likely(!flow)) {
  757. rcu_assign_pointer(new_flow->sf_acts, acts);
  758. /* Put flow in bucket. */
  759. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  760. if (unlikely(error)) {
  761. acts = NULL;
  762. goto err_unlock_ovs;
  763. }
  764. if (unlikely(reply)) {
  765. error = ovs_flow_cmd_fill_info(new_flow,
  766. ovs_header->dp_ifindex,
  767. reply, info->snd_portid,
  768. info->snd_seq, 0,
  769. OVS_FLOW_CMD_NEW);
  770. BUG_ON(error < 0);
  771. }
  772. ovs_unlock();
  773. } else {
  774. struct sw_flow_actions *old_acts;
  775. /* Bail out if we're not allowed to modify an existing flow.
  776. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  777. * because Generic Netlink treats the latter as a dump
  778. * request. We also accept NLM_F_EXCL in case that bug ever
  779. * gets fixed.
  780. */
  781. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  782. | NLM_F_EXCL))) {
  783. error = -EEXIST;
  784. goto err_unlock_ovs;
  785. }
  786. /* The unmasked key has to be the same for flow updates. */
  787. if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
  788. /* Look for any overlapping flow. */
  789. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  790. if (!flow) {
  791. error = -ENOENT;
  792. goto err_unlock_ovs;
  793. }
  794. }
  795. /* Update actions. */
  796. old_acts = ovsl_dereference(flow->sf_acts);
  797. rcu_assign_pointer(flow->sf_acts, acts);
  798. if (unlikely(reply)) {
  799. error = ovs_flow_cmd_fill_info(flow,
  800. ovs_header->dp_ifindex,
  801. reply, info->snd_portid,
  802. info->snd_seq, 0,
  803. OVS_FLOW_CMD_NEW);
  804. BUG_ON(error < 0);
  805. }
  806. ovs_unlock();
  807. ovs_nla_free_flow_actions(old_acts);
  808. ovs_flow_free(new_flow, false);
  809. }
  810. if (reply)
  811. ovs_notify(&dp_flow_genl_family, reply, info);
  812. return 0;
  813. err_unlock_ovs:
  814. ovs_unlock();
  815. kfree_skb(reply);
  816. err_kfree_acts:
  817. kfree(acts);
  818. err_kfree_flow:
  819. ovs_flow_free(new_flow, false);
  820. error:
  821. return error;
  822. }
  823. /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
  824. static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
  825. const struct sw_flow_key *key,
  826. const struct sw_flow_mask *mask,
  827. bool log)
  828. {
  829. struct sw_flow_actions *acts;
  830. struct sw_flow_key masked_key;
  831. int error;
  832. ovs_flow_mask_key(&masked_key, key, mask);
  833. error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
  834. if (error) {
  835. OVS_NLERR(log,
  836. "Actions may not be safe on all matching packets");
  837. return ERR_PTR(error);
  838. }
  839. return acts;
  840. }
  841. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  842. {
  843. struct nlattr **a = info->attrs;
  844. struct ovs_header *ovs_header = info->userhdr;
  845. struct sw_flow_key key;
  846. struct sw_flow *flow;
  847. struct sw_flow_mask mask;
  848. struct sk_buff *reply = NULL;
  849. struct datapath *dp;
  850. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  851. struct sw_flow_match match;
  852. int error;
  853. bool log = !a[OVS_FLOW_ATTR_PROBE];
  854. /* Extract key. */
  855. error = -EINVAL;
  856. if (!a[OVS_FLOW_ATTR_KEY]) {
  857. OVS_NLERR(log, "Flow key attribute not present in set flow.");
  858. goto error;
  859. }
  860. ovs_match_init(&match, &key, &mask);
  861. error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
  862. a[OVS_FLOW_ATTR_MASK], log);
  863. if (error)
  864. goto error;
  865. /* Validate actions. */
  866. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  867. acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
  868. log);
  869. if (IS_ERR(acts)) {
  870. error = PTR_ERR(acts);
  871. goto error;
  872. }
  873. /* Can allocate before locking if have acts. */
  874. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  875. if (IS_ERR(reply)) {
  876. error = PTR_ERR(reply);
  877. goto err_kfree_acts;
  878. }
  879. }
  880. ovs_lock();
  881. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  882. if (unlikely(!dp)) {
  883. error = -ENODEV;
  884. goto err_unlock_ovs;
  885. }
  886. /* Check that the flow exists. */
  887. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  888. if (unlikely(!flow)) {
  889. error = -ENOENT;
  890. goto err_unlock_ovs;
  891. }
  892. /* Update actions, if present. */
  893. if (likely(acts)) {
  894. old_acts = ovsl_dereference(flow->sf_acts);
  895. rcu_assign_pointer(flow->sf_acts, acts);
  896. if (unlikely(reply)) {
  897. error = ovs_flow_cmd_fill_info(flow,
  898. ovs_header->dp_ifindex,
  899. reply, info->snd_portid,
  900. info->snd_seq, 0,
  901. OVS_FLOW_CMD_NEW);
  902. BUG_ON(error < 0);
  903. }
  904. } else {
  905. /* Could not alloc without acts before locking. */
  906. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  907. info, OVS_FLOW_CMD_NEW, false);
  908. if (unlikely(IS_ERR(reply))) {
  909. error = PTR_ERR(reply);
  910. goto err_unlock_ovs;
  911. }
  912. }
  913. /* Clear stats. */
  914. if (a[OVS_FLOW_ATTR_CLEAR])
  915. ovs_flow_stats_clear(flow);
  916. ovs_unlock();
  917. if (reply)
  918. ovs_notify(&dp_flow_genl_family, reply, info);
  919. if (old_acts)
  920. ovs_nla_free_flow_actions(old_acts);
  921. return 0;
  922. err_unlock_ovs:
  923. ovs_unlock();
  924. kfree_skb(reply);
  925. err_kfree_acts:
  926. kfree(acts);
  927. error:
  928. return error;
  929. }
  930. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  931. {
  932. struct nlattr **a = info->attrs;
  933. struct ovs_header *ovs_header = info->userhdr;
  934. struct sw_flow_key key;
  935. struct sk_buff *reply;
  936. struct sw_flow *flow;
  937. struct datapath *dp;
  938. struct sw_flow_match match;
  939. int err;
  940. bool log = !a[OVS_FLOW_ATTR_PROBE];
  941. if (!a[OVS_FLOW_ATTR_KEY]) {
  942. OVS_NLERR(log,
  943. "Flow get message rejected, Key attribute missing.");
  944. return -EINVAL;
  945. }
  946. ovs_match_init(&match, &key, NULL);
  947. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL, log);
  948. if (err)
  949. return err;
  950. ovs_lock();
  951. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  952. if (!dp) {
  953. err = -ENODEV;
  954. goto unlock;
  955. }
  956. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  957. if (!flow) {
  958. err = -ENOENT;
  959. goto unlock;
  960. }
  961. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  962. OVS_FLOW_CMD_NEW, true);
  963. if (IS_ERR(reply)) {
  964. err = PTR_ERR(reply);
  965. goto unlock;
  966. }
  967. ovs_unlock();
  968. return genlmsg_reply(reply, info);
  969. unlock:
  970. ovs_unlock();
  971. return err;
  972. }
  973. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  974. {
  975. struct nlattr **a = info->attrs;
  976. struct ovs_header *ovs_header = info->userhdr;
  977. struct sw_flow_key key;
  978. struct sk_buff *reply;
  979. struct sw_flow *flow;
  980. struct datapath *dp;
  981. struct sw_flow_match match;
  982. int err;
  983. bool log = !a[OVS_FLOW_ATTR_PROBE];
  984. if (likely(a[OVS_FLOW_ATTR_KEY])) {
  985. ovs_match_init(&match, &key, NULL);
  986. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
  987. log);
  988. if (unlikely(err))
  989. return err;
  990. }
  991. ovs_lock();
  992. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  993. if (unlikely(!dp)) {
  994. err = -ENODEV;
  995. goto unlock;
  996. }
  997. if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
  998. err = ovs_flow_tbl_flush(&dp->table);
  999. goto unlock;
  1000. }
  1001. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1002. if (unlikely(!flow)) {
  1003. err = -ENOENT;
  1004. goto unlock;
  1005. }
  1006. ovs_flow_tbl_remove(&dp->table, flow);
  1007. ovs_unlock();
  1008. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  1009. info, false);
  1010. if (likely(reply)) {
  1011. if (likely(!IS_ERR(reply))) {
  1012. rcu_read_lock(); /*To keep RCU checker happy. */
  1013. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  1014. reply, info->snd_portid,
  1015. info->snd_seq, 0,
  1016. OVS_FLOW_CMD_DEL);
  1017. rcu_read_unlock();
  1018. BUG_ON(err < 0);
  1019. ovs_notify(&dp_flow_genl_family, reply, info);
  1020. } else {
  1021. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  1022. }
  1023. }
  1024. ovs_flow_free(flow, true);
  1025. return 0;
  1026. unlock:
  1027. ovs_unlock();
  1028. return err;
  1029. }
  1030. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1031. {
  1032. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1033. struct table_instance *ti;
  1034. struct datapath *dp;
  1035. rcu_read_lock();
  1036. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1037. if (!dp) {
  1038. rcu_read_unlock();
  1039. return -ENODEV;
  1040. }
  1041. ti = rcu_dereference(dp->table.ti);
  1042. for (;;) {
  1043. struct sw_flow *flow;
  1044. u32 bucket, obj;
  1045. bucket = cb->args[0];
  1046. obj = cb->args[1];
  1047. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1048. if (!flow)
  1049. break;
  1050. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1051. NETLINK_CB(cb->skb).portid,
  1052. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1053. OVS_FLOW_CMD_NEW) < 0)
  1054. break;
  1055. cb->args[0] = bucket;
  1056. cb->args[1] = obj;
  1057. }
  1058. rcu_read_unlock();
  1059. return skb->len;
  1060. }
  1061. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1062. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1063. [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
  1064. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1065. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1066. [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
  1067. };
  1068. static const struct genl_ops dp_flow_genl_ops[] = {
  1069. { .cmd = OVS_FLOW_CMD_NEW,
  1070. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1071. .policy = flow_policy,
  1072. .doit = ovs_flow_cmd_new
  1073. },
  1074. { .cmd = OVS_FLOW_CMD_DEL,
  1075. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1076. .policy = flow_policy,
  1077. .doit = ovs_flow_cmd_del
  1078. },
  1079. { .cmd = OVS_FLOW_CMD_GET,
  1080. .flags = 0, /* OK for unprivileged users. */
  1081. .policy = flow_policy,
  1082. .doit = ovs_flow_cmd_get,
  1083. .dumpit = ovs_flow_cmd_dump
  1084. },
  1085. { .cmd = OVS_FLOW_CMD_SET,
  1086. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1087. .policy = flow_policy,
  1088. .doit = ovs_flow_cmd_set,
  1089. },
  1090. };
  1091. static struct genl_family dp_flow_genl_family = {
  1092. .id = GENL_ID_GENERATE,
  1093. .hdrsize = sizeof(struct ovs_header),
  1094. .name = OVS_FLOW_FAMILY,
  1095. .version = OVS_FLOW_VERSION,
  1096. .maxattr = OVS_FLOW_ATTR_MAX,
  1097. .netnsok = true,
  1098. .parallel_ops = true,
  1099. .ops = dp_flow_genl_ops,
  1100. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1101. .mcgrps = &ovs_dp_flow_multicast_group,
  1102. .n_mcgrps = 1,
  1103. };
  1104. static size_t ovs_dp_cmd_msg_size(void)
  1105. {
  1106. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1107. msgsize += nla_total_size(IFNAMSIZ);
  1108. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1109. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  1110. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1111. return msgsize;
  1112. }
  1113. /* Called with ovs_mutex. */
  1114. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1115. u32 portid, u32 seq, u32 flags, u8 cmd)
  1116. {
  1117. struct ovs_header *ovs_header;
  1118. struct ovs_dp_stats dp_stats;
  1119. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1120. int err;
  1121. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1122. flags, cmd);
  1123. if (!ovs_header)
  1124. goto error;
  1125. ovs_header->dp_ifindex = get_dpifindex(dp);
  1126. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1127. if (err)
  1128. goto nla_put_failure;
  1129. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1130. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1131. &dp_stats))
  1132. goto nla_put_failure;
  1133. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1134. sizeof(struct ovs_dp_megaflow_stats),
  1135. &dp_megaflow_stats))
  1136. goto nla_put_failure;
  1137. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1138. goto nla_put_failure;
  1139. return genlmsg_end(skb, ovs_header);
  1140. nla_put_failure:
  1141. genlmsg_cancel(skb, ovs_header);
  1142. error:
  1143. return -EMSGSIZE;
  1144. }
  1145. static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
  1146. {
  1147. return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  1148. }
  1149. /* Called with rcu_read_lock or ovs_mutex. */
  1150. static struct datapath *lookup_datapath(struct net *net,
  1151. const struct ovs_header *ovs_header,
  1152. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1153. {
  1154. struct datapath *dp;
  1155. if (!a[OVS_DP_ATTR_NAME])
  1156. dp = get_dp(net, ovs_header->dp_ifindex);
  1157. else {
  1158. struct vport *vport;
  1159. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1160. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1161. }
  1162. return dp ? dp : ERR_PTR(-ENODEV);
  1163. }
  1164. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1165. {
  1166. struct datapath *dp;
  1167. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1168. if (IS_ERR(dp))
  1169. return;
  1170. WARN(dp->user_features, "Dropping previously announced user features\n");
  1171. dp->user_features = 0;
  1172. }
  1173. static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
  1174. {
  1175. if (a[OVS_DP_ATTR_USER_FEATURES])
  1176. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1177. }
  1178. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1179. {
  1180. struct nlattr **a = info->attrs;
  1181. struct vport_parms parms;
  1182. struct sk_buff *reply;
  1183. struct datapath *dp;
  1184. struct vport *vport;
  1185. struct ovs_net *ovs_net;
  1186. int err, i;
  1187. err = -EINVAL;
  1188. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1189. goto err;
  1190. reply = ovs_dp_cmd_alloc_info(info);
  1191. if (!reply)
  1192. return -ENOMEM;
  1193. err = -ENOMEM;
  1194. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1195. if (dp == NULL)
  1196. goto err_free_reply;
  1197. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1198. /* Allocate table. */
  1199. err = ovs_flow_tbl_init(&dp->table);
  1200. if (err)
  1201. goto err_free_dp;
  1202. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1203. if (!dp->stats_percpu) {
  1204. err = -ENOMEM;
  1205. goto err_destroy_table;
  1206. }
  1207. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1208. GFP_KERNEL);
  1209. if (!dp->ports) {
  1210. err = -ENOMEM;
  1211. goto err_destroy_percpu;
  1212. }
  1213. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1214. INIT_HLIST_HEAD(&dp->ports[i]);
  1215. /* Set up our datapath device. */
  1216. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1217. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1218. parms.options = NULL;
  1219. parms.dp = dp;
  1220. parms.port_no = OVSP_LOCAL;
  1221. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1222. ovs_dp_change(dp, a);
  1223. /* So far only local changes have been made, now need the lock. */
  1224. ovs_lock();
  1225. vport = new_vport(&parms);
  1226. if (IS_ERR(vport)) {
  1227. err = PTR_ERR(vport);
  1228. if (err == -EBUSY)
  1229. err = -EEXIST;
  1230. if (err == -EEXIST) {
  1231. /* An outdated user space instance that does not understand
  1232. * the concept of user_features has attempted to create a new
  1233. * datapath and is likely to reuse it. Drop all user features.
  1234. */
  1235. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1236. ovs_dp_reset_user_features(skb, info);
  1237. }
  1238. goto err_destroy_ports_array;
  1239. }
  1240. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1241. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1242. BUG_ON(err < 0);
  1243. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1244. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1245. ovs_unlock();
  1246. ovs_notify(&dp_datapath_genl_family, reply, info);
  1247. return 0;
  1248. err_destroy_ports_array:
  1249. ovs_unlock();
  1250. kfree(dp->ports);
  1251. err_destroy_percpu:
  1252. free_percpu(dp->stats_percpu);
  1253. err_destroy_table:
  1254. ovs_flow_tbl_destroy(&dp->table);
  1255. err_free_dp:
  1256. release_net(ovs_dp_get_net(dp));
  1257. kfree(dp);
  1258. err_free_reply:
  1259. kfree_skb(reply);
  1260. err:
  1261. return err;
  1262. }
  1263. /* Called with ovs_mutex. */
  1264. static void __dp_destroy(struct datapath *dp)
  1265. {
  1266. int i;
  1267. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1268. struct vport *vport;
  1269. struct hlist_node *n;
  1270. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1271. if (vport->port_no != OVSP_LOCAL)
  1272. ovs_dp_detach_port(vport);
  1273. }
  1274. list_del_rcu(&dp->list_node);
  1275. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1276. * all ports in datapath are destroyed first before freeing datapath.
  1277. */
  1278. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1279. /* RCU destroy the flow table */
  1280. call_rcu(&dp->rcu, destroy_dp_rcu);
  1281. }
  1282. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1283. {
  1284. struct sk_buff *reply;
  1285. struct datapath *dp;
  1286. int err;
  1287. reply = ovs_dp_cmd_alloc_info(info);
  1288. if (!reply)
  1289. return -ENOMEM;
  1290. ovs_lock();
  1291. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1292. err = PTR_ERR(dp);
  1293. if (IS_ERR(dp))
  1294. goto err_unlock_free;
  1295. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1296. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1297. BUG_ON(err < 0);
  1298. __dp_destroy(dp);
  1299. ovs_unlock();
  1300. ovs_notify(&dp_datapath_genl_family, reply, info);
  1301. return 0;
  1302. err_unlock_free:
  1303. ovs_unlock();
  1304. kfree_skb(reply);
  1305. return err;
  1306. }
  1307. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1308. {
  1309. struct sk_buff *reply;
  1310. struct datapath *dp;
  1311. int err;
  1312. reply = ovs_dp_cmd_alloc_info(info);
  1313. if (!reply)
  1314. return -ENOMEM;
  1315. ovs_lock();
  1316. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1317. err = PTR_ERR(dp);
  1318. if (IS_ERR(dp))
  1319. goto err_unlock_free;
  1320. ovs_dp_change(dp, info->attrs);
  1321. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1322. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1323. BUG_ON(err < 0);
  1324. ovs_unlock();
  1325. ovs_notify(&dp_datapath_genl_family, reply, info);
  1326. return 0;
  1327. err_unlock_free:
  1328. ovs_unlock();
  1329. kfree_skb(reply);
  1330. return err;
  1331. }
  1332. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1333. {
  1334. struct sk_buff *reply;
  1335. struct datapath *dp;
  1336. int err;
  1337. reply = ovs_dp_cmd_alloc_info(info);
  1338. if (!reply)
  1339. return -ENOMEM;
  1340. ovs_lock();
  1341. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1342. if (IS_ERR(dp)) {
  1343. err = PTR_ERR(dp);
  1344. goto err_unlock_free;
  1345. }
  1346. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1347. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1348. BUG_ON(err < 0);
  1349. ovs_unlock();
  1350. return genlmsg_reply(reply, info);
  1351. err_unlock_free:
  1352. ovs_unlock();
  1353. kfree_skb(reply);
  1354. return err;
  1355. }
  1356. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1357. {
  1358. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1359. struct datapath *dp;
  1360. int skip = cb->args[0];
  1361. int i = 0;
  1362. ovs_lock();
  1363. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1364. if (i >= skip &&
  1365. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1366. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1367. OVS_DP_CMD_NEW) < 0)
  1368. break;
  1369. i++;
  1370. }
  1371. ovs_unlock();
  1372. cb->args[0] = i;
  1373. return skb->len;
  1374. }
  1375. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1376. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1377. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1378. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1379. };
  1380. static const struct genl_ops dp_datapath_genl_ops[] = {
  1381. { .cmd = OVS_DP_CMD_NEW,
  1382. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1383. .policy = datapath_policy,
  1384. .doit = ovs_dp_cmd_new
  1385. },
  1386. { .cmd = OVS_DP_CMD_DEL,
  1387. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1388. .policy = datapath_policy,
  1389. .doit = ovs_dp_cmd_del
  1390. },
  1391. { .cmd = OVS_DP_CMD_GET,
  1392. .flags = 0, /* OK for unprivileged users. */
  1393. .policy = datapath_policy,
  1394. .doit = ovs_dp_cmd_get,
  1395. .dumpit = ovs_dp_cmd_dump
  1396. },
  1397. { .cmd = OVS_DP_CMD_SET,
  1398. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1399. .policy = datapath_policy,
  1400. .doit = ovs_dp_cmd_set,
  1401. },
  1402. };
  1403. static struct genl_family dp_datapath_genl_family = {
  1404. .id = GENL_ID_GENERATE,
  1405. .hdrsize = sizeof(struct ovs_header),
  1406. .name = OVS_DATAPATH_FAMILY,
  1407. .version = OVS_DATAPATH_VERSION,
  1408. .maxattr = OVS_DP_ATTR_MAX,
  1409. .netnsok = true,
  1410. .parallel_ops = true,
  1411. .ops = dp_datapath_genl_ops,
  1412. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1413. .mcgrps = &ovs_dp_datapath_multicast_group,
  1414. .n_mcgrps = 1,
  1415. };
  1416. /* Called with ovs_mutex or RCU read lock. */
  1417. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1418. u32 portid, u32 seq, u32 flags, u8 cmd)
  1419. {
  1420. struct ovs_header *ovs_header;
  1421. struct ovs_vport_stats vport_stats;
  1422. int err;
  1423. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1424. flags, cmd);
  1425. if (!ovs_header)
  1426. return -EMSGSIZE;
  1427. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1428. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1429. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1430. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1431. vport->ops->get_name(vport)))
  1432. goto nla_put_failure;
  1433. ovs_vport_get_stats(vport, &vport_stats);
  1434. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1435. &vport_stats))
  1436. goto nla_put_failure;
  1437. if (ovs_vport_get_upcall_portids(vport, skb))
  1438. goto nla_put_failure;
  1439. err = ovs_vport_get_options(vport, skb);
  1440. if (err == -EMSGSIZE)
  1441. goto error;
  1442. return genlmsg_end(skb, ovs_header);
  1443. nla_put_failure:
  1444. err = -EMSGSIZE;
  1445. error:
  1446. genlmsg_cancel(skb, ovs_header);
  1447. return err;
  1448. }
  1449. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1450. {
  1451. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1452. }
  1453. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1454. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1455. u32 seq, u8 cmd)
  1456. {
  1457. struct sk_buff *skb;
  1458. int retval;
  1459. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1460. if (!skb)
  1461. return ERR_PTR(-ENOMEM);
  1462. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1463. BUG_ON(retval < 0);
  1464. return skb;
  1465. }
  1466. /* Called with ovs_mutex or RCU read lock. */
  1467. static struct vport *lookup_vport(struct net *net,
  1468. const struct ovs_header *ovs_header,
  1469. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1470. {
  1471. struct datapath *dp;
  1472. struct vport *vport;
  1473. if (a[OVS_VPORT_ATTR_NAME]) {
  1474. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1475. if (!vport)
  1476. return ERR_PTR(-ENODEV);
  1477. if (ovs_header->dp_ifindex &&
  1478. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1479. return ERR_PTR(-ENODEV);
  1480. return vport;
  1481. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1482. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1483. if (port_no >= DP_MAX_PORTS)
  1484. return ERR_PTR(-EFBIG);
  1485. dp = get_dp(net, ovs_header->dp_ifindex);
  1486. if (!dp)
  1487. return ERR_PTR(-ENODEV);
  1488. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1489. if (!vport)
  1490. return ERR_PTR(-ENODEV);
  1491. return vport;
  1492. } else
  1493. return ERR_PTR(-EINVAL);
  1494. }
  1495. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1496. {
  1497. struct nlattr **a = info->attrs;
  1498. struct ovs_header *ovs_header = info->userhdr;
  1499. struct vport_parms parms;
  1500. struct sk_buff *reply;
  1501. struct vport *vport;
  1502. struct datapath *dp;
  1503. u32 port_no;
  1504. int err;
  1505. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1506. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1507. return -EINVAL;
  1508. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1509. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1510. if (port_no >= DP_MAX_PORTS)
  1511. return -EFBIG;
  1512. reply = ovs_vport_cmd_alloc_info();
  1513. if (!reply)
  1514. return -ENOMEM;
  1515. ovs_lock();
  1516. restart:
  1517. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1518. err = -ENODEV;
  1519. if (!dp)
  1520. goto exit_unlock_free;
  1521. if (port_no) {
  1522. vport = ovs_vport_ovsl(dp, port_no);
  1523. err = -EBUSY;
  1524. if (vport)
  1525. goto exit_unlock_free;
  1526. } else {
  1527. for (port_no = 1; ; port_no++) {
  1528. if (port_no >= DP_MAX_PORTS) {
  1529. err = -EFBIG;
  1530. goto exit_unlock_free;
  1531. }
  1532. vport = ovs_vport_ovsl(dp, port_no);
  1533. if (!vport)
  1534. break;
  1535. }
  1536. }
  1537. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1538. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1539. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1540. parms.dp = dp;
  1541. parms.port_no = port_no;
  1542. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1543. vport = new_vport(&parms);
  1544. err = PTR_ERR(vport);
  1545. if (IS_ERR(vport)) {
  1546. if (err == -EAGAIN)
  1547. goto restart;
  1548. goto exit_unlock_free;
  1549. }
  1550. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1551. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1552. BUG_ON(err < 0);
  1553. ovs_unlock();
  1554. ovs_notify(&dp_vport_genl_family, reply, info);
  1555. return 0;
  1556. exit_unlock_free:
  1557. ovs_unlock();
  1558. kfree_skb(reply);
  1559. return err;
  1560. }
  1561. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1562. {
  1563. struct nlattr **a = info->attrs;
  1564. struct sk_buff *reply;
  1565. struct vport *vport;
  1566. int err;
  1567. reply = ovs_vport_cmd_alloc_info();
  1568. if (!reply)
  1569. return -ENOMEM;
  1570. ovs_lock();
  1571. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1572. err = PTR_ERR(vport);
  1573. if (IS_ERR(vport))
  1574. goto exit_unlock_free;
  1575. if (a[OVS_VPORT_ATTR_TYPE] &&
  1576. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1577. err = -EINVAL;
  1578. goto exit_unlock_free;
  1579. }
  1580. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1581. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1582. if (err)
  1583. goto exit_unlock_free;
  1584. }
  1585. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1586. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1587. err = ovs_vport_set_upcall_portids(vport, ids);
  1588. if (err)
  1589. goto exit_unlock_free;
  1590. }
  1591. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1592. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1593. BUG_ON(err < 0);
  1594. ovs_unlock();
  1595. ovs_notify(&dp_vport_genl_family, reply, info);
  1596. return 0;
  1597. exit_unlock_free:
  1598. ovs_unlock();
  1599. kfree_skb(reply);
  1600. return err;
  1601. }
  1602. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1603. {
  1604. struct nlattr **a = info->attrs;
  1605. struct sk_buff *reply;
  1606. struct vport *vport;
  1607. int err;
  1608. reply = ovs_vport_cmd_alloc_info();
  1609. if (!reply)
  1610. return -ENOMEM;
  1611. ovs_lock();
  1612. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1613. err = PTR_ERR(vport);
  1614. if (IS_ERR(vport))
  1615. goto exit_unlock_free;
  1616. if (vport->port_no == OVSP_LOCAL) {
  1617. err = -EINVAL;
  1618. goto exit_unlock_free;
  1619. }
  1620. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1621. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1622. BUG_ON(err < 0);
  1623. ovs_dp_detach_port(vport);
  1624. ovs_unlock();
  1625. ovs_notify(&dp_vport_genl_family, reply, info);
  1626. return 0;
  1627. exit_unlock_free:
  1628. ovs_unlock();
  1629. kfree_skb(reply);
  1630. return err;
  1631. }
  1632. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1633. {
  1634. struct nlattr **a = info->attrs;
  1635. struct ovs_header *ovs_header = info->userhdr;
  1636. struct sk_buff *reply;
  1637. struct vport *vport;
  1638. int err;
  1639. reply = ovs_vport_cmd_alloc_info();
  1640. if (!reply)
  1641. return -ENOMEM;
  1642. rcu_read_lock();
  1643. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1644. err = PTR_ERR(vport);
  1645. if (IS_ERR(vport))
  1646. goto exit_unlock_free;
  1647. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1648. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1649. BUG_ON(err < 0);
  1650. rcu_read_unlock();
  1651. return genlmsg_reply(reply, info);
  1652. exit_unlock_free:
  1653. rcu_read_unlock();
  1654. kfree_skb(reply);
  1655. return err;
  1656. }
  1657. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1658. {
  1659. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1660. struct datapath *dp;
  1661. int bucket = cb->args[0], skip = cb->args[1];
  1662. int i, j = 0;
  1663. rcu_read_lock();
  1664. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1665. if (!dp) {
  1666. rcu_read_unlock();
  1667. return -ENODEV;
  1668. }
  1669. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1670. struct vport *vport;
  1671. j = 0;
  1672. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1673. if (j >= skip &&
  1674. ovs_vport_cmd_fill_info(vport, skb,
  1675. NETLINK_CB(cb->skb).portid,
  1676. cb->nlh->nlmsg_seq,
  1677. NLM_F_MULTI,
  1678. OVS_VPORT_CMD_NEW) < 0)
  1679. goto out;
  1680. j++;
  1681. }
  1682. skip = 0;
  1683. }
  1684. out:
  1685. rcu_read_unlock();
  1686. cb->args[0] = i;
  1687. cb->args[1] = j;
  1688. return skb->len;
  1689. }
  1690. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1691. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1692. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1693. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1694. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1695. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1696. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1697. };
  1698. static const struct genl_ops dp_vport_genl_ops[] = {
  1699. { .cmd = OVS_VPORT_CMD_NEW,
  1700. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1701. .policy = vport_policy,
  1702. .doit = ovs_vport_cmd_new
  1703. },
  1704. { .cmd = OVS_VPORT_CMD_DEL,
  1705. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1706. .policy = vport_policy,
  1707. .doit = ovs_vport_cmd_del
  1708. },
  1709. { .cmd = OVS_VPORT_CMD_GET,
  1710. .flags = 0, /* OK for unprivileged users. */
  1711. .policy = vport_policy,
  1712. .doit = ovs_vport_cmd_get,
  1713. .dumpit = ovs_vport_cmd_dump
  1714. },
  1715. { .cmd = OVS_VPORT_CMD_SET,
  1716. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1717. .policy = vport_policy,
  1718. .doit = ovs_vport_cmd_set,
  1719. },
  1720. };
  1721. struct genl_family dp_vport_genl_family = {
  1722. .id = GENL_ID_GENERATE,
  1723. .hdrsize = sizeof(struct ovs_header),
  1724. .name = OVS_VPORT_FAMILY,
  1725. .version = OVS_VPORT_VERSION,
  1726. .maxattr = OVS_VPORT_ATTR_MAX,
  1727. .netnsok = true,
  1728. .parallel_ops = true,
  1729. .ops = dp_vport_genl_ops,
  1730. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1731. .mcgrps = &ovs_dp_vport_multicast_group,
  1732. .n_mcgrps = 1,
  1733. };
  1734. static struct genl_family * const dp_genl_families[] = {
  1735. &dp_datapath_genl_family,
  1736. &dp_vport_genl_family,
  1737. &dp_flow_genl_family,
  1738. &dp_packet_genl_family,
  1739. };
  1740. static void dp_unregister_genl(int n_families)
  1741. {
  1742. int i;
  1743. for (i = 0; i < n_families; i++)
  1744. genl_unregister_family(dp_genl_families[i]);
  1745. }
  1746. static int dp_register_genl(void)
  1747. {
  1748. int err;
  1749. int i;
  1750. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1751. err = genl_register_family(dp_genl_families[i]);
  1752. if (err)
  1753. goto error;
  1754. }
  1755. return 0;
  1756. error:
  1757. dp_unregister_genl(i);
  1758. return err;
  1759. }
  1760. static int __net_init ovs_init_net(struct net *net)
  1761. {
  1762. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1763. INIT_LIST_HEAD(&ovs_net->dps);
  1764. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1765. return 0;
  1766. }
  1767. static void __net_exit ovs_exit_net(struct net *net)
  1768. {
  1769. struct datapath *dp, *dp_next;
  1770. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1771. ovs_lock();
  1772. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1773. __dp_destroy(dp);
  1774. ovs_unlock();
  1775. cancel_work_sync(&ovs_net->dp_notify_work);
  1776. }
  1777. static struct pernet_operations ovs_net_ops = {
  1778. .init = ovs_init_net,
  1779. .exit = ovs_exit_net,
  1780. .id = &ovs_net_id,
  1781. .size = sizeof(struct ovs_net),
  1782. };
  1783. static int __init dp_init(void)
  1784. {
  1785. int err;
  1786. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1787. pr_info("Open vSwitch switching datapath\n");
  1788. err = action_fifos_init();
  1789. if (err)
  1790. goto error;
  1791. err = ovs_internal_dev_rtnl_link_register();
  1792. if (err)
  1793. goto error_action_fifos_exit;
  1794. err = ovs_flow_init();
  1795. if (err)
  1796. goto error_unreg_rtnl_link;
  1797. err = ovs_vport_init();
  1798. if (err)
  1799. goto error_flow_exit;
  1800. err = register_pernet_device(&ovs_net_ops);
  1801. if (err)
  1802. goto error_vport_exit;
  1803. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1804. if (err)
  1805. goto error_netns_exit;
  1806. err = ovs_netdev_init();
  1807. if (err)
  1808. goto error_unreg_notifier;
  1809. err = dp_register_genl();
  1810. if (err < 0)
  1811. goto error_unreg_netdev;
  1812. return 0;
  1813. error_unreg_netdev:
  1814. ovs_netdev_exit();
  1815. error_unreg_notifier:
  1816. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1817. error_netns_exit:
  1818. unregister_pernet_device(&ovs_net_ops);
  1819. error_vport_exit:
  1820. ovs_vport_exit();
  1821. error_flow_exit:
  1822. ovs_flow_exit();
  1823. error_unreg_rtnl_link:
  1824. ovs_internal_dev_rtnl_link_unregister();
  1825. error_action_fifos_exit:
  1826. action_fifos_exit();
  1827. error:
  1828. return err;
  1829. }
  1830. static void dp_cleanup(void)
  1831. {
  1832. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1833. ovs_netdev_exit();
  1834. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1835. unregister_pernet_device(&ovs_net_ops);
  1836. rcu_barrier();
  1837. ovs_vport_exit();
  1838. ovs_flow_exit();
  1839. ovs_internal_dev_rtnl_link_unregister();
  1840. action_fifos_exit();
  1841. }
  1842. module_init(dp_init);
  1843. module_exit(dp_cleanup);
  1844. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1845. MODULE_LICENSE("GPL");