datapath.c 51 KB

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