datapath.c 56 KB

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